No question here! Just want to show some code.
$systems = { 'xai61001' => { # ref to hash ip => '192.168.1.1' , name => 'xai61001', model => '8205_E6C', }, 'xai61002' => { # ref to hash name => 'xai61002', model => '9119_MME', ip => '192.168.1.2' , }, }; $merge = { 'xai61001' => { # ref to hash description => 'Dit is een test voor xai61001', ibm_description => 'IBM system power 8 xai61001', }, 'xai61002' => { # ref to hash description => 'Dit is een test voor xai61002', ibm_description => 'IBM system power 8 xai61002', }, }; #Most examples I've found on the internet are speaking about "normal" +hashes!! #Wanted to be able to process a scalar pointing to a hash. # this is perl5 , Not perl6. Yes still struggling with it's syntax!! ##################################################################### # sliceAndDice a ref to a hash, in the end you can determine the order + in which a hash is displayed!! # I created 2 arrays which have the same order. Could be expensive whe +n processing a large hash_ref. # $sys = 'xai61002'; # entrie of the $systems HASH! print "$systems->{$sys}\n"; # HASH(0xnumber} print "$systems->{$sys}{ip}\n"; # ip address @keys = ( qw/ip name model/ ); # hardcoded the order of the keys!! print keys %{$systems->{$sys}},"\n"; # the normal way to get at the k +eys @values = @{$systems->{$sys}}{@keys} ; # getting the at the values, i +n the order of @keys # the above is processing a hash ( looks like an A +RRAY, but is NOT ) #print @keys, @values,"\n"; print "\n\nOriginal hash_ref\n"; for $idx ( 0 .. $#keys ) { print "$keys[$idx] => $values[$idx]\n"; } # adding some keys and values, please notice : This is adding to a has +h!! (which is a scalar reference) # the keys in $systems and $merge must be THE SAME $systems->{$key} $m +erge->{$key} !!, but the reference keys MUST be different # otherwise the merge will overwrite an existing key!! @merge = ( qw/description ibm_description/ ); @{$systems->{$sys}}{ @merge } = ( $merge->{$sys}{$merge[0]} , $merge-> +{$sys}{$merge[1]}); @keys = ( qw/description ip name model ibm_description/ ); @values = @{$systems->{$sys}}{@keys} ; #print @keys, @values,"\n"; print "\n\nAdded some keys and values: showing all items\n"; for $idx ( 0 .. $#keys ) { print "$keys[$idx] => $values[$idx]\n"; }

hope it's usefull to somebody!!

Replies are listed 'Best First'.
Re: Slice and dice a ref to hash
by kcott (Archbishop) on Dec 07, 2016 at 23:00 UTC

    G'day teun-arno,

    You started your post with:

    "No question here! Just want to show some code."

    You posted it in "Seekers of Perl Wisdom", which is inappropriate when you don't have a question. The first sentence on that page is:

    "If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask."

    I've moved it to Cool Uses for Perl". The first sentence on that page is:

    "This section is the place to post your general code offerings -- everything from one-liners to full-blown frameworks and apps."

    You don't need to do anything else in respect of that change beyond understanding why it was made and being more careful in future.

    There's another problem: you haven't posted your code between '<code>...</code>' tags. You have multiple instances of code that (presumably) should have looked like '$array[$index]', but renders as '$array$index'. Please fix this.

    [See also: "Writeup Formatting Tips" and "What shortcuts can I use for linking to other information?"]

    — Ken

Re: Slice and dice a ref to hash
by teun-arno (Acolyte) on Dec 07, 2016 at 22:07 UTC
    The $keys$idx should be :
    print "$keys[$idx] => $values[$idx]\n";
    Sorry!