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

hi, I have a little question ;-) I want to compare 2 array and print the same lines like :

__data_file___ DATA01,01,NAME02 DATA02,12,NAME05 DATA03,08,NAME10 <- __data_file2___ DATA11 11 NAME22 DATA03 08 NAME10 <- DATA02 12 NAME05

I have the following section of code :

open(FILE, $data_file) || die("Could not open file ($data_file)\n$!\n" +); open(FILE2, $data_file2) || die("Could not open file ($data_file2)\n$! +\n"); @raw_data=<FILE>; foreach $lun_mig (@raw_data) { chomp($lun_mig); @lun_mig_split=split(/\,/,$lun_mig); print "$lun_mig_split[0] $lun_mig_split[1] $lun_mig_split[2]\n" } @raw_data2=<FILE2>; foreach $lun_mig2 (@raw_data2) { chomp($lun_mig2); @lun_mig_split2=split(/\ /,$lun_mig2); print "$lun_mig_split2[0] $lun_mig_split2[1] $lun_mig_split2[2]\n" } close(FILE2); close(FILE);

The print command works well but I can't get both data's array (@lun_mig_split and @lun_mig_split2) outside the foreach ... and i can't compare with simple command like "if".

For your information, FILE2 is the result of a remote command (I do a ssh connection and it return the data_file2's array).

Can you help me to resolve my problem ... ?

Thanks a lot and sorry for my bad english ...

Replies are listed 'Best First'.
Re: Comparing arrays ...
by Corion (Patriarch) on May 27, 2010 at 09:54 UTC

    This is a FAQ. Typing perldoc -q difference or looking at perlfaq4, How do I compute the intersection of two arrays shows you how to do it for simple arrays. You haven't specified, but I guess want to use the third column in your files as key instead of using the whole line, so you will have to do some extra work there.

Re: Comparing arrays ...
by Marshall (Canon) on May 28, 2010 at 00:16 UTC
    This appears straightforward. Go through the first file and make a hash table with keys like you expect that lines in the 2nd file will look like (it appears that you know exactly to the character what they will be if it is a "match"). Go through each line in the 2nd file and print the match if you have seen this line data in file1 before. You didn't show it, but I don't see any reason why the NAME05 line wouldn't match like the NAME10 line does.
    #!/usr/bin/perl -w use strict; open(FILE, "data_file.txt") || die "Could not open file data_file"; open(FILE2, "data_file2.txt") || die "Could not open file data_file2"; my %file1; while (<FILE>) { chomp; tr/,/ /; $file1{$_}=1; } while (<FILE2>) { chomp; print "$_ *** matched\n" if $file1{$_}; } __END__ Prints: DATA03 08 NAME10 *** matched DATA02 12 NAME05 *** matched data_file1: DATA01,01,NAME02 DATA02,12,NAME05 DATA03,08,NAME10 data_file2: DATA11 11 NAME22 DATA03 08 NAME10 DATA02 12 NAME05 __END__ Prints: DATA03 08 NAME10 *** matched DATA02 12 NAME05 *** matched data_file1: DATA01,01,NAME02 DATA02,12,NAME05 DATA03,08,NAME10 data_file2: DATA11 11 NAME22 DATA03 08 NAME10 DATA02 12 NAME05
Re: Comparing arrays ... (/usr/bin)
by tye (Sage) on May 28, 2010 at 00:31 UTC
    ( \ sort -u < $file1; \ sed -e 's/,/ /g' < $file2 | sort -u \ ) | sort | uniq -d

    - tye        

      Hi !

      I learned much more thanks to all of you !

      I made a mistake in my first post ...

      __data_file___ DATA01,01,NAME02 DATA02,12,NAME05 DATA03,08,NAME10 <- __data_file2___ DATA11 11 NAME22 DATA22 08 NAME10 <- DATA02 12 NAME05
      I want to match only the second and the third row.

      For example :

      if (($lun_mig_split[1] == $lun_mig_split2[1]) && ($lun_mig_split[2] eq + $lun_mig_split2[2])) { Do something }

      But I can't get both array @lun_mig_split and @lun_mig_split2 to match and I don't know how I can do this ...

      If someone has an idea ;-)

      PS : I test with "difference" ("union/intersection") and it works but it match all the line and it's not what I want.