Re: Correct data structure / sort method to use



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.
.



Relevant Pages

  • Re: sorting DBM hash
    ... a DBM Hash. ... The first sort routine prints out keys and value. ... An anonymous sub routine is used to hold sub routines ...
    (perl.beginners)
  • sorting DBM hash
    ... Sorting DBM Hash ... The first sort routine prints out keys and value. ... An anonymous sub routine is used to hold sub routines ...
    (perl.beginners)
  • sorting DBM hash
    ... Sorting DBM Hash ... The first sort routine prints out keys and value. ... An anonymous sub routine is used to hold sub routines ...
    (perl.beginners)
  • Re: Correct data structure / sort method to use
    ... Anno Siegel wrote: ... 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 ... but then in the sort_by_used sub I don't appear to get the value .. ...
    (comp.lang.perl.misc)
  • RE: sorting problem with range definition
    ... column so you probably need to reverse the keys. ... Sub Sort() ... I put the recorded macro sort which works here: ...
    (microsoft.public.excel.programming)