in reply to Comparing adjacent lines

What do you want to happen to the data after you've made these changes? Print it out to a file? Save it to a database? Push it onto an array created outside the loop? As it stands, you make changes to the @gtf array, and then it's clobbered when the loop restarts and splits the next line into it. Since you haven't localized it to the loop, the last line will remain in it after the loop exits, but I doubt that's what you want.

I'll go through your code and add some comments below, followed by my suggestions:

my $previous = ''; while (<>) { @gtf = split /\s+/, $_; chomp(); # switch those two to chomp the line before you split it, # otherwise your chomp is meaningless. Also, those are # the default arguments to split: while(<>){ chomp; my @gtf = split; if ("$gtf[9]" ne "$previous") { # don't put quotes around single variables; potentially buggy if( $gtf[9] ne $previous ){ $gtf[2] =~ tr/ex*/ex1/; # this probably doesn't do what you want. # tr/// transliterates characters: here e becomes e, # x becomes x, and a literal asterisk becomes 1. # If you want to reset $gtf[2] back to 'ex1', just do so: $gtf[2] = 'ex1'; } else { $gtf[2]++; # this may bite you too. $x='ex9'; $x++; $x=='ey0' # you probably want to increment just the number: $gtf[2] =~ s/(\d+)/$1+1/e; } # now save $gtf[9] in $previous so it can be compared to the next li +ne $previous = $gtf[9]; # here is where you would print out the array values, or do whatever + you like with them }

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^2: Comparing adjacent lines
by daccame (Initiate) on Jul 10, 2012 at 17:12 UTC

    As you could probably tell, I'm new to Perl, so thanks for being gentle :) Thanks for the comments, they were extremely helpful. After the loop I am creating a new file; this program is for changing file formats.

    One thing though: after ex# has been replaced with ex1, the subsequent ex# with matching @gtf[9] don't count up from ex1. They revert to the original ex# value+1 and count from there. I think that rather than taking the new ex1, its using the default ex# (like you said, changes made to @gtf are clobbered when the loop restarts). I guess I could specify ex1 as its own variable before the loop, like The Code Captain suggested...I'll test and see what happens.

      I've been trying out different things and just can't get it to work...any ideas?

        Lots of them, depending on what "doesn't work" means.

        Aaron B.
        Available for small or large Perl jobs; see my home node.