The following line works perfectly
for(my $i=0; $i < ($max_size - $curr_size) ; $i++){
push (@{$_}, 0);
}
But this one doesn't.
push (@{$_}, 0) for (1 .. ($max_size - $curr_size));
It gives me an error message like this:
Can't use string ("1") as an ARRAY ref while "strict refs" in use at coordReadEasy.pl line 124, <DATA> line 16.
Why? how can I solve this?
The range version of for
sets $_
to each element, so in @{$_}
you’re trying to dereference $_
as though it were an array reference. These are all equivalent:
for my $x (1..10) {
print "$x\n"
}
for (1..10) {
print "$_\n"
}
print "$_\n" for (1..10);
The easy solution is to create another variable for your array reference:
push @{$ref}, 0 for 1 .. $max_size - $curr_size;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With