This is similar to already proposed solutions except that it uses maps rather than loops. I'm only posting it because others might be interested in the way you can pass a subroutine reference to Data::Dumper->Sortkeys() to control the key sort order; I'd not seen this before but went looking in the docs for a solution when I realised that magic-incrementing the key beyond "z" would make a mess of the default ->Sortkeys() output.

use 5.026; use warnings; use Data::Dumper; my $numX = 5; my $numY = 7; my $key = q{a}; my %posns = map { my $y = $_; map { my $x = $_; $key ++ => [ $x, $y ] } 0 .. $numX - 1 ; } 0 .. $numY - 1; my $rcKeySorter = sub { my $rhPosns = shift; my @sortOrder = map { unpack q{x4a*}, $_ } sort map { pack q{Na*}, length, $_ } keys %{ $rhPosns }; return \ @sortOrder; }; print Data::Dumper ->new( [ \ %posns ], [ qw{ *posns } ] ) ->Sortkeys( $rcKeySorter ) ->Dumpxs();

The output.

%posns = ( 'a' => [ 0, 0 ], 'b' => [ 1, 0 ], 'c' => [ 2, 0 ], 'd' => [ 3, 0 ], 'e' => [ 4, 0 ], 'f' => [ 0, 1 ], 'g' => [ 1, 1 ], 'h' => [ 2, 1 ], 'i' => [ 3, 1 ], 'j' => [ 4, 1 ], 'k' => [ 0, 2 ], 'l' => [ 1, 2 ], 'm' => [ 2, 2 ], 'n' => [ 3, 2 ], 'o' => [ 4, 2 ], 'p' => [ 0, 3 ], 'q' => [ 1, 3 ], 'r' => [ 2, 3 ], 's' => [ 3, 3 ], 't' => [ 4, 3 ], 'u' => [ 0, 4 ], 'v' => [ 1, 4 ], 'w' => [ 2, 4 ], 'x' => [ 3, 4 ], 'y' => [ 4, 4 ], 'z' => [ 0, 5 ], 'aa' => [ 1, 5 ], 'ab' => [ 2, 5 ], 'ac' => [ 3, 5 ], 'ad' => [ 4, 5 ], 'ae' => [ 0, 6 ], 'af' => [ 1, 6 ], 'ag' => [ 2, 6 ], 'ah' => [ 3, 6 ], 'ai' => [ 4, 6 ] );

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: Make a hash by a range of keys and values by johngg
in thread Make a hash by a range of keys and values by GHMON

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.