Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have two txt files, i need to compare a particular column (column 1) of numbers between each file and return the unique numbers.
file1 6.66 6.75 6.75 6.66 6.75 6.75 7.17 7.17 7.31 7.31 7.41 7.41 file2 6.75 6.66 6.75 6.75 7.17 7.17 7.31 7.31 7.41 7.41
So here 6.66 would be returned? Thanks

Replies are listed 'Best First'.
Re: comparing two arrays
by GrandFather (Saint) on Oct 20, 2005 at 10:14 UTC

    Good on ya. What have you tried though and were does it go wrong? I'd guess you might have tried using a hash and counted the number of entries then printed out the ones that were unique (have a count of 1), but it's not clear where you are having trouble.


    Perl is Huffman encoded by design.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: comparing two arrays
by blazar (Canon) on Oct 20, 2005 at 10:21 UTC

    And the question is?

    I hope it's not
    So here 6.66 would be returned?

    But you're not asking us to do your homework, are you? If not, as we all hope, what have you tried thus far? Which is your code?

    Said this, it's not entirely clear to me whether you want uniq numbers per file or amongst all files. Whatever, you may have guessed that this kind of questions gets asked quite frequently, and indeed there's at least a faq that should be of considerable interest to you: check perldoc -q unique.

    Also, you may have done a search here (or a Super Search); in particular a question with a subject nearly identical to yours has been asked quite recently.

Re: comparing two arrays
by borisz (Canon) on Oct 20, 2005 at 10:34 UTC
    use Fatal qw/open/; my %h; open my $fh, '<', '/tmp/file1'; while ( defined ($_ = <$fh>)){ /^([\d.]+)/ and $h{$1}++; } open $fh, '<', '/tmp/file2'; while ( defined ($_ = <$fh>)){ /^([\d.]+)/ and delete $h{$1}; } print join "\n", keys %h;
    Boris

      Consider:

      file1 6.66 6.75 6.75 6.66 6.75 6.75 7.17 7.17 7.31 7.31 7.41 7.41 file2 6.75 6.66 6.75 6.75 7.17 7.17 7.31 7.31 7.41 7.41 6.62 6.75

      which should generate:

      6.62 6.66

      Perl is Huffman encoded by design.
        Upps, you are so right.
        use Fatal qw/open/; my ( %h, %g ); open my $fh, '<', '/tmp/file1'; while ( defined ($_ = <$fh>)){ /^([\d\.]+)/ and $h{$1}++; } open $fh, '<', '/tmp/file2'; while ( defined ($_ = <$fh>)){ /^([\d\.]+)/ and $g{$1}++; } my @k = keys %h; delete @h{keys %g}; delete @g{@k}; print join "\n",( keys %h, keys %g );
        Boris
      thanks for getting me started