in reply to Compare two arrays

You should uncomment print Dumper %seen; and write \%seen instead of %seen: you are probably not getting what you expect in your %seen hash.

You may need to use captures in your regex (it depends on weither you want to use the whole name or just the number) in which case you'll find everything you need in Extracting matches (hint: it may be next unless $acc =~ /(^\d+)/; my $key = $1;/). Once your keys are what you expect them to be, in the filename loop you just have to check exists $seen{$key} to know if there is a match. See exists

Replies are listed 'Best First'.
Re^2: Compare two arrays
by Anonymous Monk on Oct 01, 2014 at 17:14 UTC
    Why can't this work?
    ... for my $filename (@file) { next unless $filename =~ /^\d+/; if(grep { %seen{$_} =~ $filename} keys %seen) { print " $filename\n"; } else { print "$filename is not in array\n"; } }

      %seen{$_} is incorrect, you're only accessing one element (ie: a scalar) so you have to use the sigil $: $seen{$_}

      Now, look at your dump of \%test, you'll see that the value in $seen{$_} is actually an array, not a string, unless you have changed your data structure since the first post.

      Now your grep test (if I ignore your previous errors), applied to 96329XY and 96329XY_K.txt would turn into : "96329XY" =~ /96329XY_K.txt/, you probably have the wrong order. And you probably want to use quotemeta: grep { $filename =~ /\Q$_/ } keys %seen.

        I tested like this:
        ... for my $filename (@file) { #next unless $filename =~ /^\d+/; #if(grep { $seen{$_} =~ $filename} keys %seen) { # Failed #if(grep { $filename =~ $seen{$_} } keys %seen) { # Failed if(grep { $filename =~ /\Q$_/ } keys %seen) { print " YES - $filename\n"; } else { print " NO - $filename\n"; } }
        Why would it only work using "quotemeta"? I even removed the
        "next unless $filename =~ /^\d+/;

        Thanks for you time!