在下面,我向您展示了使用perl正则表达式进行操作的部分代码。
我打算如何做的部分代码:
if(-f $outfile)
{
$word=~s/("\S+|\S+|")\s*/$1/g;
print $fh_out '<a href="' .$word.'/'.$word. '.html">'.$word.'</a>';
}
else
{
print "invalid";
}
只考虑$ word部分:
这里$ word包含以下内容:
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.At点它应该打印tv15.据你所知,其他行不应该影响做电视*
我想将$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 :(得分: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 :(得分: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
答案 2 :(得分: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行的错误,
答案 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