Is it safe in Perl6 (as opposed to perl5 or other languages) to loop through an array while modifying it? For instance, if I have an array of websites to download, and I add failed downloads to the end of the array to re-download, will perl6 behave as expected? (I have about 50k links to download and trying to test all out would be time consuming.)
If not safe, what is a general approach? I have been thinking of storing links of interrupted downloads in another array, and loop through that array after original array was done. However, this is like a fox chasing its tail because I have to store failed downloads in another array (or overwrite the original array).
For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.
If we want to loop through an array, we can use the length property to specify that the loop should continue until we reach the last element of our array. In the loop above, we first initialized the index number so that it begins with 0 .
It is definitely safe in a single-threaded environment:
my @a = ^5;
for @a {
@a.push: $_ + 10 if $_ < 30
}
say @a
[1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34]
In a multi-threaded environment (which is what is better be used in your task) nothing can be taken for granted. Therefore appending of a new element to the array is better be wrapped into a Lock
:
my @a = ^5;
my Lock $l .= new;
for @a {
start {
... # Do your work here
$l.protect: {
@a.push: $_ with $site
}
}
}
say @a
Note that the last sample won't work as expected because all start
ed threads must be await
ed somewhere within the loop. Consider it just as a basic demo.
Yet, locking is usually be avoided whenever possible. A better solution would use Channel
and react/whenever
blocks.
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