The next Perl snippet should print the first 5 elements of the array referenced by the hash value, or less if the array is shorter.
while ( my ($key,$value) = each %groups ) { print "$key: \n"; my @list = grep defined, @{$value}; my @slice = grep defined, @list[0..4]; foreach my $item ( @slice ) { print " $item \n"; } print " (", scalar @slice, " of ", scalar @list, ")\n"; }
I do not think that the first grep defined necessary, but it cannot do much harm, and it must ensure that there are no undefined array elements before the slice. The second grep defined should remove the elements of the undefined array as a result of slice when @list shorter than 5.
%groups was populated with repeated calls:
$groups{$key} = () unless defined $groups{$key}; push @{$groups{$key}}, $value;
In most cases, it works fine:
key1: value1 value2 value3 value4 value5 (5 of 100)
But sometimes - and I did not work out under what circumstances - I see:
key2: value1 (1 of 5) key3: value1 value2 (2 of 5)
I expect the length of the printed list and x from (x of y) will be min(5,y)
What can cause this behavior?
source share