Sorting a hash containing a hash of hashes

Discussion in 'Perl Misc' started by IanW, Dec 14, 2005.

  1. IanW

    IanW Guest

    I have a hash arrangement that is used to store data like this:

    $hash{directory}{filename}{size}

    I want to sort the files in a directory by size before listing them. I
    thought I could do it like this ($dir contains the current directory):

    foreach my $f (sort { $filehash{$dir}{$a}{size} <=>
    $filehash{$dir}{$b}{size} } keys %{ $filehash{$dir} }) { }

    However that doesn't seem to work.. I have to confess to still not having
    got my head round data structures in Perl, so it may be something obvious
    but please put me out of my misery anyway.

    Thanks
    Ian
    IanW, Dec 14, 2005
    #1
    1. Advertising

  2. IanW

    IanW Guest

    hmmm I think that may actually work and it's something else in my code that
    isn't

    "IanW" <> wrote in message
    news:dnpa8f$kdm$...
    >I have a hash arrangement that is used to store data like this:
    >
    > $hash{directory}{filename}{size}
    >
    > I want to sort the files in a directory by size before listing them. I
    > thought I could do it like this ($dir contains the current directory):
    >
    > foreach my $f (sort { $filehash{$dir}{$a}{size} <=>
    > $filehash{$dir}{$b}{size} } keys %{ $filehash{$dir} }) { }
    >
    > However that doesn't seem to work.. I have to confess to still not having
    > got my head round data structures in Perl, so it may be something obvious
    > but please put me out of my misery anyway.
    >
    > Thanks
    > Ian
    >
    IanW, Dec 14, 2005
    #2
    1. Advertising

  3. IanW

    Paul Lalli Guest

    IanW wrote:
    > I have a hash arrangement that is used to store data like this:
    >
    > $hash{directory}{filename}{size}
    >
    > I want to sort the files in a directory by size before listing them. I
    > thought I could do it like this ($dir contains the current directory):
    >
    > foreach my $f (sort { $filehash{$dir}{$a}{size} <=>
    > $filehash{$dir}{$b}{size} } keys %{ $filehash{$dir} }) { }


    That looks correct to me. Have you seen the Posting Guidelines for
    this group? They recommend posting a short-but-complete script which
    demonstrates the problem you're experiencing. I'm willing to bet that
    if you took this step - if you parred your problem down to the shortest
    possible script that demonstrates the error - that you'd find it on
    your own.

    For example:
    #!/usr/bin/perl
    use strict;
    use warnings;

    my %filehash;
    my $dir = '.';
    $filehash{$dir}{$_}{size} = -s $_ for glob ('./*');
    foreach my $f (sort { $filehash{$dir}{$a}{size} <=>
    $filehash{$dir}{$b}{size} }
    keys %{$filehash{$dir}} ) {
    print "File: $f, size: $filehash{$dir}{$f}{size}\n";
    }
    __END__
    File: ./temp.txt~, size: 0
    File: ./terms.txt, size: 7
    File: ./terms.txt~, size: 10
    File: ./rmd, size: 30
    File: ./cpd, size: 32
    File: ./rm22, size: 34
    File: ./cp22, size: 36
    <etc>

    > However that doesn't seem to work..


    That is the worst of all possible error descriptions. *How* doesn't it
    work? What output do you recieve that fails to meet your expectations?
    Again, please see the Posting Guidelines for advice on how to ask
    better questions, and therefore receive better answers.

    > I have to confess to still not having
    > got my head round data structures in Perl, so it may be something obvious


    Not in what you've shown us. I'm betting the error is in the creation
    of the data structure, not in its printing. I recommend a debugging
    statement along the lines of:
    use Data::Dumper;
    print Dumper(\%filehash);

    That will show you exactly what the hash contains.

    > but please put me out of my misery anyway.


    I'm not sure that means what you think it means...

    Paul Lalli
    Paul Lalli, Dec 14, 2005
    #3
  4. IanW

    Ian Stuart Guest

    IanW wrote:
    > hmmm I think that may actually work and it's something else in my code that
    > isn't
    >
    > "IanW" <> wrote in message
    > news:dnpa8f$kdm$...
    >
    >>I have a hash arrangement that is used to store data like this:
    >>
    >>$hash{directory}{filename}{size}
    >>
    >>I want to sort the files in a directory by size before listing them. I
    >>thought I could do it like this ($dir contains the current directory):
    >>
    >>foreach my $f (sort { $filehash{$dir}{$a}{size} <=>
    >>$filehash{$dir}{$b}{size} } keys %{ $filehash{$dir} }) { }
    >>
    >>However that doesn't seem to work.. I have to confess to still not having
    >>got my head round data structures in Perl, so it may be something obvious
    >>but please put me out of my misery anyway.

    I would have expected your sort to work...

    You can always do a test by getting Data::Dumper to spit out the list:

    if ($debug) {
    @f = sort {
    $filehash{$dir}{$a}{size}
    <=>
    $filehash{$dir}{$b}{size}
    } keys %{ $filehash{$dir} }
    warn "Dumper @f";
    }

    Just a thought....

    --
    Ian, Perl Laghu
    Ian Stuart, Dec 14, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Moura
    Replies:
    2
    Views:
    141
    Bruno Moura
    Nov 28, 2009
  2. Scott  Gilpin
    Replies:
    2
    Views:
    211
  3. Perl Learner

    Hashes of hashes or just one hash ?

    Perl Learner, Jun 8, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    207
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    202
  5. Replies:
    3
    Views:
    199
Loading...

Share This Page