
rGeoffrey
User
Mar 12, 2001, 9:32 AM
Post #4 of 10
(1248 views)
|
The previous example wanted to sort data that looked like this... xxxx, bbb, NNN123, xxxxxxxx and used this code...
my $SortField = 2; print OUTPUT map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [(split (',', $_))[$SortField], $_] } <INPUT>; You want to work with data like... Sitename|SiteURL|Hitsout|HitsIn and sort on HitsIn, which is $array[3] after the split. So we need to change the split to work on a pipe not a comma Make $SortField == 3 And change 'cmp' for string comparison to '<=>' for number comparison Which means that this should do the job..
my $SortField = 3; my @results = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [(split ('\|', $_))[$SortField], $_] } <DATA>; And to print the table we can make a few changes to the original and get...
print ("\nThe sorted table for HitsIn is:\n<table>\n", "<tr><th>Order</th><th>Sitename</th><th>SiteURL</th>", "<th>Hitsout</th><th>HitsIn</th></tr>\n", ); my $ct = 1; foreach my $row (@results) { print '<tr>'; print "<td>", $ct++, "</td>\n"; foreach my $col (split ('\|', $row)) { print "<td>$col</td>\n"; } print "</tr>"; } print "\n</table>\n"; -- Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?
|