Sorting numbers in an array

Kevin C. Krinke kckrinke-eqjHHVKjh9GttCpgsWEBFlaTQe2KTcn/ at public.gmane.org
Wed Aug 18 06:30:59 UTC 2004


On Tue, 2004-08-17 at 23:44, Madison Kelly wrote:
> Scott Elcomb wrote:
> > On Tue, 2004-08-17 at 22:51, Madison Kelly wrote:
> > 
> >>Hi all,
> >>

<snip>

> 
> Hmm, I didn't think that far ahead (a side effect of coding so late I 
> guess... :p ). For a single-dimension array it worked great, thanks! 
> (and to Anton and Andy!).
> 
> What I need to do now though is somehow sort along with it two other 
> arrays... What I had been trying to do, and what I am realizing now 
> probably not work, is to somehow tie three arrays together and sort them 
> together. Maybe it will be clearer if I explain what I am doing:
> 
> I look at a directory and count how much space is used by the files 
> within it and how many files they are (excluding sub-directories). So 
> for example; here is the unsorted output I am working with:
> 
> -=-=- code output -=-=-
>   | |- Recording the new directory in the database as being on the 
> destination partition:
>   | |- Processing: [/], file num: [4], space within: [14.20 MB - 
> 14892387 bytes]

<snip>

>   | |- Processing: [/Maddy's Images/Wallpapers/working/.xvpics], file 
> num: [4], space within: [16.7 KB - 17086 bytes]
> 
> 
> Below here is the directory content sizes now sorted thanks to the help 
> of you guys:
> 
> 
>   | |- Sorting the directories by the size of their files (excluding 
> sub-directories):
>   | |- #0: Size: [1.036 GB - 1112068984 bytes]

<snip>

>   | |- #39: Size: [0 Bytes - 0 bytes]
>   | \- Finished listing where files from this partition will be backed 
> up to!
>   -=-=- end code output -=-=-
> 
> As I print out each unsorted directory, file count and file size I fead 
> each variable into an array. I am now thinking of tieing the values 
> together into a single string, sorting them and splitting them back out 
> using a ':' as a seperator. Will the sizes still sort numberically 
> though? It also seems like a really cumbersome way of doing it, too...

Off the top of my head, late at night and feeling creative:

/usr/bin/perl -e '
#: First we need to process the directories and build up our data
#: structure. In this case we are going to have each row of output
#: represented as follows:
#:
#:  $data{$path} = { files => $num_files, space => $bytes_of_space }
#:
#: Of course you can replace/remove/add as many key=>val pairs to
#: each $path hash ref to suite your needs.

#: Fudge some data for example purposes
%data = ( "/images/many"  => { files => 13, space => 32437947 },
          "/images/style" => { files => 2, space => 1024 },
          "/images/these" => { files => 0, space => 0 },
          "/images/those" => { files => 1, space => 0 },
          "/images/Post"  => { files => 1, space => 37182 } );

#: First we sort "normally"
@sorted_by_path =
  sort( keys( %data ) );
print "\nSorted by path name (ascending alphanumeric):\n";
Print_Output( @sorted_by_path );

#: Then we sort on the files key value (note $b and $a)
@sorted_by_files =
  sort { $data{$b}->{files} <=> $data{$a}->{files} } keys( %data );
print "\nSorted by file count (descending):\n";
Print_Output( @sorted_by_files );

#: And now we sort on the space key value
@sorted_by_space =
  sort { $data{$b}->{space} <=> $data{$a}->{space} } keys( %data );
print "\nSorted by space within (descending):\n";
Print_Output( @sorted_by_space );

#: our simple sub to print some human readable output
sub Print_Output {
  @sorted = @_;
  $c = 0;
  foreach $path ( @sorted ) {
    #: we need to be careful here because some rows of data have
    #: no files and/or size.
    print "\t[" . $c ."] " . $path .
          "\t: " . ($data{$path}->{files}||"0") .
          "\t: " . ($data{$path}->{space}||"0") . "\n";
    $c++;
  }
}
'

For more Perl help try:
http://to.pm.org/                   (Toronto Perl Mongers)
http://perlmonks.org/               (Excellent forums)
http://perl.com/pub/q/documentation (Where I began to learn Perl)
http://search.cpan.org/             (Where I now learn the most)

- Kevin

-- 
Kevin C. Krinke <kckrinke-eqjHHVKjh9GttCpgsWEBFlaTQe2KTcn/@public.gmane.org>
Open Door Software Inc.

--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list