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

Okay... it's a homework question, but a good one. How can I re-write this program to use $_ instead of $word, without actually using the magic variable "$_" explicitly ?
foreach my $word (split /\s+/, $line) { $word =~ s/microsoft/monopoly/gi; print $word if $word =~ /yes/; }
Thanks!

2002-05-09 Edit by Corion : Added CODE tags

Replies are listed 'Best First'.
(jeffa) Re: No explicit $_ ?
by jeffa (Bishop) on May 09, 2002 at 14:19 UTC
    foreach (split /\s+/, $line) { s/microsoft/monopoly/gi; print if /yes/; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: No explicit $_ ?
by Chady (Priest) on May 09, 2002 at 15:30 UTC

    lately, I'm becoming addicted.

    print grep { /yes/; } map { s/microsoft/monopoly/gi; } split /\s+/, $line;

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      One more...
      print grep{/yes/&&s/microsoft/monopoly/gi}(split /\s+/, $line);


      Hopes
      $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print
Re: No explicit $_ ?
by hotshot (Prior) on May 09, 2002 at 14:22 UTC
    here's your answer:
    for (split(/\s+/, $line)) { s/microsoft/monopoly/gi; print if /yes/; }
    and please format your question befor submitting it.

    Thanks.

    Hotshot