my @array = array_creation_statement;
list_function @array;
####
list_function array_creation_statement;
####
#!/usr/bin/perl -w
use strict;
# The easy try. (OK, but it's 6 lines, not one statement)
#
my @for_ar;
for my $row (1..8) {
for my $col ('a'..'h') {
push @for_ar, $col.$row;
}
}
# It can be squeezed to two lines, but still not one (and bad style)
# my @for_ar;
# for my $row (1..8) { for ('a'..'h') {push @for_ar, $_.$row;}};
# The nested map (right items, wrong order)
# a1 a2 a3 ... instead of a1 b1 c1 ...
my @map_ar = map { map {$_} ($_.'1'..$_.'8') } ('a'..'h');
# the "brute force" try (right items, wrong order)
# a1 a2 a3 ...
my @grep_ar = grep { /[1-8]$/ } ('a1' .. 'h8');
# sorted brute force (OK, but boy! it's ugly and LONG)
#
my @grep_sort_ar = sort {substr($a,1,1) <=> substr($b,1,1) || $a cmp $b}
grep{/[1-8]$/}('a1'..'h8');
# the wrong "brute force" try (wrong items, and too many)
# (uncomment the next and the last comment to see the results)
#my @grep_map_ar = grep{ /[1-8]$/ } map{map {$_}('a'.$_..'h'.$_)}(1..8);
# *******************************************************************
# Finally, the nested map with a correction (OK!)
my @map2_ar = map {my $row = $_; map { $_.$row } ('a'..'h') } (1..8);
# printing the results
print @{[ join ",", @$_]}, "\n" for (
["for (OK)"], \@for_ar,
["map"], \@map_ar,
["grep"], \@grep_ar,
["sorted grep (OK)"], \@grep_sort_ar,
# ["wrong grep"], \@grep_map_ar,
["*map 2* (OK)"], \@map2_ar
);