in reply to Check If 2 Arrays Match

Thank you for all of the responses, this did the trick:
chomp (@ID); foreach $ID(@ID){ foreach $file(@files){ print "$file" if ($file =~ /($ID)/); } }

Replies are listed 'Best First'.
Re^2: Check If 2 Arrays Match
by ELISHEVA (Prior) on Jul 21, 2009 at 08:52 UTC

    Unless your ids are always exactly the same length, you might want to replace /($ID)/ with /^$ID$/. Otherwise "123" will match all of the following files: "123", "1234", and "4123".

    I also omitted the parentheses around $ID. "(...)" makes Perl do extra work to "capture" whatever is between the parenthesis. Since you already have the value of $ID you don't need to capture it.

    Another potential bug. If your id contains fullstops, i.e. '.' or other special regular expresssion characters you should probably escape the id, i.e. /^\Q$ID\E$/. The reason is that Perl will read the dot as a wildcard, so "1.2" will match "1.2", "122", "152", and so on.

    Best, beth

Re^2: Check If 2 Arrays Match
by gulden (Monk) on Jul 21, 2009 at 11:04 UTC
    One more:
    foreach my $id(@ID){ print "$id\n" if grep(/^$id$/, @files); }

    «A contentious debate is always associated with a lack of valid arguments.»