something of a noob question

Discussion in 'Perl' started by NewsHound, Jun 30, 2004.

  1. NewsHound

    NewsHound Guest

    I've been playing around with Perl for a while, but I'm still something of
    a beginner. I was wondering what happens in the following case. Take
    a "foreach" loop iterating through the sorted keys of a hash:

    foreach $key (sort keys %hash) {
    # do stuff here
    }

    Does the "sort" function re-sort the keys before each iteration of the
    "foreach" loop? I was wondering if it is possible to insert additional
    elements into the hash in the body of the loop code and have the
    sort function operate on them for the next iteration of the loop?

    Thanks,
    Mike
     
    NewsHound, Jun 30, 2004
    #1
    1. Advertising

  2. NewsHound

    Jim Gibson Guest

    In article <JYAEc.38444$>, NewsHound
    <> wrote:

    > I've been playing around with Perl for a while, but I'm still something of
    > a beginner. I was wondering what happens in the following case. Take
    > a "foreach" loop iterating through the sorted keys of a hash:
    >
    > foreach $key (sort keys %hash) {
    > # do stuff here
    > }
    >
    > Does the "sort" function re-sort the keys before each iteration of the
    > "foreach" loop? I was wondering if it is possible to insert additional
    > elements into the hash in the body of the loop code and have the
    > sort function operate on them for the next iteration of the loop?


    The 'sort keys' creates a temporary array and the foreach iterates over
    that array. But don't take my work for it -- try it and see.

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
     
    Jim Gibson, Jun 30, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yttrium
    Replies:
    7
    Views:
    2,541
    Yttrium
    Oct 15, 2003
  2. Guest
    Replies:
    4
    Views:
    483
    Guest
    Oct 13, 2004
  3. Pekka Järvinen
    Replies:
    2
    Views:
    668
    Richard Tobin
    Apr 29, 2008
  4. Replies:
    4
    Views:
    221
    Tad McClellan
    Jun 1, 2007
  5. Replies:
    9
    Views:
    162
Loading...

Share This Page