Gossamer Forum
Home : General : Perl Programming :

help with sorting

Quote Reply
help with sorting
Hi to all,
Having a problem with sorting.
My file "client.txt" has lines of 2 fields,
in the form of:
8564|Jansens
3657|Vandamme
1235|Fullmark
...etc
I would like to sort this file on the first field
so that this file is rewritten as:
1235|Fullmark
3657|Vandamme
8564|Jansens

I made the following code but it does not work
and returns an error
Can someone help me please.

=============================================
# Variables used
$clientpath = "/home/mysite/public_html/client.txt";
$flock = "1";
# Sorting Code
open (clientfile, "$clientpath");
if ($flock == 1) {flock(clientfile, 2);}
while (<clientfile>) {
$line = $_;
chop ($line) if ($line =~ /\n$/);@fields = split (/\|/, $line);
foreach $line (sort { $line{$a} <=> $line{$b} })
}
close (clientfile);
==============================================

Regards,
Sanuk
Quote Reply
Re: [sanuk] help with sorting In reply to
Looks like this code is causing the problem:

foreach $line (sort { $line{$a} <=> $line{$b} })
}

You have missing brackets but are also trying to sort incorrrectly. $line{$a} would be used when sorting a hash....you are sorting an array so would just need $a <=> $b

Last edited by:

RedRum: Jan 13, 2002, 3:47 AM
Quote Reply
Re: [RedRum] help with sorting In reply to
Hi RedRum,
Thanks for your fast reply
I have now changed the code as You sugested,
But it no work and gives an error in this line:
"foreach $line (sort $a <=>$b)"

Here my changed code:
=============================================
# Variables used
$clientpath = "/home/mysite/public_html/client.txt";
$flock = "1";
# Sorting Code
open (clientfile, "$clientpath");
if ($flock == 1) {flock(clientfile, 2);}
while (<clientfile>) { ## Start while open
$line = $_;chop ($line) if ($line =~ /\n$/);@fields = split (/\|/, $line);
foreach $line (sort $a <=>$b)
} ## End While open
close (clientfile);
==============================================

Regards,
Sanuk
Quote Reply
Re: [sanuk] help with sorting In reply to
Yes because your foreach loop is missing { }. Foreach loops normally look like:

foreach (something) {
something
}

...whereas yours is currently:

foreach (something)

You need to get everything into the array and then use something like:

@array = sort { $a <=> $b } @array;

Last edited by:

RedRum: Jan 13, 2002, 4:54 AM
Quote Reply
Re: [RedRum] help with sorting In reply to
Thank You,
I understand that I am doing something wrong with the For-Each-Loop.
But I have never worked with areas and also do not have enough CGI-knowledge.
Could You please help me to write this small code snippet so that the file is rewritten and sorted.
Regards,
Sanuk
Quote Reply
Re: [sanuk] help with sorting In reply to
You could always use a shell script to handle it...
try:
cat file.txt | sort
and if you want to output it back to a file (not the original file itself though!)
cat file.txt | sort > file2.txt

Adrian
Quote Reply
Re: [brewt] help with sorting In reply to
Hi,
Thanks for reply, But Sorry I dont umderstand about shell script !!!
As you see I already have problems with CGI.
I only would like that someone helps me out with my For-Each-Loop., so that my file get sorted on the first field of this file
And YES I want that after sorted, the result is saved as the same file again
All help welcome and a nice day for everuone.
Regards,
Sanuk