Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: hash array question

by choroba (Cardinal)
on Nov 08, 2019 at 16:56 UTC ( [id://11108482]=note: print w/replies, xml ) Need Help??


in reply to hash array question

The expected structure doesn't make any sense, have you tried to populate a hash by it and use Data::Dumper to visualise it?
Odd number of elements in hash assignment at ...line 29. $VAR1 = { 'HASH(0x563b633597c8)' => undef, 'RED' => { 'PRICE' => 0, 'CODE' => 'code value 1', 'LABEL' => 'RED' }, 'HASH(0x563b63359750)' => 'BLUE' };
You can't assing two values to a single key.

If you want to assign an array reference to each key, though, it's doable:

#!/usr/bin/perl use warnings; use strict; use Test::More; my %in = ( 1 => { PRICE => 0, CODE => 'code value 1', LABEL => 'RED' }, 2 => { PRICE => 0, CODE => 'code value 2', LABEL => 'RED' }, 3 => { PRICE => 1, CODE => 'code value 3', LABEL => 'BLUE' } ); my %out; push @{ $out{ $_->{LABEL} } }, $_ for values %in; is_deeply \%out, { RED => [ { PRICE => 0, CODE => 'code value 1', LABEL => 'RED' }, { PRICE => 0, CODE => 'code value 2', LABEL => 'RED',} ], BLUE => [ { PRICE => 1, CODE => 'code value 3', LABEL => 'BLUE', } ], }, 'same';
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: hash array question
by Tony23 (Initiate) on Nov 11, 2019 at 12:27 UTC
    Many thanks, this is what i'm looking for. Is it also possible to add a key to each sub array? Something like:
    RED => [ {KEYNAME =>[ PRICE => 0, CODE => 'code value 1', LABEL => 'RED'] }, { KEYNAME => [PRICE => 0, CODE => 'code value 2', LABEL => 'RED',}] ], BLUE => [KEYNAME => [ { PRICE => 1, CODE => 'code value 3', LABEL => 'BLUE', } ]]

      Also, an expression such as
          { KEYNAME => [ PRICE => 0, CODE => 'code value 1', LABEL => 'RED' ] }
      suggests you may be mistaking an array element for part of a hash key/value pair. The  => (fat arrow) operator is just a fancy comma (see Comma Operator), it does nothing | nothing in and of itself to form key/value pairs. The expression above is equivalent to
          { KEYNAME => [ 'PRICE', 0, 'CODE', 'code value 1', 'LABEL', 'RED' ] }
      (update: in which an anonymous array is initialized with a set of unrelated, i.e., unpaired, elements).


      Give a man a fish:  <%-{-{-{-<

      Sorry, I don't understand. After RED, you have two hashes in an array, but after BLUE, there's no hash, just an array. Also, are you sure you want to store the tuples in an array instead of a hash?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found