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!

sorting in perl

Status
Not open for further replies.

there

Programmer
Feb 4, 2002
15
CA
I am trying to sort in perl. I need HELP. It is not working for me and I have tried everything and anything. Please advise me what to do.

example of database
name|company|address|state

I want to sort in company both in asc and desc

open (LISTDATA, "$database");
@indata = <LISTDATA>;
close (LISTDATA);
foreach $entries (<LISTDATA>){
@tline = split(&quot;|&quot;,$entries);
$newlines = $tline[1];
($name1, $company, $address, $state) = split(/\|/, $entries);
}
$i = 0;
foreach (sort values %newlines) {
#@pbx_print = split(/\|/,$newlines{$_});
if ($name eq &quot;$name1&quot;) {
print &quot;<form action=\&quot;index.cgi\&quot; method=\&quot;POST\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td class=distributor_field>$name</td>\n&quot;;
print &quot;<td class=distributor_field><input type=\&quot;submit\&quot; class=submit value=\&quot;Select\&quot;></td>\n&quot;;
print &quot;</tr>\n&quot;;
print &quot;</form>\n\n&quot;;
$i++;
}
}
Thanks in Advance!

There
 
Your two lines:

$newlines = $tline[1];
($name1, $company, $address, $state) = split(/\|/, $entries);
}

I think should read:

($name1, $company, $address, $state) = split(/\|/,$entries);
$newlines{$company} = $_;

and then your line:

foreach (sort values %newlines)

should read

foreach (sort keys %newlines)
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,

I apprepriate your quick response. I did what you have recommended but it didn't work. Could you look over my coding below and see where I went wrong.

Objective: sort by $company

open (LISTDATA, &quot;$database&quot;);
@indata = <LISTDATA>;
close (LISTDATA);
foreach $entries (@indata){
($name1, $company, $address, $state) = split(/\|/, $entries);
$newlines{$company} = $_;
}
$i = 0;
foreach (sort keys %newlines) {
if ($name eq &quot;$name1&quot;) {
print &quot;<form action=\&quot;index.cgi\&quot; method=\&quot;POST\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td class=distributor_field>$company</td>\n&quot;;
print &quot;<td class=distributor_field><input type=\&quot;submit\&quot; class=submit value=\&quot;Select\&quot;></td>\n&quot;;
print &quot;</tr>\n&quot;;
print &quot;</form>\n\n&quot;;
$i++;
}
}

I will send you a cc to your email.

thanks for your help! Thanks in Advance!

There
 
foreach my $company (sort keys %newlines) {
if ($company eq $name1) {
print &quot;<form action=\&quot;index.cgi\&quot; method=\&quot;POST\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td class=distributor_field>$company</td>\n&quot;;
print &quot;<td class=distributor_field><input type=\&quot;submit\&quot; class=submit value=\&quot;Select\&quot;></td>\n&quot;;
print &quot;</tr>\n&quot;;
print &quot;</form>\n\n&quot;;
$i++;
}
}

 
LightElf,

thank you for reply.

1.0 line: if ($name eq &quot;$name1&quot;) {
===========
I need to keep it as $name eq $name1 due to calling of the previous webpage.

As you suggested, it didn't work. any suggestions?

Objectives: sort by $company of companies who have the same $name.

The call from the previous page for $name works. It does through the database and grab all the lines with the same $name. The next thing for that list, alpha it.


2.0 coding to sort by company
==============
open (LISTDATA, &quot;$database&quot;);
@indata = <LISTDATA>;
close (LISTDATA);
foreach $entries (@indata){
($name1, $company, $address, $state) = split(/\|/, $entries);
$newlines{$company} = $_;
}
$i = 0;
foreach my $company (sort keys %newlines) {
if ($name eq &quot;$name1&quot;) {
print &quot;<form action=\&quot;index.cgi\&quot; method=\&quot;POST\&quot;>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td class=distributor_field>$company</td>\n&quot;;
print &quot;<td class=distributor_field><input type=\&quot;submit\&quot; class=submit value=\&quot;Select\&quot;></td>\n&quot;;
print &quot;</tr>\n&quot;;
print &quot;</form>\n\n&quot;;
$i++;
}
} Thanks in Advance!

There
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top