cronus has asked for the wisdom of the Perl Monks concerning the following question:

Hmm... I seek a wise guru - I need to create an array that holds filenames in it along with an arbitary number for each file. And I need the array sorted by the number from largest to smallest. Like so...
$array['adsf.htm'][10] $array['sdfg.htm'][7] $array['qwet.htm'][4] $array['zxcv.htm'][2]
Advice ?

Replies are listed 'Best First'.
Re: Mind Bending Arrays
by boo_radley (Parson) on Jan 04, 2001 at 19:04 UTC
    That would be a hash. Arrays are indexed numerically.

    From perldata :

    Perl has three data structures: scalars, arrays of scalars, and associative arrays of scalars, known as ``hashes''. Normal arrays are indexed by number, starting with 0. (Negative subscripts count from the end.) Hash arrays are indexed by string.
    The only gotcha I can think of is that hashes aren't stored 'in order' -- perl will store hash elements in a seemingly random fashion, but that's what sort keys %hash is for.
Re: Mind Bending Arrays
by extremely (Priest) on Jan 04, 2001 at 19:15 UTC
    Well that smells like homework but what you've got so far is close to fine. Try $array[0]=['file.ext',37] which is a single record in array @array or if the filenames are unique try $hash{file.ext}=37 which is a single record in the hashed array %hash; Sorting the first is as easy as:
    @sorted = sort {$a[1] <=> $b[1]} @array; # use {$b[1] <=> $a[1]} for a reversed sort # or {$a[0] cmp $b[0]} to sort by filenames

    Sorting the hash is a little trickier but look around on the site for Schwartzian Transform. =)

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Mind Bending Arrays
by c-era (Curate) on Jan 04, 2001 at 19:20 UTC
    I think everyone is looking at this wrong, if I see your question right you want to sort the arrays by their length (if I'm wrong it was fun to do anyways). You can do this with an array or a hash. With an array you should make the filename the first element of the array bellow is an example
    @array = (["filename1",1],["filename2",1,2,3],["filename3",3,2]); @array = sort {$#$b <=> $#$a} @array; for (@array){ print $$_[0]; }
    and here is an example of using a hash (note you need an array to store the order of the keys)
    %hash = ("filename1" => [1], "filename2" => [1,2,3], "filename3" => [3 +,2]); @sortedkeys = sort {$#{$hash{$b}} <=> $#{$hash{$a}} } keys (%hash); print @sortedkeys;
    I hope this helps.
Re: Mind Bending Arrays
by I0 (Priest) on Jan 04, 2001 at 19:05 UTC
    @array = sort {$b->[1] <=> $a->[1] } ( ['adsf.htm',10], ['sdfg.htm',7], ['qwet.htm',4], ['zxcv.htm',2], );
      I appreciate the help, but bare in mind I have no experience with arrays at all. From reading through other posts on arrays, I know I can do;
      @array = sort ($b->[1] <=> $a->[1]) @array;
      but how do I fill the array with the filename and number in the first place ?
        You should probably be using a hash instead.

        my %hash; while (my ($filename, $number) = &get_both) { $hash{$filename} = $number; } my @sorted_files = sort { $hash{$a} <=> $hash{$b} } keys %hash;
        If you really need to put this in an array (to keep track of the insertion order, for example), you could probably do it like this:
        my @array; while (my ($filename, $number) = &get_both) { push(@array, [ $filename, $number ]); } my @sorted = sort { $a->[1] <=> $b->[1] } @array;