in reply to [Perl 5.26][Linux LEAP] Array/List/Hash misunderstanding
Hello!
I'm assuming that you call 'element' a value of $hash{$key} (because in a canonical way of speaking 'element' is a pair of key and value in a hash).
1. Detect the element is an ARRAY... in perl
foreach my $element (values %hash) { if (ref $element eq ref []) { } # OR if (ref $element eq 'ARRAY') { } }
2. Acknowledge the number of elements in each hash
my $number_of_elements = scalar keys %hash;
3. Avoid having the two ARRAY references within a hash element
There can be only one reference in a hash value. Did you mean "avoid pushing ARRAY into hash element, if it's already an ARRAY ref"?
my %hash; foreach my $pair (qw/1_one 1_two 2_one 2_two/) { my ($key, $val) = split('_', $pair); if (not exists $hash{$key}) { $hash{$key} = []; } push @{ $hash{$key} }, $val; }
|
|---|