in reply to compare lines within a file

What have you tried? It's hard to give guidance if we don't know where you are coming from and where your issues lie.

As your input file looks a lot like a variant on CSV (with spaces and newlines as delimiters), I would suggest using one of the modules for parsing these types of files off of . My usual choice is Text::CSV, but the search results for CSV will give you an idea of how many solutions are out there for this parsing problem. The above module will generate an array of arrays for you - let us know if you have difficulty dealing with Perl references.

Your code may end up looking something like this (adapted from the documentation):

#!/usr/bin/perl use strict; use warnings; use Text::CSV; my @result; my $csv = Text::CSV->new ( { sep_char => ' ' } ) # should set binary +attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<", "test.csv" or die "test.csv: $!"; while ( my $row = $csv->getline( $fh ) ) { if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0/\E}) { if (@result) { $result[1] = $row->[3]; } else { $result[0] = $row->[2]; } } } printf "%s\t%s\n", @result; $csv->eof or $csv->error_diag(); close $fh;

As you did not wrap your input text in <code> tags, it's possible the files were mangled during posting. For example, if your file is actually tab delimited, you would need to specify "\t" as your delimiter, not " ".

Update: Fixed typo in code

Replies are listed 'Best First'.
Re^2: compare lines within a file
by garyboyd (Acolyte) on Mar 10, 2011 at 08:32 UTC

    Thanks for helping out with this I really appreciate the input from you guys.

    I tried the above code but I get an error:

    Use of uninitialized value in printf at parse_result.txt.pl line 23, <$fh> line 45.

    The code I used was practically the same as yours except for changing the name of the input file to results.txt and changing the " " to "\t"

    #!/usr/bin/perl use strict; use warnings; use Text::CSV; my @result; my $csv = Text::CSV->new ( { sep_char => '\t ' } ) # should set binar +y +attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<", "result.txt" or die "result.txt: $!"; while ( my $row = $csv->getline( $fh ) ) { if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0//E}) { if (@result) { $result[1] = $row->[3]; } else { $result[0] = $row->[2]; } } } #printf "%s\t%s\n", @result; printf @result; $csv->eof or $csv->error_diag(); close $fh;

      oops, shouldn't have included the printf @result; in the code, but it still doesn't work with the

      #printf "%s\t%s\n", @result;

      uncommented........

        Define "doesn't work". Please provide your input file (wrapped in <code> tags) as well as your observed and expected outputs.

        One problem is that you have written '\t' in place of "\t". These mean different things in Perl. Double quotes interpolate, whereas single quotes do not. This means '\t' yields the literal string of a backslash followed by a t; "\t" yields a tab character. There is a list of escape sequences in the link I have provided.