develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Perlfunc for each(), keys(), values() has been changed

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
March 30, 2013 16:12
Subject:
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
CAD2L-T1c8cPdgz_GLDhWsNABxeXYtEjj1AejewDaJm0N8eNOzQ@mail.gmail.com
On Sat, Mar 30, 2013 at 2:42 AM, Eric Brine <ikegami@adaelis.com> wrote:

> On Sat, Mar 30, 2013 at 1:45 AM, demerphq <demerphq@gmail.com> wrote:
>
>> On Friday, 29 March 2013, Eric Brine wrote:
>>
>>>  On Fri, Mar 29, 2013 at 10:39 AM, demerphq <demerphq@gmail.com> wrote:
>>>
>>>> Well it means that
>>>>
>>>> foreach my $key (keys %hash) {
>>>>   delete $hash{$key};
>>>> }
>>>>
>>>> will never change the iteration order.
>>>>
>>>
>>> So you can delete arbitrary keys without changing order?
>>>
>>
>> I never said you could did I?
>>
>
> Well, you said the following is doesn't change iteration order:
>
>
> foreach my $key (keys %hash) {
>   delete $hash{$key};
> }
>
> And the documentation is clear that the delete can be conditional, so that
> means the following doesn't change iteration order:
>
>
> foreach my $key (keys %hash) {
>   delete $hash{$key} if cond();
> }
>
> Since that deletes arbitrary keys, that means that deleting arbitrary keys
> doesn't change iteration order.
>
>
>
>>
>>>
>>> On Fri, Mar 29, 2013 at 7:39 AM, demerphq <demerphq@gmail.com> wrote:
>>>
>>>> Any insertion into the hash may change the order, as will any deletion
>>>
>>>
>>> So you can't delete arbitrary keys without changing order?
>>>
>>
>> Have you been drinking? are you tryin to wind me up? I mean seriously,
>> why would you omit the text that immediately follows that which you are
>> quoting? The text that specifically states the one circumstance in which
>> you can delete an item from a hash and be guaranteed to not have the
>> hash order change...
>>
>
> The text I did quote, and says you can delete arbitrary keys?
>
>
The text states that you can delete the current key during iteration,
without changing the order.

What it should say is that it won't affect the order until after the
iteration is done.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About