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

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

Thread Previous | Thread Next
From:
demerphq
Date:
March 30, 2013 16:54
Subject:
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
CANgJU+Vx0r7GmEiUQV9LSLi3T=iKtVHUyRbM_Og_-ziJSJ6eeg@mail.gmail.com
On 30 March 2013 17:12, Brad Gilbert <b2gills@gmail.com> wrote:
> 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.

Should it? We already say that if something inserts into the hash then
the order changes, so if they simply delete the latest key, then the
order shouldn't change at all. IOW, I believe the rules says that

my (%h,@k1,@k2);
%h= ("A".."Z");
delete $h{each %h};
delete $h{each %h};
while (my $k= each %h) { push @k1, $k }
@k2= keys %h;
is "@k1","@k2";

should pass.

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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