johngg explained with code, let me try with words. You are correct that @indexes doesn't change. After the splice, your @arr has been reduced to the elements ('one','three','four','five'); I hope how splice works is clear? Then, @arr[@indexes] is attempting to access array indices 0 through 4. That works fine for the indices 0 through 3, which returns all of the remaining elements of @arr. Index 4 no longer exists in @arr, so instead, undef is returned (which is the normal behavior when accessing an array index that doesn't exist) - in other words, @arr[0..4] evaluates to ('one','three','four','five',undef). When you print those values, undef will evaluate to the empty string, and if warnings are enabled, issue a warning. This is why the suggestions from the AM post are good: always Use strict and warnings, and as per the Basic debugging checklist, when in doubt, use Data::Dumper or Data::Dump to show your variables, since they will more clearly show you undef (or whitespace in strings, etc.).
In reply to Re: splice and indexes
by haukex
in thread splice and indexes
by teun-arno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |