in reply to Re: On Removing Elements from Arrays
in thread On Removing Elements from Arrays
Then again, depending on what you want to test for you may now also test whether it is undefined because it was assigned an undefined value (explicitly or implicitly) or because it was deleted (or never explicitly used in an assignment):
my @array; @array[3..9] = (3,4,undef,undef,7); delete @array[4,8]; print join"\n",map{defined $array[$_] ? "$_: $_" : exists $array[$_] ? "$_: undef-but-exists" : "$_: undef-not-exists"} 0 .. 10; __END__ OUTPUT: 0: undef-not-exists 1: undef-not-exists 2: undef-not-exists 3: 3 4: undef-not-exists 5: undef-but-exists 6: undef-but-exists 7: 7 8: undef-not-exists 9: undef-but-exists 10: undef-not-exists
This output may seem confusing. 0-3 don't exist because they were never assigned anything. 4 and 8 don't exist because we deleted them. 5 and 6 exist because we explicitly assigned the undef value to them. Element 9 does exist because when you assign a smaller list to a larger slice, the remainder of the slice is implictly assigned undef values. 10 doesn't exist because we've never assigned anything to it (we are beyond the end of the array).
|
|---|