SunQuest
           Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 15th, 2001, 10:43 AM
gtaber gtaber is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 0 gtaber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation

I'm trying to sort an array by a specific field in the array. I can't get this work at all.

Here's a sample line in the array named @results.
<tr><td><a href=mailto:name1@myemail.com>Name 1</a></td><td>5834</td><td align=right>+1 510-555-5555</td></tr>

I want to sort on the field "Name1". So I know I have to split that one out. So here's my code to split out that field.

=================
foreach $line (sort ({ (split("/\>/", $a))[3] <=> (split("/\/", $b))[3] } @results))
{
print "$line";
}
=================

Any help would be greatly appreciated.

Guyle Taber
gtaber@geoworks.com

Reply With Quote
  #2  
Old February 15th, 2001, 05:05 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
Thumbs up

Here's a sort of generalized chunk of code you can use to do that, and used
in lots of other situations, too.


#You have an array. Each line of the array is 1 record.
#The &error() returns info to the user if the open fails. You write this.
open (CUSTFILE, "$db") or &error('Error opening database file');
@lines = <CUSTFILE>;
close(CUSTFILE);

#Decide which field you want to sort on. Start counting at 0.
#You want the third:
$field = 2;

#What separators your fields? Pretend it's a space. Might be ||.
$sep = ' ';

#Use Perl sort, giving it a flexible sorting subroutine
#that uses the values of $sep and $field to figure out what the field
#delimiter is and which field to use (0,1,2,...)
@lines = sort byDictd @lines;

sub byDictField
{
(split(/$sep/, lc($a)))[$field] cmp (split(/$sep/, lc($b)))[$field];
#cmp for comparing words
#dictionary order, ignoring case (w/o lc(), get ASCII order)
}

sub byNumField
{
(split(/$sep/, $a))[$field] <=> (split(/$sep/, $b))[$field];
#<=> for comparing numbers as values, notwords
}

Pretty flexible, and for many programs, it means you write the sort routine
once and just set $sep and $field as needed. I dunno about you, but I have
cases where records are returned sorted by date, by last name, by title,
and all sort of crap.

Hope this helps

Mickalo
__________________

Thunder Rain Internet Publishing

Custom Programming & Database development
Providing Personal/Business
Internet Solutions that work!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Sort Array on Specific Variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway