Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980785]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-12-16 03:13 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (70 votes). Check out past polls.