in reply to using function delete with arrays

You will get a different value for $cnt because you are actually extending the array each time.
From Perl 5.6.0, delete may be used on an array element. It sets the element to its undefined state, but does not shift the index of the ones after them - use splice for that. As with a hash, delete returns the deleted value.
This does not buy you much over using undef, in fact undef can be more efficient, but delete can be used on an array slice, which undef cannot.
The function exists can also be used on an array element, but not on a slice. The result given by exists is subtle. If the element was delete'd then both defined and exists will return FALSE. If the element was set to undef, then defined returns FALSE but exists returns TRUE!

Replies are listed 'Best First'.
Re^2: using function delete with arrays
by locked_user rumos-er (Novice) on Jul 17, 2007 at 13:52 UTC
    1. I know about slice. Thnx.
    2. So where is a bug or logical humor of function delete ?
    2.1. When all elements of array are 'deleted' then array is realy clear.
    2.2. Also if I make delete of last(!) element(-s) of array - number of array items will changes (I calculate it as result of scalar function not as $#).
    As I understand - scalar or delete works wrong. Exactly what of them - is my main question :)

      2.1, 2.2: The first paragraph of "perldoc -f delete" contains:

      In the case of an array, if the array elements happen to be at the end, the size of the array will shrink to the highest element that tests true for exists() (or 0 if no such element exists).

      Does this reasonably answer your question?

        I know this, but:
        1. if test for 'exists' returns false for deleted elements - then why scalar returns number of all elements ? (with deleted).
        2. if delete realy delete last elements - why it doesn't delete middle elements ?
        this is a questions about language realisation, not about how this function work.. I trying to understand for which purposes I can use this function.