in reply to Regex and array

hi,

if you tested negative for corion's explanation which is usually the case(people usually tend to open a file that does not exists), try this:

#!/usr/bin/perl use strict; my @array = qw(q e t u o); while(<DATA>){ chomp(my $r = $_); grep{print "$_\n" if ($r =~ /$_/g)}@array; } __DATA__ ff qksjnfji klksnsd jkksdjen rjkaa
i always use grep or map when going through arrays or hashes, try it, it's a lot of fun !

cheers

Replies are listed 'Best First'.
Re^2: Regex and array
by toolic (Bishop) on May 25, 2010 at 13:30 UTC
    grep{print "$_\n" if ($r =~ /$_/g)}@array;
    • There is no need for the //g modifier.
    • I would not advocate this usage of grep. You are ignoring its return value. I think a for loop would be more straightforward in this case:
    for (@array) { print "$_\n" if ($r =~ /$_/); }
Re^2: Regex and array
by Deus Ex (Scribe) on May 25, 2010 at 13:11 UTC

    Thanks baxy77bax for your suggestions.

    I applied it in the shape that you and toolic suggested, and it worked!

    Thanks again for your precious help!