So, I never knew this and I want to get some clarifcation on it. I know if you do
foreach (@list){
if you change $_ in that loop it will affect the actual data. But, I did not know that if you did
foreach my $var1 (@list){
If you changed $var1 in the loop it would change the actual data. :-/ So, is there a way to loop over @list but keep the variable a read-only copy, or a copy that if changed will not change the value in @list?
A foreach loop is used to iterate over a list and the variable holds the value of the elements of the list one at a time. It is majorly used when we have a set of data in a list and we want to iterate over the elements of the list instead of iterating over its range.
The Perl last statement is used inside a loop to exit the loop immediately. The last statement is like the break statement in other languages such as C/C++, Java.
Make a copy of @list in the for statement:
foreach my $var1 (() = @list) {
# modify $var without modifying @list here
}
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