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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with SORT script ...

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
I'm trying to sort a pointer file by its contents, which will in turn allow me to display (FileName) in the correct order
.
The pointer file is as follows ..

FileName|CompleteDate|OrderDate|OrderNumber|CustomerName

eg. 123456|27|26|123|Jon Doe

On the page is a option to select how to sort ..
1. By Complete Date
2. By Order Date
3. By Order Number
4. By Amount
5. By Customer Name

--- code ---

if ($Sort eq 1) {
$OrderLine=0;
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/CompletedLogFile.txt") or dienice(); @orders = <INF>; close (INF);
if ($SortOrder eq "Reverse") {
foreach $line (reverse sort byComDate @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
}
else { foreach $line (sort byComDate @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
} }
if ($Sort eq 2) {
$OrderLine=0;
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/CompletedLogFile.txt") or dienice(); @orders = <INF>; close (INF);
if ($SortOrder eq "Reverse") {
foreach $line (reverse sort byOrderDate @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
}
else { foreach $line (sort byOrderDate @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
} }
if ($Sort eq 3) {
$OrderLine=0;
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/CompletedLogFile.txt") or dienice(); @orders = <INF>; close (INF);
if ($SortOrder eq "Reverse") {
foreach $line (reverse sort byOrderNumber @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
}
else { foreach $line (sort byOrderNumber @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
} }
if ($Sort eq 4) {
$OrderLine=0;
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/CompletedLogFile.txt") or dienice(); @orders = <INF>; close (INF);
if ($SortOrder eq "Reverse") {
foreach $line (reverse sort byPrice @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
}
else { foreach $line (sort byPrice @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
} }
if ($Sort eq 5) {
$OrderLine=0;
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/CompletedLogFile.txt") or dienice(); @orders = <INF>; close (INF);
if ($SortOrder eq "Reverse") {
foreach $line (reverse sort byName @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
}
else { foreach $line (sort byName @orders) { chomp($line); ($order,$CD,$OD,$ON,$PR,$name) = split(/\|/,$line); ;
$OrderLine++; $order =~ s/\s+$//; $OrderFile[$OrderLine] = $order; };
} }

sub byComDate {
return $a[1] <=> $b[1];
}
sub byOrderDate {
return $a[2] <=> $b[2];
}
sub byOrderNumber {
return $a[3] <=> $b[3];
}
sub byPrice {
return $a[4] <=> $b[4];
}
sub byName {
return $a[5] cmp $b[5];
}

--- ---

Then the complete files are loaded in order ..

--- ---

for ($i=1;$i<$OrderLine+1;$i++) {
open (INF,"../cgi-bin/COMPLETED_ORDERS/$Current_Month/$OrderFile[$i].txt"); @ary = <INF>; close(INF);
foreach $line (@ary) {
chomp($line);
($Var,$Result) = split(/\|/,$line);
if ($Var eq "Date") { $Date[$i] = "$Result"; $Date[$i] =~ s/\s+$//; }
if ($Var eq "Time") { $Time[$i] = "$Result"; $Time[$i] =~ s/\s+$//; }
if ($Var eq "ComDate") { $ComDate[$i] = "$Result"; $ComDate[$i] =~ s/\s+$//; }
if ($Var eq "ComTime") { $ComTime[$i] = "$Result"; $ComTime[$i] =~ s/\s+$//; }
if ($Var eq "Order Number") { $OrderNumber[$i] = "$Result"; $OrderNumber[$i] =~ s/\s+$//; }
if ($Var eq "First Name") { $firstname[$i] = "$Result"; $firstname[$i] =~ s/\s+$//; }
if ($Var eq "Middle Name") { $middlename[$i] = "$Result"; $middlename[$i] =~ s/\s+$//; }
if ($Var eq "Last Name") { $lastname[$i] = "$Result"; $lastname[$i] =~ s/\s+$//; }
if ($Var eq "Total Price") { $totalprice[$i] = "$Result"; $totalprice[$i] =~ s/\s+$//; }
}
}

--- ---

Then displayed on the page in a loop..

Problem is the various options to sort are making any difference. Its just displaying from top to bottom or visa versa with reverse, as in the pointer file.

Any help please .. thanks

 
ahh i've missed out the split in the subroutines ..

@a = split(/\|/,$a);
@b = split(/\|/,$b);

:)
 
Your code is way too verbose. YOu also seem to be spliting a five field file into six parts:


Code:
    0          1           2         3            4

FileName|CompleteDate|OrderDate|OrderNumber|CustomerName


Code:
   0    1   2   3   4   5
order,$CD,$OD,$ON,$PR,$name

- Kevin, perl coder unexceptional!
 
Sorry, yes, it should have read,

FileName|CompleteDate|OrderDate|OrderNumber|Price|CustomerName

This amount of code was the only way i could see to do it. I'm sure there is a simplier way, i'm new to perl tbh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top