Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Arrays of Arrays, by array element - complex

Status
Not open for further replies.

jez

Programmer
Joined
Apr 24, 2001
Messages
370
Location
VN
I hope someone can help and I will be very grateful if you can....

I have written a script that scans my website for new documents (type .doc,.xls, .pdf etc), like a focused search. I have used File::Find to find each document and then loaded up an array with that doc's info (age, path, type, section etc..this is for later in the program when I output the HTML and provide doc info for each of the latest.

File::Find executes a sub for each file it finds, so part of this is getting all the info into the array for each file. This array is then pushed into another array at the bottom of this sub so that the next file can be found and loaded into the first array. (sorry if not very clear).

It all works as far as finding the files etc.. but i want to order the final output by the age of the file, which is a variable at @AoA[$i][3], so I'm looping through the top level array and each element in it is another array with all the file info i have collected along the way.

I have tried to extract the age into another array and simply sort that with <=> but although this works I can't tie it back to the values in the original array....

The code for the main bit of the script is below and starts from the 'wanted' sub that is called for each file found by File::Find.

sub wanted {

$filedate = -M $File::Find::name;
$filedetail = int($filedate);
if ($filedetail < $depth) {
if ($_ =~/\.pdf$|\.doc$|\.xls$|\.zip$/){

if ($_ =~/\.pdf$/){$pic = &quot;pdf.gif&quot;;}
elsif ($_ =~/\.doc$/){$pic = &quot;word.gif&quot;;}
elsif ($_ =~/\.xls$/){$pic =&quot;excel.gif&quot;;}
elsif ($_ =~/\.zip$/){$pic =&quot;zip.gif&quot;;}
$filecred =(stat($File::Find::name))[9];
$filecred = (gmtime $filecred);
$_ = $File::Find::name;
s/$homepath/$relpath/;
$linker = $_;
@filedir =(split '/', $linker);
$filedee = $filedir[0] . &quot;/&quot; . $filedir[1] . &quot;/&quot; . $filedir[2] . &quot;/&quot; . $filedir[3] . &quot;/&quot; . $filedir[4];
if (/(.*)ta(.*)/){$sect=&quot;Trade Section&quot;; }
elsif (/(.*)elexon(.*)/){$sect = &quot;Elexon&quot;;}
else {$sect = &quot;general (not E or TA)&quot;; }
$icon = $iconpath . $pic;
if ($filedetail < $since ) { $newby=0; }
@filevals =($count, $filedetail, $sect, $icon, $linker, $filecred, $filedee, $filedir[4], $newby);
if ($sect eq &quot;ELEXON&quot;) {
push (@elexon, [@filevals]);
push (@erank, $filedetail);
} elsif ($sect eq &quot;Trading Arrangements&quot;) {
push (@trade, [@filevals]);
push (@trank, $filedetail);
} else {
push (@others, [@filevals]);
push (@orank, $filedetail);
}
$count ++;
}

}
##


this is an attempt I've made at sorting it but it doesn't work...

@erow = (sort @erank);
for($i=0;$i<@erow;$i++) {
$esorted[$i] = ($erow[$i]{$a} <=> $elexon[$i][1]{$b}) {print &quot;Days since uploaded <b>$esorted[$i][1]</b>&quot;;



I know this is long and if your still reading, thanks, but I really am stumped on this and I have only been learning perl for about 2 months now, so PLEASE HELP ME.

Oh and anyone who wants to see the whole script let me know.

 
The following code will sort AoA by the value at AoA[n][3]:
Code:
@sortedAoA = sort MySortRtn @AoA;
...
sub MySortRtn {
   return $a->[3] <=> $b->[3];
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks for this, it has certainly done something, I think I have another error somewhere else, because it is now looping too much and repeatedly listing file detail that it has already shown. I will track it down and sort it out though.

Thanks
 
Yeah, just a missing bracket, Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top