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

Hi all, I need to match the contents of file against another.
file1: 1 2 3 file2: 55 1 65 2 76 3
if the contents of file1 match a column of data in file 2 printout to a new file the entire line whith mtached data. I have tried putting both files into an array and then use foreach to search, but having problems (naturally)
open (file, "<file1"); while (<file>) { @array=$_; } open (file2, "<file2"); open (out, ">out"); while (<file2>) { @array2=$_; ($one, $two) = split (/\s+/, $_); } foreach $file2 (@array2) { foreach $file1 (@array) { ###lost here
thanks

Replies are listed 'Best First'.
Re: matching an array
by dragonchild (Archbishop) on Jul 19, 2004 at 19:41 UTC
    • Is it going to match a specific column in the second file?
    • Does row #1 in file1 have to match row #1 in file2? What if row1 doesn't, but all other rows do?
    • Do you need to preserve the line for matches in file2? What I mean is if lines 1, 3, & 5 match, do you just have to say that or do you have to display the whole line for whom a column matched?

    You might know what you're trying to do, but you're not explaining it very well. The more detail, the better the help.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      just need to compare against column two in file2. Then print the whole line if column 2 mtaches file1.
        So, can line9 in file1 match the value on line3 in file2? What happens if the number of lines are different? You're not thinking of the various possibilities that a piece of code should really take into account.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: matching an array
by davorg (Chancellor) on Jul 19, 2004 at 20:07 UTC

    It's not very clear exactly what your program needs to do, but I think you want something a bit like this.

    open FILE1, 'file1' or die $!; my @keys = <FILE1>; chomp @keys; my %keys = map { $_ => 1 } @keys; close FILE1; open FILE2, 'file2' or die $!; open FILE3, '>file3' or die $!; # output file print FILE3 grep { exists $keys{(split)[1]} } <FILE2>; close FILE2; close FILE3;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: matching an array
by graff (Chancellor) on Jul 20, 2004 at 03:08 UTC
    I find myself having to do this sort of thing a lot with various pairs of files; usually the column delimiters are whitespace (like your case) and sometimes they are other things (colons, commas, or whatever); usually the "key" field is in the first column and sometimes it's elsewhere; usually I just need to see keys that make up the intersection (or union or difference) and sometimes I want to see the whole line from one file or the other or both.

    So I wrote a general purpose utility to handle all that, and I use it almost every day. I posted it here: cmpcol

    For your case, the command line would be:

    cmpcol -i -l2 file1 file2:2 > file3
    i.e.: show the intersection (-i), list full lines from the second file (-l2), compare the first column in file1 (default key field) against the second column in file2 (:2), redirect stdout to file3. HTH
Re: matching an array
by pbeckingham (Parson) on Jul 19, 2004 at 19:44 UTC

    Try using List:Util to compare lists.