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