Re: how to efficiently sort hash without using keys()



Paul Johnson [PJ], on Wednesday, May 25, 2005 at 12:21 (+0200) made
these points:

>> @array = sort @array;
PJ> Using each will also affect the iterator.

hm.

PJ> I suggest changing the the previous code so it doesn't care what happens
PJ> to the iterator. For example:
PJ> my @keys = keys %hash;
PJ> for (@keys)
PJ> {
PJ> some_sub_that_messes_with_the_iterator();
PJ> }

yes, I actually change that, and using it like this:
my $hr = $get->fetchall_hashref('id');
$hr->{cattable_index} = [ reverse sort {
$hr->{$a}{counter} <=> $hr->{$b}{counter} } keys %{$hr} ];
$hr->{oses} = get_oses($hr);
$hr->{categories} = get_categories($hr);
$hr->{categories_index} = [ sort { $a <=> $b } keys %{$hr->{categories}} ];

for my $os ( @{$hr->{oses}} ) {
....

hashes, hashes, hashes ! :)

--

How do you protect mail on web? I use http://www.2pu.net

["Whenever I see a dalmation I say, `What number are you?'" - s.w.]


.



Relevant Pages

  • Re: using an assoc. array as a set
    ... >>Simply because I want to use an assoc. ... array as a set, i don't care ... I suppose it should be done on top of simple hashes. ...
    (comp.lang.perl.misc)
  • Re: Collections and Decorators
    ... take care of everything? ... iterator is one example. ... the source code but before I did that I wanted to ask this so as to ... static class SynchronizedCollection ...
    (comp.lang.java.programmer)
  • C# v2.0 Iterators and the Visitor design pattern
    ... So as the object is traversed, the iterator first returns the position ... The idea is that you can traverse a collection where the Visitor design ... the iterator takes care of that for you. ... Visitor pattern was kinda neat. ...
    (microsoft.public.dotnet.languages.csharp)