in reply to hash array question
You can't assing two values to a single key.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' };
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';
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: hash array question
by Tony23 (Initiate) on Nov 11, 2019 at 12:27 UTC | |
by AnomalousMonk (Archbishop) on Nov 11, 2019 at 22:21 UTC | |
by choroba (Cardinal) on Nov 11, 2019 at 16:28 UTC |