in reply to Sorting By Column

Read the files line-by-line, split each pair of lines along whitespace, compare the relevant field, and output the lines if they differ:

#!/usr/bin/perl use feature qw/say/; use warnings; use strict; open my $file1, "<", "test1.txt" or die "Could not open first file: $! +\n"; open my $file2, "<", "test2.txt" or die "Could not open second file: $ +!\n"; while(my $line1 = <$file1>) { my $line2 = <$file2>; chomp ($line1, $line2); my ($key1, $num1, $str1) = split /\s+/, $line1; my ($key2, $num2, $str2) = split /\s+/, $line2; if($str1 ne $str2) { say $line1; say $line2; } } close $file1 or die "Could not close first file: $!\n"; close $file2 or die "Could not close second file: $!\n";

There's bound to be shorter, more idiomatic ways of achieving the same thing, but since your userpage indicates you're still new to Perl, you may find this the most instructive/useful.

On an unrelated side note, could you edit your post to use <code> tags for your sample data? See the following nodes for more information on formatting etc.:

Replies are listed 'Best First'.
Re^2: Sorting By Column
by dasgar (Priest) on Jul 10, 2014 at 19:02 UTC

    I could be wrong, but I think your while loop is going to have problems if the two specified files do not have the same number of lines. If the first file has less lines than the second file, your code won't fully process all lines in the second file. If the first file has more lines than the second file, your loop will try to read in more lines from the second file than what it has.

      You're right -- based on the sample data the OP shared, I assumed that both files would have the same number of lines. Thanks for pointing this out, I should have mentioned it explicitely.

        Thanks AppleFritter!

        The original file is combined such as what I'm trying to achieve so for every key 1 there is a key 2.

        This should work fine and I will work with it (or a variation thereof).

        The sample provided was just a snipping from the original file as an example of what I'm working with.

Re^2: Sorting By Column
by Roguehorse (Initiate) on Jul 10, 2014 at 22:21 UTC

    This worked PERFECTLY!

    Thanks a billion!

      *tips hat* You're welcome!

      BTW, I should also point out that this ignores the second field (the numbers) entirely, as with the sample data you supplied, there is never a situation where these don't match. Depending on whether this is also the case for your actual data, you may still want to adjust the above code.