Smith Jeff D wrote:

> Thanks for the feedback--maybe I screwed up but what happens for me is that
> the ordered array (1) only lists the keys, not the array values the hash key
> points to and (2) I still don't get an ordered list of the keys that are put
> in the "ordered" array--it comes out un-ordered.

Ooops.  Bad things happen when you start out too quickly specifying program
structures.  When you need an ordered array, you need to use an array, rather
than a hash.  Hashes *do not, ever* offer any warranty on internal ordering.
What they offer instead is speedy access to string-keyed data elements.

> I took your line and just added a for loop/print for the ordered array and
> got "red,yellow,blue, orange, violet, green" only  as the result.
>
> I must be dense but using just a Keys expression can't return the values,
> can it??--wouldn't it be better to do a while/each and get both key and
> value for the HofA somehow??

I'm afraid your original thread got spearated from this post, so that is hard to
say.  Usually, the way to control ordering of hash output is to sort on the
keys.

foreach (sort {$a <=> $b} keys %my_hash) {   #numeric
foreach (sort {$a cmp $b} keys %my_hash) {   #ASCII
foreach (sort {my_compare($a, $b)} keys %my_hash) {   # custom comparison
   do_things $my_hash{$_};
}

 If there is a special ordering to the keys that cannot be expressed in a
standard comparison function, this might be an indication that you shoud be
using a package to hold the information, so that you can treat the data in the
specific ordering called for in its logical context..


Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to