Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-09-25 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (202 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!