v_melnik has asked for the wisdom of the Perl Monks concerning the following question:
Dear colleagues,
I tried to google the answer, but haven't successed, so I'll be grateful for your help.
How can I get some list of results of some "foreach" loop? Just something like that:
my @results = foreach (@data) { $_ = $_ * 13; # some command to add the element to the resulting list # kind of push(), but how to get the reference # to the resulting array? }
Right now I'm using the following construction that I don't like:
@results = eval { my @r; foreach (@data) { $_ = $_ * 13; push(@r); } return(@r); }
Of course, I'm not too happy with that. How does the best way look like?
Lots of thanks to all for hints, tips and ideas!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting a result from "foreach" block
by AppleFritter (Vicar) on Jun 25, 2014 at 11:47 UTC | |
by v_melnik (Scribe) on Jun 25, 2014 at 11:50 UTC | |
by AppleFritter (Vicar) on Jun 25, 2014 at 11:53 UTC | |
|
Re: Getting a result from "foreach" block
by CountZero (Bishop) on Jun 25, 2014 at 14:28 UTC | |
by AnomalousMonk (Archbishop) on Jun 25, 2014 at 18:51 UTC | |
by RonW (Parson) on Jun 26, 2014 at 17:18 UTC | |
by AnomalousMonk (Archbishop) on Jun 26, 2014 at 18:31 UTC | |
|
Re: Getting a result from "foreach" block
by toolic (Bishop) on Jun 25, 2014 at 12:28 UTC | |
|
Re: Getting a result from "foreach" block
by Anonymous Monk on Jun 25, 2014 at 12:02 UTC |