Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Comparing adjacent lines

by The Code Captain (Initiate)
on Jul 09, 2012 at 23:56 UTC ( #980785=note: print w/replies, xml ) Need Help??


in reply to Comparing adjacent lines

Hi There

I can see a few problems straight off. You compare to $previous but you never set $previous to anything except the empty string at the start. Probably not what you intended. Also you chomp() your input after you have tokenised it. Also probably not what you intended.

I would suggest something like this (untested):

my $previous=undef; my $ex=1; while(<>) { chomp(); @gtf=split(/\s+/, $_); if( ($#gtf>=9) && defined($previous) && ($gtf[9] eq $previous) ) { $ex+=1; } else { $ex=1; } $previous=($#gtf>=9)?$gtf[9]:undef; $gtf[2]="ex".$ex; }

You don't have to check the size of your input ($#gtf) if you're not concerned with it, but remember that perl's autovivification will create elements of an array for you which may not be want you want so I prefer to check the element is there before accessing it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980785]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-07-21 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?