perldoc > SERIES(1)

📂 Found in /usr/share/perl/5.34/pod/perlfaq4.pod

📛 NAME

perlfaq4 — How to perform an operation on a series of integers

🚀 Quick Reference

Use CaseCommandDescription
Apply function to each array element, collect results my @results = map { my_func($_) } @array; Map over array
Apply function to each array element, ignore results foreach my $iterator (@array) { some_func($iterator); } Loop without collecting
Apply function to each integer in a small range, collect results my @results = map { some_func($_) } (5 .. 25); Map over range (creates list)
Apply function to each integer in a large range without memory blowup push(@results, some_func($_)) for 5 .. 500_005; For loop over range (optimised, no intermediate list)

🤔 How do I perform an operation on a series of integers?

🔄 To call a function on each element in an array, and collect the results, use:

my @results = map { my_func($_) } @array;

For example:

my @triple = map { 3 * $_ } @single;

🔕 To call a function on each element of an array, but ignore the results:

foreach my $iterator (@array) {
    some_func($iterator);
}

🔢 To call a function on each integer in a (small) range, you can use:

my @results = map { some_func($_) } (5 .. 25);

⚠️ but you should be aware that in this form, the .. operator creates a list of all integers in the range, which can take a lot of memory for large ranges. However, the problem does not occur when using .. within a for loop, because in that case the range operator is optimized to iterate over the range, without creating the entire list. So

my @results = ();
for my $i (5 .. 500_005) {
    push(@results, some_func($i));
}

or even

push(@results, some_func($_)) for 5 .. 500_005;

💡 will not create an intermediate list of 500,000 integers. Using a for loop with a range avoids constructing the entire list, making it safe for huge ranges.

SERIES(1)
📛 NAME 🚀 Quick Reference 🤔 How do I perform an operation on a series of integers?

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-22 01:16 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format