Re: Correct data structure / sort method to use
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 9 Jan 2006 12:16:54 GMT
Niall Macpherson <niall.macpherson@xxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
>
> >
> > Then make the hash(ref) a parameter of the function instead of (or in
> > addition to) the keys. Untested:
> >
> > sub sort_by_used {
> > my $hr = shift;
> > sort { $hr->{ $a}->{ used} <=> $hr->{ $b}->{ used} } keys %$hr;
> > }
> >
> > Anno
> > --
>
> Thanks Anno . I would rather go with this than with Gunnars suggestion
> since in my real program which is much larger, the %dbspaces hash is
> not a global variable and I would prefer to pass it to the sort
> function explicitly.
>
> I had kind of figured this might be the besty way to go but I still
> don't understand how $a and $b get passed through to the sort
The system takes care of $a and $b, they're none of your business.
> subroutine though and therefore cannot figure out how to pass
> aditional parameters.
What have $a and $b to do with additional parameters?
> I tried changing
>
> my @sort_by_used_keys = sort sort_by_used ( keys %dbspaces);
^^^^
What is the second sort() for? It will destroy the order sort_by_used()
has established.
> to
>
> my @sort_by_used_keys = sort sort_by_used ( \%dbspaces, keys
> %dbspaces);
>
> but then in the sort_by_used sub I don't appear to get the value ..
>
> sub sort_by_used
> {
> my $hr = shift;
>
> print Dumper $hr;
> return(0);
> }
>
> gives me
>
> $VAR1 = undef;
Not if you call sort_by_used as shown, it would show the content of
%dbspaces.
If you want to pass the keys as parameters, the sub should in some
way make use of them. Yours doesn't.
sub sort_by_used {
my $hr = shift;
sort { $hr->{ $a}->{ used} <=> $hr->{ $b}->{ used} @_;
}
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.
- Follow-Ups:
- Re: Correct data structure / sort method to use
- From: Niall Macpherson
- Re: Correct data structure / sort method to use
- References:
- Correct data structure / sort method to use
- From: Niall Macpherson
- Re: Correct data structure / sort method to use
- From: Anno Siegel
- Re: Correct data structure / sort method to use
- From: Niall Macpherson
- Correct data structure / sort method to use
- Prev by Date: Re: Correct data structure / sort method to use
- Next by Date: Re: Correct data structure / sort method to use
- Previous by thread: Re: Correct data structure / sort method to use
- Next by thread: Re: Correct data structure / sort method to use
- Index(es):
Relevant Pages
|