freelanceprogrammers.org Forum Index » Perl

Help with the sort function


View user's profile Post To page top
beckywb Posted: Tue Jun 22, 2004 12:16 pm


Joined: 22 Jun 2004

Posts: 2
Help with the sort function
Can anyone help me with using the sort function in Perl?

I am printing a list of data that I would like to have sorted by a
numeric value. I can`t seem to get the sort function to work. I want
to sort by the $lexile variable in the list. I have tried all kinds
of variations and placement of the @sorted=sort {$a<=>$b } @unsorted
command and am having no luck. Below is my code without any sort
command included. Can someone tell me the exact syntax and where to
place the code to make this work?

Thanks.

#!/usr/bin/perl
require "cgi-lib.pl";
$match="no";
&ReadParse;
print &PrintHeader;

#open file in read only mode
open (FILE,"readingcounts.txt") || die "Can`t find database
";
@indata = <FILE>;
close(FILE);

#start HTML and table header
print <<"PrintTag";
<html>
<head>
<TITLE>Castille Elementary School, Reading Counts Program</TITLE>
</head>
<body>
<table width="500" table border="1">
<tr>
<th>Book Title</th>
<th>Author</th>
<th>Lexile number</th>
<th>Points</th>
</tr>
PrintTag

#assign variables to each record in the database
foreach $i (@indata)
{
chomp($i);
($catalognumber,$title,$author,$lexile,$readinglevel,$grl,$points,$ve
rsion) = split(/|/,$i);

print "<tr>";
print "<td>$title</td>";
print "<td>$author</td>";
print "<td align="right">$lexile</td>";
print "<td align="right">$points</td>";
print "</tr>
";
}


print "</table>";
print "</body></html>";

#end of program
Reply with quote
Send private message
View user's profile Post To page top
merlynstoneh... Posted: Tue Jun 22, 2004 9:15 pm


Joined: 21 May 2004

Posts: 44
Help with the sort function
>>>>> "beckywb" == beckywb <beckywb2@...> writes:

beckywb> require "cgi-lib.pl";
beckywb> $match="no";
beckywb> &ReadParse;

No no no. Don`t use this ancient unsupported library.

use CGI.pm... it comes standard with Perl, and has done so for nearly
a decade.

I ignored the rest of your request... fixing this is more important.


--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Reply with quote
Send private message
View user's profile Post To page top
slackusr Posted: Wed Jun 23, 2004 3:11 am


Joined: 23 Jun 2004

Posts: 2
Help with the sort function
I`m understanding that you have lines of text with fields delimited
by |, so when you get the lines from the file, you want to sort the
lines by the lexile number, which is the 4th item (the subscript
will be 3) returned from the split function... so you want to do

@indata = sort {(split(/|/, $a))[3] <=> (split(/|/, $b))[3]}
@indata;

@indata will then be (numerically) sorted in ascending order by
lexile number

Would be a little nicer to load the items into a list of lists
though, so you`d only have to split the line once.

--- In perl_official@yahoogroups.com, "beckywb" <beckywb2@c...>
wrote:
> Can anyone help me with using the sort function in Perl?
>
> I am printing a list of data that I would like to have sorted by a
> numeric value. I can`t seem to get the sort function to work. I
want
> to sort by the $lexile variable in the list. I have tried all
kinds
> of variations and placement of the @sorted=sort {$a<=>$b }
@unsorted
> command and am having no luck. Below is my code without any sort
> command included. Can someone tell me the exact syntax and where
to
> place the code to make this work?
>
> Thanks.
>
> #!/usr/bin/perl
> require "cgi-lib.pl";
> $match="no";
> &ReadParse;
> print &PrintHeader;
>
> #open file in read only mode
> open (FILE,"readingcounts.txt") || die "Can`t find database
";
> @indata = <FILE>;
> close(FILE);
>
> #start HTML and table header
> print <<"PrintTag";
> <html>
> <head>
> <TITLE>Castille Elementary School, Reading Counts Program</TITLE>
> </head>
> <body>
> <table width="500" table border="1">
> <tr>
> <th>Book Title</th>
> <th>Author</th>
> <th>Lexile number</th>
> <th>Points</th>
> </tr>
> PrintTag
>
> #assign variables to each record in the database
> foreach $i (@indata)
> {
> chomp($i);
>
($catalognumber,$title,$author,$lexile,$readinglevel,$grl,$points,$ve
> rsion) = split(/|/,$i);
>
> print "<tr>";
> print "<td>$title</td>";
> print "<td>$author</td>";
> print "<td align="right">$lexile</td>";
> print "<td align="right">$points</td>";
> print "</tr>
";
> }
>
>
> print "</table>";
> print "</body></html>";
>
> #end of program
Reply with quote
Send private message
View user's profile Post To page top
merlynstoneh... Posted: Wed Jun 23, 2004 6:45 am


Joined: 21 May 2004

Posts: 44
Help with the sort function
>>>>> "slackusr" == slackusr <no_reply@yahoogroups.com> writes:

slackusr> @indata = sort {(split(/|/, $a))[3] <=> (split(/|/, $b))[3]}
slackusr> @indata;

slackusr> Would be a little nicer to load the items into a list of lists
slackusr> though, so you`d only have to split the line once.

Yes, that`s exactly why people keep referencing the Schwartzian Transform,
to solve this precise case:

@indata = map $_->[0], sort { $a->[4] <=> $b->[4] }
map [$_, split /|/, $_], @indata;

If that`s too complex, look at Sort::Fields in the CPAN.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Reply with quote
Send private message
View user's profile Post To page top
beckywb Posted: Thu Jun 24, 2004 11:33 pm


Joined: 22 Jun 2004

Posts: 2
Help with the sort function
I don`t understand the reference to "look at Sort::Fields in the
CPAN". What are you referring to?


--- In perl_official@yahoogroups.com, merlyn@s... wrote:
> >>>>> "slackusr" == slackusr <no_reply@yahoogroups.com> writes:
>
> slackusr> @indata = sort {(split(/|/, $a))[3] <=> (split(/|/,
$b))[3]}
> slackusr> @indata;
>
> slackusr> Would be a little nicer to load the items into a list of
lists
> slackusr> though, so you`d only have to split the line once.
>
> Yes, that`s exactly why people keep referencing the Schwartzian
Transform,
> to solve this precise case:
>
> @indata = map $_->[0], sort { $a->[4] <=> $b->[4] }
> map [$_, split /|/, $_], @indata;
>
> If that`s too complex, look at Sort::Fields in the CPAN.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503
777 0095
> <merlyn@s...> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment
Perl training!
Reply with quote
Send private message
View user's profile Post To page top
perl_jam03 Posted: Fri Jun 25, 2004 10:15 pm


Joined: 05 May 2004

Posts: 16
Help with the sort function
Merlyn refers to the Perl on-line repository at cpan.org. You can
also search at search.cpan.org.

On Windows you can run `ppm` from the command line and then install
by entering `intall Sort-Fields`.

Have fun
Rex


--- In perl_official@yahoogroups.com, "beckywb" <beckywb2@c...>
wrote:
> I don`t understand the reference to "look at Sort::Fields in the
> CPAN". What are you referring to?
>
>
> --- In perl_official@yahoogroups.com, merlyn@s... wrote:
> > >>>>> "slackusr" == slackusr <no_reply@yahoogroups.com> writes:
> >
> > slackusr> @indata = sort {(split(/|/, $a))[3] <=> (split(/|/,
> $b))[3]}
> > slackusr> @indata;
> >
> > slackusr> Would be a little nicer to load the items into a list
of
> lists
> > slackusr> though, so you`d only have to split the line once.
> >
> > Yes, that`s exactly why people keep referencing the Schwartzian
> Transform,
> > to solve this precise case:
> >
> > @indata = map $_->[0], sort { $a->[4] <=> $b->[4] }
> > map [$_, split /|/, $_], @indata;
> >
> > If that`s too complex, look at Sort::Fields in the CPAN.
> >
> > --
> > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1
503
> 777 0095
> > <merlyn@s...> <URL:http://www.stonehenge.com/merlyn/>
> > Perl/Unix/security consulting, Technical writing, Comedy, etc.
etc.
> > See PerlTraining.Stonehenge.com for onsite and open-enrollment
> Perl training!
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help