Sorting a hash containing a hash of hashes

I

IanW

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
 
P

Paul Lalli

IanW said:
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
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
 
I

Ian Stuart

IanW said:
hmmm I think that may actually work and it's something else in my code that
isn't
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....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top