in reply to Getting a result from "foreach" block
May the more enlightened members of the monastery correct me if I'm wrong, but foreach is a control construct. It does not return a value; it controls the execution of your code.
What you want is map, which takes a list, performs a operation on each element and returns the resulting list of processed elements:
#!/usr/bin/perl use strict; my @array = (1, 2, 3, 4, 5); my @results = map { $_ * 13 } @array; # @results is (13, 26, 39, 52, 65)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting a result from "foreach" block
by v_melnik (Scribe) on Jun 25, 2014 at 11:50 UTC | |
by AppleFritter (Vicar) on Jun 25, 2014 at 11:53 UTC |