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 how ?

 



patrik
stranger

Jun 14, 2001, 3:02 AM

Post #1 of 9 (1662 views)
Sorting how ? Can't Post

Got a litle problem, I got a file looking something like:

Name|9|email@mail.com|file1
Name5|2|email@mail.com|file1
Name2|3|email@mail.com|file1
Name7|1|email@mail.com|file1
Name9|8|email@mail.com|file1

I need to sort the print "blablabla" thing
after the 2:nd parameter in the fil.
It should print the lines like:

Name7 -- 1 -- email@mail.com -- file1
Name5 -- 2 -- email@mail.com -- file1
Name2 -- 5 -- email@mail.com -- file1
Name9 -- 8 -- email@mail.com -- file1
Name -- 9 -- email@mail.com -- file1

I used a while loop, but I would like the code shorter.
Is there a simple way ???
Im using PERL on a Win98/NT server.....
Thx Patrik

Excuse the bad english
Im from Sweden ;)


Jasmine
Administrator / Moderator

Jun 14, 2001, 6:10 AM

Post #2 of 9 (1658 views)
Re: Sorting how ? [In reply to] Can't Post


Code
my @newarray = 
map { join ' -- ', @$_ }
sort { $a->[1] <=> $b->[1] }
map { [ split /\|/ ] }
<DATA>;


A little explanation is in order. Let's go through it, line by line, backwards.


Code
    <DATA>;  # or array

This is what's being worked on.


Code
    map { [ split /\|/ ] }

Each line of the array is being split on | and is being passed along to:


Code
    sort { $a->[1] <=> $b->[1] }

the sorting line, which sorts on the second element of the array. The sorted line is being returned to:


Code
    map { join ' -- ', @$_ }

which joins the elements with -- as shown in the sample of the desired result.


Code
my @newarray =

New array, sorted. This is a very rudimentary explanation, so a description of the subtle nuances of what's going on have been omitted.

Welcome to the Schwartzian Transform.




patrik
stranger

Jun 14, 2001, 6:29 AM

Post #3 of 9 (1656 views)
Re: Sorting how ? [In reply to] Can't Post

Thx 4 quick reply, but I just found out that it the "sorting"
should be:

Name5 - 289347 - email@email.com - file1
Name2 - 12548- email@email.com - file1
Name6 - 11111 - email@email.com - file1
Name3 - 9854 - email@email.com - file1
Name4 - 5412 - email@email.com - file1
Name1 - 998 - email@email.com - file1
Name7 - 423 - email@email.com - file1

(not the real numbers but something like that)
Is it possible to do this the same way ??
(at work, cant try it right now)
Its 4 a "scoreboard" kind of thing ?!

Excuse the bad english
Im from Sweden ;)


Jasmine
Administrator / Moderator

Jun 14, 2001, 7:06 AM

Post #4 of 9 (1654 views)
Re: Sorting how ? [In reply to] Can't Post

If you need the second column in descending order, just change the sort line to:


Code
sort { $b->[1] <=> $a->[1] }



If that's not it, I'm not understanding the "(not the real numbers but something like that)" portion of your question...?



patrik
stranger

Jun 14, 2001, 7:31 AM

Post #5 of 9 (1653 views)
Re: Sorting how ? [In reply to] Can't Post

Tnx again =)
Going to try it out when i get home from work :P

Excuse the bad english
Im from Sweden ;)


patrik
stranger

Jun 14, 2001, 1:44 PM

Post #6 of 9 (1646 views)
Re: Sorting how ? [In reply to] Can't Post

Thx, works great =)

Excuse the bad english
Im from Sweden ;)


Cure
User

Jun 15, 2001, 8:16 PM

Post #7 of 9 (1636 views)
Re: Sorting how ? [In reply to] Can't Post

my @newarray = map {s!\|! -- !g;substr($_,1) }
reverse sort
map { pack('C',/\|(.+)\|/) . $_} <DATA>;


:)


Cure



Jasmine
Administrator / Moderator

Jun 15, 2001, 8:38 PM

Post #8 of 9 (1635 views)
Re: Sorting how ? [In reply to] Can't Post

Cure, are you sure about that? That yields:


Code
Name -- 9 -- email@mail.com -- file1 
Name9 -- 8 -- email@mail.com -- file1
Name7 -- 1 -- email@mail.com -- file1
Name5 -- 2 -- email@mail.com -- file1
Name2 -- 3 -- email@mail.com -- file1

which isn't sorted by the second column, and it gives "Argument "9|email@mail.com" isn't numeric in pack at ..." warnings.

Methinks you mean:

Code
map { pack('C',/\|(.+)\|.+\|/) . $_}

And even then, it's still slower, not much slower, but slower nonetheless ;)

Code
Benchmark: timing 500000 iterations of schwartz, substrpack... 
schwartz: 7 wallclock secs ( 6.87 usr + 0.00 sys = 6.87 CPU) @ 72780.20/s (n=500000)
substrpack: 8 wallclock secs ( 7.03 usr + 0.00 sys = 7.03 CPU) @ 71123.76/s (n=500000)




patrik
stranger

Jun 16, 2001, 2:05 AM

Post #9 of 9 (1632 views)
Re: Sorting how ? [In reply to] Can't Post

Ok, I got another "problem" now,
i need to update the file =)
Heres what i got on a html page:
2 text fields named: "name1" and "name2"
2 textfields named: "res1" & "res2"
"name1" refers to the first column i the file
and "res1" refers to the second column.
If "ref1" is more than "ref2" the second
column in the line starting width "name1" should increse ($ref1 = $ref +3) with 3.
Its working but the rutin is huge, is there a good way
with little code to do this ? If U gurus dont get this
reply and Ill post the html & the perlcode =)


Excuse the bad english
Im from Sweden ;)

 
 


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

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