Perl Grep and Map

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


23 points

All,

I have an array whereby each element in that array is of the following format:

@myNames = ('a b', 'c d', 'e f', 'g h', 'i j');

However, I am only interested in grepping the second part of each element - i.e. b, d, f, h and j.

How do I grep for second part? Would Map be helpful?

I get the entire element returned and not the section of that element that I am after.



-6 points

Grep is a boolean test. Does it match? If so, we want it.

If you are modifying the data, map works better.

my @lastNames = map {(split / /)[1]} @myNames;

Anonymous's picture
Created by Anonymous
0 points

This is useful. Try already mentioned Perl Grep function.

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.