I need to fill some array with data and dump it to the screen. Here is the code:
my @a;
@a[0] = 69;
foreach $sibling ($someotherlist){
my $cha = $sibling->{"id"} * 1;
if (defined @a[$cha]){
@a[$cha] = 0;
}
@a[$cha] = @a[$cha] + 1;
}
print Dumper(@a);
The problem is that it only prints $VAR1 = 69;
It should be something like:
$VAR1 = {
0 => 69,
1 => 30,
20 => 90
}
use strict; use warnings;. You will then get at least one warning ofScalar value @a[0] better written as $a[0]* 1is rather uselessif (defined $a[$cha]) { $a[$cha] = 0; }can be simplified to$a[$cha] //= 0;. If you don't want a dependency on Perl 5.10+, you can use$a[$cha] ||= 0;in this case.$a[$cha] = $a[$cha] + 1;can be simplified to++$a[$cha].