in reply to [untitled node, ID 150523]

theres bound to be loads of different ways to do this, map and foreach spring to mind:
use strict; use warnings; my @words = qw(Richard Rolled All Over Rondas Red Carpet); my @r_words; print "Example 1:\n\n"; foreach (@words) { if(/^R/) { push @r_words,$_; } } foreach (@r_words) { print $_,"\n"; } @r_words = ();
Update: Ok this is the scoundrels way of doing it :)

In honesty I wouldn't have done it this way anyway but a fair point from rob_au. So this is how not to do it :)
print "Example 2:\n\n"; map { push @r_words, $_ if /^R/ }@words; foreach (@r_words) { print $_,"\n"; }
HTH

Replies are listed 'Best First'.
Re: Re: Array culling - simple question
by rob_au (Abbot) on Mar 09, 2002 at 10:48 UTC
    Its generally considered bad form to make use of the map BLOCK, LIST function in this manner - This is generally because, the real power of this function lies in the return of a list, each element having been evaluated by your BLOCK or expression.

    This has even been addressed by faq_monk at What's wrong with using grep or map in a void context? :-)

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'