GHMON has asked for the wisdom of the Perl Monks concerning the following question:

Hi

i want to fill %positon by range of keys and values ,like this !!!!! that make keys in range of @ks and any keys have 2 values in range of $xpoints and $ypoints !!!!!

use warnings ; use strict ; print 'Last X Point' , "\n" ; my $xpoints = chomp (<STDIN>) ; print 'Last Y Point' , "\n" ; my $ypoints = chomp (<STDIN>) ; my @xp = ("0" .. $xpoints) ; my @yp = ("0" .. $ypoints) ; my @ks = ("1" .. ((xpoints*$ypoints))) ; my %position = ( 'a' => [0,0] , 'b' => [1,0] , 'c' => [2,0] , 'd' => [3,0] , 'e' => [0,1] , 'f' => [1,1] , 'g' => [2,1] , 'h' => [3,1] , 'i' => [0,2] , 'k' => [1,2] , 'l' => [2,2] , 'm' => [3,2] , 'n' => [0,3] , 'o' => [1,3] , 'p' => [2,3] , 'q' => [3,3] , ) ;

Replies are listed 'Best First'.
Re: Make a hash by a range of keys and values
by choroba (Cardinal) on Oct 31, 2018 at 21:37 UTC
    Several issues:
    1. chomp doesn't return the modified string, it changes the argument in place.
    2. xpoints needs a sigil.
    3. Spaces aren't needed before semicolons.
    4. Numbers don't need double quotes.
    5. The actual number of elements is ($xpoints + 1) * ($ypoints + 1) - 1 because you start at 0, 0.

    #! /usr/bin/perl use warnings ; use strict ; print 'Last X Point' , "\n"; chomp( my $xpoints = <STDIN> ); print 'Last Y Point' , "\n"; chomp( my $ypoints = <STDIN> ); my %position; my $key = 'a'; for my $y (0 .. $ypoints) { for my $x (0 .. $xpoints) { $position{$key++} = [$x, $y]; } } for my $key ('a' .. $key) { print "'$key' => [$position{$key}[0],$position{$key}[1]],\n" if $position{$key}; }
    Note that the output is different to yours which is missing "j":
    'a' => [0,0], 'b' => [1,0], 'c' => [2,0], 'd' => [3,0], 'e' => [0,1], 'f' => [1,1], 'g' => [2,1], 'h' => [3,1], 'i' => [0,2], 'j' => [1,2], 'k' => [2,2], 'l' => [3,2], 'm' => [0,3], 'n' => [1,3], 'o' => [2,3], 'p' => [3,3],

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      TNX BRO :)))))

Re: Make a hash by a range of keys and values
by poj (Abbot) on Oct 31, 2018 at 21:26 UTC
    #!perl use warnings; use strict; print 'Last X Point > '; chomp(my $xpoints = <STDIN>); print 'Last Y Point > '; chomp(my $ypoints = <STDIN>); #print "X=$xpoints Y=$ypoints\n"; my %position = (); my $key = 'a'; for my $y (0..$ypoints){ for my $x (0..$xpoints){ $position{$key++} = [$x,$y]; } } use Data::Dump 'pp'; pp \%position;
    poj

      TNX BRO :))))))

Re: Make a hash by a range of keys and values
by localshop (Monk) on Oct 31, 2018 at 21:37 UTC
    #!/usr/bin/env perl use Data::Dumper; my $lastx = 4; my $lasty = 5; my $points = []; for ( my $i1=0; $i1<=$lastx; $i1++ ) { for ( my $i2=0; $i2<=$lasty; $i2++) { push @$points, [$i2, $i1] ; } } my $position = {}; foreach my $letter ( ('a'.. 'z', 'A' .. 'Z' )[0.. scalar(@$points)-1] + ) { $position->{$letter} = pop(@$points); } foreach my $letter ( sort { "\L$a" cmp "\L$b" } keys %$position ) { say "$letter = [ $position->{$letter}[0], $position->{$letter}[1] +]"; }

    bugger - just saw that poj beat me and with a much neater solution .. is cool that you can just inc a letter and it will expand from z -> aa .. didn't know that.

Re: Make a hash by a range of keys and values
by johngg (Canon) on Nov 01, 2018 at 00:27 UTC

    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.

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Make a hash by a range of keys and values
by roboticus (Chancellor) on Oct 31, 2018 at 21:28 UTC

    GHMON:

    OK, but what are you having difficulty with? I also don't see the relationship between %position and the array @ks. What are you trying to do?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.