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: Beginner:
Sorting Help Please

 



casManG
Deleted

Mar 11, 2000, 3:10 PM

Post #1 of 6 (1157 views)
Sorting Help Please Can't Post

When I open my text file that is formated like this and read it into an array:

90|Quake|30|
543|Unreal|29|
543|Half Life|29|
543|Unreal Tournament|29|

I need to sort the array by the second column only alphabetically (i.e. the game name) Can this be done?

Thanks
casManG


Cure
User

Mar 11, 2000, 10:57 PM

Post #2 of 6 (1157 views)
Re: Sorting Help Please [In reply to] Can't Post

Hi casManG

This will work for you::


open(CURE, "cure.txt") or die "coudlnt open file. $!";

map { print $_->[0] } sort { $a->[2] cmp $b->[2] } map{ [$_ ,
split(/\|/)] } <CURE>;

close(CURE);


Cure


[This message has been edited by Cure (edited 03-12-2000).]


casManG
Deleted

Mar 13, 2000, 2:09 PM

Post #3 of 6 (1157 views)
Re: Sorting Help Please [In reply to] Can't Post

Hey Cure,
Thanks for that but I was stupid and forgot to mention that I dont want to print but I want to store what you have it printing back into an array so that if I just have a
print @arrayname;
Then it would print sorted. Can this be done? Sorry for not being more clear before.

Thanks
casManG

-ACTUALLY-
I think I got it working now using a push. Please tell me if I am doing this a bad way but here is what I have:

map { push (@casManG, $_->[0]) }
sort { $a->[1] cmp $b->[1] }
map{ [$_ , split(/\|/)] } @data ;

Which puts it into the @casManG.
Is this a good way to do it?

Later
casManG


[This message has been edited by casManG (edited 03-13-2000).]


Cure
User

Mar 13, 2000, 2:44 PM

Post #4 of 6 (1157 views)
Re: Sorting Help Please [In reply to] Can't Post

Hi casManG

this will work for you::

open(CURE, "cure.txt") or die "coudlnt open file. $!";

map { push (@cure, $_->[0]) }
sort { $a->[2] cmp $b->[2] }
map{ [$_ , split(/\|/)] } <CURE>;

close(CURE);


print @cure;

Cure

[This message has been edited by Cure (edited 03-13-2000).]


casManG
Deleted

Mar 13, 2000, 2:47 PM

Post #5 of 6 (1157 views)
Re: Sorting Help Please [In reply to] Can't Post

Thanks man...that works perfect for me!


Cure
User

Mar 13, 2000, 3:06 PM

Post #6 of 6 (1157 views)
Re: Sorting Help Please [In reply to] Can't Post

Your Welcome:

Cure

 
 


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

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