in reply to foreach loop question

> Probably, the list for looping is populated before the loop starts?

Yes.

You might want to try

while (my ($key, $value) = each(%h)) { }
But I am not sure if you'll get all your new keys.

P.S. next; at the end of foreach block is redundant.

Replies are listed 'Best First'.
Re: foreach loop question
by robartes (Priest) on Apr 16, 2003 at 18:50 UTC
    You might want to try {code with each} But I am not sure if you'll get all your new keys.

    Actually, the docs for each specifically say that you should not do this - as each returns hash elements in an essentially random order (or at least in an unpredictable order), so doing something like this is playing with fire :).

    CU
    Robartes-

      That's what I meant by "I am not sure if you'll get all your new keys." I guess it did not come across that way.

      I always figured that each and related functions keys and values iterated over the hash from start to finish with the order being determined by the hashing algorithm. Can an internals person shed some light on what determines this "essentially random order"?


      "The dead do not recognize context" -- Kai, Lexx

        Unless you run the hashing algorithm in your head (I believe it's in hv.h), it'll look sufficiently random. :)

Re: Re: foreach loop question
by dda (Friar) on Apr 16, 2003 at 18:53 UTC
    About "next": I added it while experimenting with the code. Thanks :)

    --dda