2017-06-20 19 views
1

在下面,我向您展示了我使用perl正则表达式所做的部分代码。在那里我触动了怎么办代码

部分:

if(-f $outfile) 
     { 
     $word=~s/("\S+|\S+|")\s*/$1/g; 

       print $fh_out '<a href="' .$word.'/'.$word. '.html">'.$word.'</a>'; 
     } 
     else 
     { 
      print "invalid"; 
     } 

只考虑$字部分:

这里$字有以下内容:

DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv1 
tv2 
tv3 
tv4 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv11 
tv12 
tv13 
tv14 
tv15 
1 
2 
3 

为了您的类信息:

这里tv5(即数字5比其他电视更大) 。所以其他行仍然是相同的地方,电视的范围开始在那个范围它应该与其他电视相比,并打印最大的一个。像更多的行后,我们会发现tv11 .. tv15更明智的点。它应该打印电视15。你的知识其他行不应该受影响,而为电视*

我想将$word到以下内容。我如何用正则表达式来做到这一点?

$word预计输出

DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv15 
1 
2 
3 
+0

更具体一点,你想过滤什么?之前'$ word'元素的顺序与预期输出的顺序不同。 –

+0

它与“电视* 5”元素有什么关系,需要保留,其他要过滤吗? –

+0

是的,在这里tv5(即数字5比其他电视更大)。所以其他行仍然是相同的电视范围开始在该范围它应该与其他电视比较,并打印最大的一个。我们会发现tv11..tv15.At它应该打印tv15.For知识其他行不应该受影响,而为电视* @JohnDoe –

回答

0

也许不是最有效的或正确的方式做到这一点,但它是不傻,如果它的工作原理,对不对? (不是真的)。

use strict; 
use warnings; 

# read the $word, you will ignore this part since you already have it 
my $word = ""; 
while(<DATA>) 
{ 
    $word .= $_; 
} 

# split each line from $word and store the lines in an array 
my @lines = split /\n/, $word; 

# the initial value is 0 
my $max_value = 0; 

# iterate over the lines 
for (my $i = 0; $i < scalar (@lines); $i++) 
{ 
    # if the current line matches the pattern tv1234* 
    if ($lines[$i] =~ /tv(\d*)/) 
    { 
     # $max_value will store the 1234* 
     $max_value = int($1); 
     # get the index of the next line 
     my $j = $i+1; 

     # if the next element matches the pattern tv1234* 
     if ($lines[$j] =~ /tv(\d*)/) 
     { 
      # check if the value if the value from the next line is 
      # lower than the max value 
      if (int($1) < $max_value) 
      { 
       # if it is remove the next line 
       splice(@lines, $j, 1); 
      } 
      else 
      { 
       # max value is the value of the next line 
       $max_value = int ($1); 
       # otherwise remove the current one 
       splice (@lines, $i, 1); 
       # since we delete the current index, decrement it to don't skip 
       $i--; 
      } 
     } 
     # we finished the block of tv*s so we reset the max value 
     else 
     { 
      $max_value = 0; 
     } 
    } 
} 

# recompute the initial $word with the filtered lines 
my $word = join ("\n", @lines); 
print $word; 

__DATA__ 
DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv1 
tv2 
tv3 
tv4 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv11 
tv12 
tv13 
tv14 
tv15 
1 
2 
3 
+0

我面对错误,当我使用文件句柄。我发布了相同的改变代码与问题。@ John Doe –

+0

我已经说了我的答案下面的错误。@ John Doe –

0

这是我的参考链接,我修改了代码。 code copy and utilization

我的代码:

if(-f $outfile) 

{ 

my $word = ""; 
$word=~s/("\S+|\S+|")\s*/$1/g; 
open my $word1,'<',$word or die "error"; 
while(<$word1>) 
{ 
    $word .= $_; 
} 
close $word1; 

# split each line from $word and store the lines in an array 
my @lines = split /\n/, $word; 

# the initial value is 0 
my $max_value = 0; 

# iterate over the lines 
for (my $i = 0; $i < scalar (@lines); $i++) 
{ 
    # if the current line matches the pattern tv1234* 
    if ($lines[$i] =~ /tv(\d*)/) 
    { 
     # $max_value will store the 1234* 
     $max_value = int($1); 
     # get the index of the next line 
     my $j = $i+1; 

     # if the next element matches the pattern tv1234* 
     if ($lines[$j] =~ /tv(\d*)/) 
     { 
      # check if the value if the value from the next line is 
      # lower than the max value 
      if (int($1) < $max_value) 
      { 
       # if it is remove the next line 
       splice(@lines, $j, 1); 
      } 
      else 
      { 
       # max value is the value of the next line 
       $max_value = int ($1); 
       # otherwise remove the current one 
       splice (@lines, $i, 1); 
       # since we delete the current index, decrement it to don't skip 
       $i--; 
      } 
     } 
     # we finished the block of tv*s so we reset the max value 
     else 
     { 
      $max_value = 0; 
     } 
    } 
} 

# recompute the initial $word with the filtered lines 
my $word = join ("\n", @lines); 
print $word; 

}

错误:

误差在./generate_dcms_html.pl线4条,

+0

'打开我的$ word1,'<',$ word或死“错误”;'从你正在阅读的地方看-_- –

+0

阅读$ word。@ JohnDoe –

+0

当你应该从'$ outfile'阅读时 –

1

这里是您的解决方案的代码。 只是一个建议,请尽量详细说明问题,让这个人可以很容易理解。

代码:

use strict; 

my @in = qw/DAC_Datapath JESD_RX Overall_DV RNM_model tv1 tv2 tv3 tv4 tv5 1 2 3 path RX Overall_DV M_model tv11 tv12 tv13 tv14 tv15 1 2 3/; 

print "Input is :\n" . join("\n", @in) . "\n\n" . '-'x40 . "\n\n"; 

my @out; 
my @tv; 
my $biggerTV; 

foreach my $data (@in) { 
    if($data =~ /^tv/i){ 
     push(@tv, $data);  
    } 
    else{ 
     if(@tv){ 
      # find bigger tv now 
      foreach my $tv (@tv){ 
       my $tvNum = $tv; $tvNum =~ s/tv//i; 
       my $biggerTVNum = $biggerTV; $biggerTVNum =~ s/tv//i; 
       $biggerTV=$tv if($tvNum > $biggerTVNum); 
       # print "$biggerTV\n"; 
      } 
      push(@out,$biggerTV); 
      @tv =(); # empty TV array 
      $biggerTV = ""; 
     } 
     push(@out,$data); 
    } 
} 

print "Output is :\n" . join("\n", @out); 

执行代码的输出:

Input is :

DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv1 
tv2 
tv3 
tv4 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv11 
tv12 
tv13 
tv14 
tv15 
1 
2 
3 

Output is :

DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv15 
1 
2 
3 
-1

试试这个

use warnings; 
use strict; 
chomp(my @ar = <DATA>); 
my $f = 0; 
my @dumy; 
my @vr; 
foreach (@ar) 
{ 
    (/(^\d+$|\D+$)/)?"@{[push(@vr,$_) and $f=0]}":push(@dumy,$_) and $f = 1; 
    if(($f == 0) && @dumy) 
    { 
     push(@vr,(sort{$b cmp $a} @dumy)[0]); 
     ($vr[-1],$vr[-2]) = ($vr[-2],$vr[-1]); 
     @dumy =(); 
    } 

} 

print join "\n",@vr,"\n"; 


__DATA__ 
DAC_Datapath 
JESD_RX 
Overall_DV 
RNM_model 
tv1 
tv2 
tv3 
tv4 
tv5 
1 
2 
3 
path 
RX 
Overall_DV 
M_model 
tv11 
tv12 
tv13 
tv14 
tv15 
1 
2 
3 
+0

用户可能(或不)观察到的Perl有点新鲜(甚至无法从文件中正确读取)。这足够接近代码混淆了。所以没有冒犯,但没有一些评论......你的代码并不真正有用。 –