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

Hello Monks, me again. I am sorry to be bothering you again! I am trying to compare a list of two indexes the first (var1) being 1000~ enteries long and the second(var2) ~70 enteries long. A simple task i thought. I wish (at this stage) just to print indexs occuring once in var1 and once in var2.
@var2=<INPUT>; @copyvar2=@var2; foreach $var1 (@var1){ print "OUTER LOOP OK $var1\n"; @copyvar2=@var2; foreach $var2 (@var2) { print "INNER LOOP OK $\n"; chomp $var1; # was: chomp $headers; chomp $var2; # was: chomp $matched; if ($var1=~/$var2/){ print "$var1\.\.$var2\n"; } else { } } }
The copy of @var (lines 2 and 5) is needed as before this was created the loop only ever ran through once, as the original array had been deleated. (ASIDE Is this a side effect due to scoping?i am using strict, and declare my variables at the start of the program ). The problem. The above code only ever returns the first matched value , not the list of matched values. I have checked via print statements (see code) that each value of var2 is compared to each value of var1. I have double checked the input data has correct format (ie no spaces/newlines were they should not be, no multiple occurences of any value in either set). I have used different methods of looking for a match.(eq, ==, /^var2$/). I have placed a different variable than the first var2 into the "if statement" line, this returns correct match from var1, so i know that match principle works. Apologies for the rant, i was just trying to make this problem clearer, many thanks in advance. PC

Code edit per author - dvergin 2003-08-11

Replies are listed 'Best First'.
Re: Compare two index lists
by princepawn (Parson) on Aug 11, 2003 at 16:58 UTC
    # ALWAYS use strict :) use strict my @var2 =<INPUT> for my $var2 (@var2) { for (@var1) { # how is @var1 set??? print "match: $var1, $var2\n" if ($var2 =~ /$_/); } }

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

      What happens if for instance the word "lala" exists 4 times in each? the OP made it sound like that should not be printed.

      -Waswas
        Luckily the nature of the input file means that this should never happen! But...if it did... ideally the index should only be matched once. PS lines 9 and 10 should read
        chomp $var1; chomp $var2;
        i changed it to ease readers through the problem
      Hi there, Thanks for the suggestion, but it seems to still only bring back the first hit? -------------------THIS WAS BECAUSE MY FILES HAD HIDDEN CHARACTERS --------------SEE LATER NODE I too ALWAYS use strict :-), and thank the day it was made, as it saves me from my bad spelling!cheers again pc
Re: Compare two index lists
by monktim (Friar) on Aug 11, 2003 at 17:19 UTC
    The data set seems small enough. You can store all the list data in a hash. Since the hash key is unique you won't hjave duplicate data when you get the keys. You can use the value field to store the number of times the data is found.
    use strict; use warnings; my @list_a = ( 'a'..'g'); my @list_b = ( 'A'..'C', 'a'..'c'); my %count; $count{$_}++ foreach (@list_a, @list_b); foreach (sort keys %count) { print "$_ appears $count{$_} time.\n" if $count{$_} == 1; }
      He used regular expressionn matching... so your example is fine if the hash is tied via Tie::Hash::Regex or Tie::Hash::Approx, but otherwise...

      Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

        I agree but it seemed like he was looking for any solution that worked. "I have used different methods of looking for a match.(eq, ==, /^var2$/)". Thanks for pointing it out.
Re: Compare two index lists
by husker (Chaplain) on Aug 11, 2003 at 19:42 UTC
    Sounds like a simple intersection to me. If so, the Set::Array module might fit your needs.
Re: Compare two index lists
by pdotcdot (Acolyte) on Aug 11, 2003 at 18:22 UTC
    Hi, i have just ran my input files throught 4 different editors than i used before looking for anything untoward.. and found hidden characters that the other editor did not "see". i have re-edited the files and my original program works, and i will be using the principle of the other programs in the future. Many thanks again to all who helped, prehaps next time i should triple check my files from now on (re first node(!). PC