I have contrived a problem to help me learn, iterating through an array 1 ..1000, if an item is > 850, save to new array which is visible outside the foreach block. I often find myself wanting to do similar things in while or if blocks but wonder if I am approaching this in the wrong way. So it's more a question of Perl style in handling such scenarios
our @newa; my @a = 1..1000; foreach my $elem (@a) { if ($elem > 850) { push @newa, $elem; }; say "newa is: @newa\n"; } say "newa is:", @newa;
In reply to iterating through array and saving matches to new array by jazzfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |