Sorting numbers in an array

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Wed Aug 18 03:44:10 UTC 2004


Scott Elcomb wrote:
> On Tue, 2004-08-17 at 22:51, Madison Kelly wrote:
> 
>>Hi all,
>>
>>   I have an array of interger values in perl (i386 Linux 2.6 - FC2). Is 
>>there an easy way that anyone knows to pull those values out from 
>>smallest value to largest (or vice versa)?
>>
>>   Thanks!
> 
> 
> How many dimensions are in the array?
> 
> Single-dimensional arrays can be done quickly with sort (works with
> floats as well)
> 
> Eg.
> 
> my @ints = qw(3 8 4 7 5 3 5 1);
> my @sorted_ints = sort(@ints);
> 
> foreach my $num (@sorted_ints) {
>         print "$num\n";
> }
> 
> Hope it helps.
> 
> - Scott.
> 

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]
  | |- Processing: [/Maddy's Images], file num: [10], space within: 
[963.1 KB - 986168 bytes]
  | |- Processing: [/vids], file num: [31], space within: [1.036 GB - 
1112068984 bytes]
  | |- Processing: [/Maddy's Images/Anime], file num: [38], space 
within: [6.13 MB - 6425716 bytes]
  | |- Processing: [/Maddy's Images/Favorite], file num: [22], space 
within: [2.65 MB - 2780829 bytes]
  | |- Processing: [/Maddy's Images/Icons-n-stuff], file num: [2], space 
within: [55.9 KB - 57271 bytes]
  | |- Processing: [/Maddy's Images/Mermaids], file num: [5], space 
within: [254.1 KB - 260205 bytes]
  | |- Processing: [/Maddy's Images/Post], file num: [2], space within: 
[123.9 KB - 126854 bytes]
  | |- Processing: [/Maddy's Images/style], file num: [0], space within: 
[0 Bytes - 0 bytes]
  | |- Processing: [/Maddy's Images/vids], file num: [31], space within: 
[1.036 GB - 1112068984 bytes]
  | |- Processing: [/Maddy's Images/Wallpapers], file num: [23], space 
within: [8.96 MB - 9393749 bytes]
  | |- Processing: [/Maddy's Images/Anime/Astro Boy], file num: [49], 
space within: [2.70 MB - 2829006 bytes]
  | |- Processing: [/Maddy's Images/Anime/chacha], file num: [91], space 
within: [4.87 MB - 5110453 bytes]
  | |- Processing: [/Maddy's Images/Anime/Futaba-kun], file num: [8], 
space within: [3.71 MB - 3888806 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ghost in the Shell], file num: 
[73], space within: [14.04 MB - 14724290 bytes]
  | |- Processing: [/Maddy's Images/Anime/jthm], file num: [16], space 
within: [274.4 KB - 280959 bytes]
  | |- Processing: [/Maddy's Images/Anime/Lum], file num: [29], space 
within: [2.60 MB - 2729299 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ranma], file num: [218], space 
within: [17.21 MB - 18043144 bytes]
  | |- Processing: [/Maddy's Images/Anime/.thumbnails], file num: [37], 
space within: [561.7 KB - 575189 bytes]
  | |- Processing: [/Maddy's Images/Anime/Uh-huh], file num: [8], space 
within: [2.04 MB - 2136178 bytes]
  | |- Processing: [/Maddy's Images/Anime/.xvpics], file num: [1], space 
within: [3.4 KB - 3432 bytes]
  | |- Processing: [/Maddy's Images/Anime/Futaba-kun/.xvpics], file num: 
[2], space within: [9.2 KB - 9443 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ghost in the Shell/Sonic], 
file num: [6], space within: [400.9 KB - 410529 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ghost in the 
Shell/.thumbnails], file num: [46], space within: [619.1 KB - 634008 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ranma/Sonic], file num: [8], 
space within: [1.67 MB - 1752506 bytes]
  | |- Processing: [/Maddy's Images/Anime/Ranma/.thumbnails], file num: 
[140], space within: [1.73 MB - 1815526 bytes]
  | |- Processing: [/Maddy's Images/Anime/Uh-huh/.thumbnails], file num: 
[19], space within: [181.5 KB - 185836 bytes]
  | |- Processing: [/Maddy's Images/Favorite/Mers], file num: [25], 
space within: [6.39 MB - 6695960 bytes]
  | |- Processing: [/Maddy's Images/Favorite/.thumbnails], file num: 
[20], space within: [232.0 KB - 237571 bytes]
  | |- Processing: [/Maddy's Images/Favorite/Mers/.thumbnails], file 
num: [23], space within: [309.6 KB - 317014 bytes]
  | |- Processing: [/Maddy's Images/Favorite/Mers/.xvpics], file num: 
[2], space within: [9.5 KB - 9738 bytes]
  | |- Processing: [/Maddy's Images/Icons-n-stuff/.xvpics], file num: 
[1], space within: [3.6 KB - 3669 bytes]
  | |- Processing: [/Maddy's Images/Mermaids/.thumbnails], file num: 
[3], space within: [36.3 KB - 37182 bytes]
  | |- Processing: [/Maddy's Images/Post/.thumbnails], file num: [1], 
space within: [12.2 KB - 12492 bytes]
  | |- Processing: [/Maddy's Images/style/.thumbnails], file num: [0], 
space within: [0 Bytes - 0 bytes]
  | |- Processing: [/Maddy's Images/Wallpapers/.thumbnails], file num: 
[23], space within: [221.8 KB - 227143 bytes]
  | |- Processing: [/Maddy's Images/Wallpapers/working], file num: [13], 
space within: [30.94 MB - 32437947 bytes]
  | |- Processing: [/Maddy's Images/Wallpapers/.xvpics], file num: [7], 
space within: [31.8 KB - 32543 bytes]
  | |- Processing: [/Maddy's Images/Wallpapers/working/.thumbnails], 
file num: [21], space within: [196.2 KB - 200919 bytes]
  | |- 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]
  | |- #1: Size: [1.036 GB - 1112068984 bytes]
  | |- #2: Size: [30.94 MB - 32437947 bytes]
  | |- #3: Size: [17.21 MB - 18043144 bytes]
  | |- #4: Size: [14.20 MB - 14892387 bytes]
  | |- #5: Size: [14.04 MB - 14724290 bytes]
  | |- #6: Size: [8.96 MB - 9393749 bytes]
  | |- #7: Size: [6.39 MB - 6695960 bytes]
  | |- #8: Size: [6.13 MB - 6425716 bytes]
  | |- #9: Size: [4.87 MB - 5110453 bytes]
  | |- #10: Size: [3.71 MB - 3888806 bytes]
  | |- #11: Size: [2.70 MB - 2829006 bytes]
  | |- #12: Size: [2.65 MB - 2780829 bytes]
  | |- #13: Size: [2.60 MB - 2729299 bytes]
  | |- #14: Size: [2.04 MB - 2136178 bytes]
  | |- #15: Size: [1.73 MB - 1815526 bytes]
  | |- #16: Size: [1.67 MB - 1752506 bytes]
  | |- #17: Size: [963.1 KB - 986168 bytes]
  | |- #18: Size: [619.1 KB - 634008 bytes]
  | |- #19: Size: [561.7 KB - 575189 bytes]
  | |- #20: Size: [400.9 KB - 410529 bytes]
  | |- #21: Size: [309.6 KB - 317014 bytes]
  | |- #22: Size: [274.4 KB - 280959 bytes]
  | |- #23: Size: [254.1 KB - 260205 bytes]
  | |- #24: Size: [232.0 KB - 237571 bytes]
  | |- #25: Size: [221.8 KB - 227143 bytes]
  | |- #26: Size: [196.2 KB - 200919 bytes]
  | |- #27: Size: [181.5 KB - 185836 bytes]
  | |- #28: Size: [123.9 KB - 126854 bytes]
  | |- #29: Size: [55.9 KB - 57271 bytes]
  | |- #30: Size: [36.3 KB - 37182 bytes]
  | |- #31: Size: [31.8 KB - 32543 bytes]
  | |- #32: Size: [16.7 KB - 17086 bytes]
  | |- #33: Size: [12.2 KB - 12492 bytes]
  | |- #34: Size: [9.5 KB - 9738 bytes]
  | |- #35: Size: [9.2 KB - 9443 bytes]
  | |- #36: Size: [3.6 KB - 3669 bytes]
  | |- #37: Size: [3.4 KB - 3432 bytes]
  | |- #38: Size: [0 Bytes - 0 bytes]
  | |- #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...

Thanks!!

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