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

Replies are listed 'Best First'.
Re: Array culling - simple question
by Biker (Priest) on Mar 09, 2002 at 10:29 UTC

    the letter "R."

    I see two letters in there. This should work for "the letter 'R'" as a single letter. You should be able to modify it for the double letter search if that's what you really need.

    @r_words = grep /^R/,@words;



    Everything will go worng!

Re: Array culling - simple question
by simon.proctor (Vicar) on Mar 09, 2002 at 10:28 UTC
    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
      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'