From your question, I am assuming you are an experienced Python programmer, but new to Perl.
If you're looking for something similar to Python list comprehensions in Perl,
start by mastering the basic Perl built-ins: foreach, map and grep.
As LanX has pointed out, map is the right fit for your specific example.
Effective Perl Programming has a nice item
"Use foreach, map and grep as appropriate",
which
provides a handy summary of when to use foreach, map and grep:
- Use foreach to iterate read-only over each element of a list
- Use map to create a list based on the contents of another list
- Use foreach to modify elements of a list
- Use grep to select elements in a list
If you are interested in functional style programming in Perl, once you've mastered these basics, move on to the Perl core List::Util module.
After that, CPAN List::MoreUtils.
Choosing "good" modules from CPAN is a bit of a dark art.
Remember, CPAN is a "commons" with a very low barrier of entry ... so it is full both of gems, and utter rubbish.
Before adding a dependency to some random third-party CPAN module,
I suggest you check some basic things,
to get a feel for how good the module is,
like the module rating, CPAN Testers results, change log, test suite, reputable author responsive to bug reports,
reported bug list, META.yml/json, pre-reqs, license, kwalitee, and so on, then
perhaps google for the module to get a feel for how widely used it is and what folks think of it.
For List::Comprehensions, we see:
- No reviews. That is, nobody has bothered to review or rate it.
- No change log. No license. No META.
- The module has not been updated since 2003. Probably abandoned.
- This is the only module written by this author.
- Test results ok, but test suite not comprehensive (just one tiny test.t file).
Contrast with, for example, List::MoreUtils:
- Seven reviews. Good rating score.
- Comprehensive change log.
- The module has been updated many times.
- This author has written over 100 CPAN modules.
- Test results ok, test suite has six .t files and a Test.pm.
See Also
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.