something of a noob question

N

NewsHound

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
 
J

Jim Gibson

NewsHound said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top