in reply to Nested foreach

With a regular expression and a well placed map your nested loop can be reduced to a single line
## build a regular expression to match all the words in @ignore my $ignore = '(?:\b'. join('\b|\b', map quotemeta, @ignore). '\b)'; ## assuming @words contains the contents of the file s/$ignore// for map lc, @words;
Now it's just a matter of writing @words back to the original file. See. quotemeta, join, map and lc for info on the functions used in the above snippet, perlsyn for info on the for modifier and perlre for further details of the regex used.
HTH

_________
broquaint

Replies are listed 'Best First'.
•Re: Re: Nested foreach
by merlyn (Sage) on Apr 25, 2003 at 15:37 UTC
    my $ignore = '(?:\b'. join('\b|\b', map quotemeta, @ignore). '\b)';
    That's a lot of B's. (I was making a motorboat sound just thinking about that regex. {grin})

    Wouldn't this be simpler?

    my $ignore = '\b(?:' . join("|", map "\Q$_", @ignore) . ')\b';

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Wouldn't this be simpler?
      Er, yes, yes it would ;)

      mental note to self - refactor

      _________
      broquaint