xref: /openbsd-src/gnu/usr.bin/perl/ext/B/t/f_map (revision 5b2d23599f1f4d2ccd1808cd130de4377ef9fa48)
1*5b2d2359Smillert#!perl
2*5b2d2359Smillert# examples shamelessly snatched from perldoc -f map
3*5b2d2359Smillert
4*5b2d2359Smillert# translates a list of numbers to the corresponding characters.
5*5b2d2359Smillert@chars = map(chr, @nums);
6*5b2d2359Smillert
7*5b2d2359Smillert%hash = map { getkey($_) => $_ } @array;
8*5b2d2359Smillert
9*5b2d2359Smillert{
10*5b2d2359Smillert    %hash = ();
11*5b2d2359Smillert    foreach $_ (@array) {
12*5b2d2359Smillert	$hash{getkey($_)} = $_;
13*5b2d2359Smillert    }
14*5b2d2359Smillert}
15*5b2d2359Smillert
16*5b2d2359Smillert#%hash = map {  "\L$_", 1  } @array;  # perl guesses EXPR.  wrong
17*5b2d2359Smillert%hash = map { +"\L$_", 1  } @array;  # perl guesses BLOCK. right
18*5b2d2359Smillert
19*5b2d2359Smillert%hash = map { ("\L$_", 1) } @array;  # this also works
20*5b2d2359Smillert
21*5b2d2359Smillert%hash = map {  lc($_), 1  } @array;  # as does this.
22*5b2d2359Smillert
23*5b2d2359Smillert%hash = map +( lc($_), 1 ), @array;  # this is EXPR and works!
24*5b2d2359Smillert
25*5b2d2359Smillert%hash = map  ( lc($_), 1 ), @array;  # evaluates to (1, @array)
26*5b2d2359Smillert
27*5b2d2359Smillert@hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
28*5b2d2359Smillert
29*5b2d2359Smillert
30