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

    Wow, great, thank you!

    Such a shame I have never used map() before!

    V.Melnik

      You're welcome! Keep map and grep in mind, they're among the most useful functions you'll ever see for list processing.

      For more useful list-related functions, see e.g. List::Util. (In particular, this contains a reduce function that sadly does not exist as a builtin.)