Solved

Sychronized sorting?

Posted on 2002-05-07
7
186 Views
Last Modified: 2010-03-05
Hello, I have a list of users and their "points", however i have tried to come up with a sorting script (using bubble sort, eww...) a few times and have failed, can someone please show me an example script that would sort data that looks like this?

BTAnamedERIK:107
RonJonSSE:57
Oz7656:1
tkdman401:25
Sou1Tear:5
EZMONEY80:20
TkeMe2UrLeadr:11
deadphishphilay:3
AngelStr641:2
IronmanGus:1
BlInK182GuRl36k:12
cEDGabriel:3
PaPaloche:4
Infested2010:2
Danamajr:2
JeeStringDiva:1
MRitchie02:1
LilCGShuo2J:2
CGorg:1
Canivour:2
WutUpKneegroe:2
PoEbAbY3424:2
MusaUGA32:4
GreatGodlyOne:2

thank you very much.
0
Comment
Question by:PitViper
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6994181
open IN, "datat.txt";  #this would be file containing data
@i=<IN>;

print "sorted alpha, case insensitive\n\n";
@o=sort {uc($a) cmp uc($b)} @i;
print @o;
print "\n\nthis is sorted by the number at the end";
@o=sort {substr($a,index($a,':')+1) <=> substr($b,index($b,':')+1)} @i;
print @o;
0
 
LVL 84

Expert Comment

by:ozo
ID: 6994217
In what order do you want them sorted?
0
 

Author Comment

by:PitViper
ID: 6994234
bebonham - the points are your if you can explain your answer a little, it works but thats some advanced code and i'm having a hard time dissecting it. Thanks :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
bebonham earned 200 total points
ID: 6994260
sorting is easy once you get the hang of it.

first:

sort works like this

@sorted_list=sort {comparison} @list_to_sort;

the default sort is alpha, and case sensitive.

perl uses $a and $b as references to each of the sets of two items that get compared.

so, @output= sort {$a <=> $b} @input;

would numerically sort @input into @output.

this line here,
@o=sort {substr($a,index($a,':')+1) <=> substr($b,index($b,':')+1)} @i;

takes whatever is after the ':' (substr($a,index($a,':')+1))

and compares it against the same from item two - substr($b,index($b,':')+1)


the part about {uc($a) cmp uc($b)} just converts the data to upper case, so the comparison is case sensitive.
keep in mind, anything you do to the list in the sort subroutine doesn't modify the actual data, it only tells sort what order to put it in.
 
if you have other questions, please look here
http://www.perldoc.com/perl5.6.1/pod/func/sort.html

thanks,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6994263
correction....this
"the part about {uc($a) cmp uc($b)} just converts the data to upper case, so the comparison is case sensitive.
keep in mind, anything you do to the list in the sort subroutine doesn't modify "

should say the it makes the comparision NOT case sensitive
0
 

Author Comment

by:PitViper
ID: 6994295
thanks, is it possible to get it into a hash with eaze? i.e. $mypoints{$name}, would make life a lot easier :) thanks again.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6994367
heck yeah!!

, but keep in mind then it isn't sorted anymore...


open IN, "datat.txt";
@i=<IN>;

foreach(@i)
{
chomp($myPoints{substr($_,0,index($_,':'))}=substr($_,index($_,':')+1));
}
@k=keys %myPoints;
foreach(@k)
{
print "this dude " . $_ . " he has " . $myPoints{$_} . " points.\n";
}


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 109
Regular Expression question to filter with negation. 6 89
Regular Expression for URL 10 91
combine multiple lines 2 44
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now