Dear fellow Monks,
Here is the matrix I am building.
It is a chessboard, i.e. a 64 items array
   +-----------------------+
 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 like
a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 c2 d2 e2 f2 g2 h2 a3 b3 ... and so on.
For my application, I need to create the array and assign the values in one statement only, or create an anonimous array, which is conceptually the same.
In short, I need to do either
my @array = array_creation_statement; list_function @array;
or
list_function array_creation_statement;
This is what I have tried so far.
#!/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 );
Is there any Golfer who can get the right sequence with one shorter (or perhaps more meaningful) assignment?
my @array = something_magic;
Thanks
 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Filling an array. (Golf challenge?) by gmax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.