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

CGI and Sort

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
0
0
US
I have this program that supposed to sort data. However, it stops at the first record.

open(INF,"<apart.out") or dienice("Couldn't open apart.out for reading: $! \n");
@data = <INF>;
close(INF);


foreach $i (sort bycity @data) {
chomp($i);
($name,$address,$city,$state,$zipcode,$location,$photo,$squarefoot,$bed,$bath,$lv1,$lv2,$lv3,$lv4,$lv5,$lv6,$lv7,$lv8,$askingprice,$security,$deposit,$email,$telephone,$timetocall,$comment1,$month,$day,$year) = split(/\|/,$i);

sub bycity {
@a = split (/\|/,$a);
@b = split(/\|/,$b);
$a[3] cmp $b[3] || $a[2] cmp $b[2] || $a[9] cmp $b[9] || $a[18] cmp $b[18] ;


}
 
Shouldn't this: foreach $i (sort bycity @data) be like this: foreach $i (sort bycity(@data))

There's always a better way. The fun is trying to find it!
 
tviman, I don't think the parens are necessary.

samesale, what do you mean the script just stops, does it give an error? Have you tried to narrow down where it is failing? Check to make sure @data gets all the data from the file by printing it out. Also check to make sure your sort is working as expected, by printing out the resulting array.

 
I do not get any error. It just stop after reading the first record and prints it. Even that record in not sorted.
 
i would try
foreach $i ( @data) {

to inspect my data and test the unshown rest of the script
 
@a = split (/\|/,$a);

split $a into @a if I had to guess this is the problem.

are you using use strict? It will help debug
 
Where are $a and $b being set?
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Throw everything into a MySQL database instead of "apart.out", will be much easier and quicker to sort and print.
 
PaulTEG, $a and $b are set when the sub is called from the [tt]sort[/tt] command, they hold the two rows to be compared.

samesale, I can't see anything obviously wrong with your script, except that you are missing a } to close your foreach loop.

It's a long shot, but try putting
Code:
print $data[2];
before the foreach statement to prove that you really are reading multiple lines from the input file.

-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top