CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sorting problems

 



Zeke_D
Deleted

Nov 13, 2000, 6:09 AM

Post #1 of 3 (766 views)
sorting problems Can't Post

The following code displays files in a six-column table. However the files are not sorted before they are displayed. I would like to sort $key as a nmber
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print "<table border='0' cellpadding='0' cellspacing='0' WIDTH='100%'>";
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
$c++;
print "<td align='center'><FONT SIZE='-1'>$key</td>";
unless ($c % 6) {
print "<TR>";
}
}
}
print "</tr></table>";
</pre><HR></BLOCKQUOTE>


sleuth
Enthusiast / Moderator

Nov 12, 2000, 10:42 PM

Post #2 of 3 (766 views)
Re: sorting problems [In reply to] Can't Post

 
Try adding this,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $key (sort keys %include) {
</pre><HR></BLOCKQUOTE>

Sleuth


rGeoffrey
User / Moderator

Nov 13, 2000, 11:42 AM

Post #3 of 3 (766 views)
Re: sorting problems [In reply to] Can't Post

If you are going to be sorting numerically rather than asciibetically, then you need to do something a little stronger than just 'sort', like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sort { $a <=> $b } keys %include
</pre><HR></BLOCKQUOTE>

I ran into a similar table problem recently. Below is a function based on my problem made to fit your data.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


&Print_Table (6, 'header message', sort { $a <=> $b } map { ($include{$_} eq 'yes') ? $_ : () } (keys %include));

sub Print_Table
{
my ($columns, $message, @names) = @_;
$columns = (@names > $columns) ? $columns : scalar (@names);

print "<table border='0' cellpadding='0' cellspacing='0' WIDTH='100%'>";
print (qq[\n<tr><th colspan="$columns">$message</th></tr>\n]) if ($message);
while (@names) {
print ("<tr>\n",
(map { "<td align='center'><FONT SIZE='-1'>$_</font></td>\n" } splice (@names, 0, $columns)),
"</tr>"
);
}
print "</table>\n";
}
</pre><HR></BLOCKQUOTE>

The function will take a number of columns and an optional header for the 'th' tag that will span the whole table on the first line. If you don't want it just send an empty string ('') in the second position.

The nice thing about this function is that if you have an array that is shorter than the proposed width of the table, it will take care of that for you.

Also I moved the checking for 'yes' outside of the printing of the table and placed it between the keys and the sort.

[This message has been edited by rGeoffrey (edited 11-13-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives