gmax has asked for the wisdom of the Perl Monks concerning the following question:
+-----------------------+
8 |a8 b8 c8 d8 e8 f8 g8 h8|
7 |a7 b7 c7 d7 e7 f7 g7 h7|
6 |a6 b6 c6 d6 e6 f6 g6 h6|
5 |a5 b5 c5 d5 e5 f5 g5 h5|
4 |a4 b4 c4 d4 e4 f4 g4 h4|
3 |a3 b3 c3 d3 e3 f3 g3 h3|
2 |a2 b2 c2 d2 e2 f2 g2 h2| ^
1 |a1 b1 c1 d1 e1 f1 g1 h1| ^
+-----------------------+ ^
a b c d e f g h |
- ->-->-->-->-->-->-->-->---^
And this is the reading direction
i.e. a1 - h1, a2 - h2, etc.
Therefore I need an array holding items likeormy @array = array_creation_statement; list_function @array;
list_function array_creation_statement;
Is there any Golfer who can get the right sequence with one shorter (or perhaps more meaningful) assignment?#!/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 );
my @array = something_magic;Thanks
_ _ _ _ (_|| | |(_|>< _|
|
|---|