jreades has asked for the wisdom of the Perl Monks concerning the following question:
While understanding what map{} is supposed to do, I seem to be consistently unable to use it correctly... which suggests that, on some level I really don't know what it actually does.
As I understand it, map{} applies some function (defined within the BLOCK) to each element of an array and the result is passed to the left.
So, you could, for instance, write a for loop without using a for simply by writing:
my @array = (1, 2, 3, 4, 5); my @adjusted_array = map { $_ * 10 } @array; print (join ',', @adjusted_array);
But what about more complicated statements?
I make it about as far as loading a hash from an array that I've split before it all starts to look like a monkey hitting the keys largely at random.
my @array = ('one-1', 'two-2', 'three-3', 'four-4', 'five-5'); my %hash = map { split /-/ } @array; print (join ',', keys %hash);
Why, for instance, could I not work this through even though it's clearly a mapping situation?
if ($access) { foreach (@{$access}) { $set{$_->[0]} = $_->[1]; } }
I guess what I'm seeking is the wisdom to understand how map operates within the BLOCK since most mentions of the map{} function are either cryptic or basic and I fall somewhere in between.
TIA
|
---|