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: Intermediate:
Sort array by 2 columns - comma separated

 



patricks
New User

Nov 17, 2009, 9:46 AM

Post #1 of 7 (1308 views)
Sort array by 2 columns - comma separated Can't Post

Hi All,

I have comma separated data as such:


Code
   

Col 1.....Col 2.....Col 3.....Col 4....Col 5.....Col 6

Active, 112233, not-required, Johnd, HelenH, This is the title

Agave, 223344, required, LisaM, FredB, Another title

Blue Lagoon, 556677, required, JustinB, AngelaF, Here is another title



I want to do this in Perl and I am on Windows.

I want to sort by both the 1st and 2nd column in ascending order. I have been successful sorting on the 1st column, not the 2nd.

Here are some of my attempts:


Code
   

@data = sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } <IN>; # Sorts just the 1st column

@TOI_buf = sort {lc($a) cmp lc($b) || ($a) cmp ($b)} @TOI_buf; # Sorts just the 1st column



I have tried changing the 2nd cmp to <=>, but it made no difference. I work on Perl once or twice a year, so I would appreciate

any help here. Many thanks....


shawnhcorey
Enthusiast


Nov 17, 2009, 1:08 PM

Post #2 of 7 (1302 views)
Re: [patricks] Sort array by 2 columns - comma separated [In reply to] Can't Post


Code
my @sorted = map { $_->[0] } 
sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] || $a->[0] cmp $b->[0] }
map { [ $_, split m{ \s* \, \s* }msx ] }
<IN>;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


patricks
New User

Nov 17, 2009, 1:55 PM

Post #3 of 7 (1300 views)
Re: [shawnhcorey] Sort array by 2 columns - comma separated [In reply to] Can't Post

I copied and pasted the code. I ran it and it sorts ONLY on Col 2, not Col 1 (Primary) and Col 2 (Secondary). Any hints?


shawnhcorey
Enthusiast


Nov 17, 2009, 3:33 PM

Post #4 of 7 (1298 views)
Re: [patricks] Sort array by 2 columns - comma separated [In reply to] Can't Post

When I run it, it sorts on column 1 then 2. How about some sample data?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


patricks
New User

Nov 17, 2009, 4:02 PM

Post #5 of 7 (1296 views)
Re: [shawnhcorey] Sort array by 2 columns - comma separated [In reply to] Can't Post

Hi: I have attached a small portion of the CSV file to this thread.

Thank you for your help...
Attachments: Sample.csv (1.26 KB)


shawnhcorey
Enthusiast


Nov 17, 2009, 4:40 PM

Post #6 of 7 (1293 views)
Re: [patricks] Sort array by 2 columns - comma separated [In reply to] Can't Post

I get this result:

Code
Active,4197,Active Monitoring: IPv4 Support ,required,anilk,jchevook 
Java,4985,Java: Captive portal ,required,aranjan,mjaganna
MEGA-ACCOUNT,4485,MEGA-ACCOUNT: MPLS: Multiple VLAN Registration Protocol ,required,rramakri,goli
MPLS,2796,MPLS Converged Services: 1/2 label operations,required,nitinb,akwong
MPLS,3639,MPLS Converged Services: RSVP Automesh: setting up full mesh ,required,nitinb,dishac
NEO,6100,NEO: XPY w/Fine queuing,required,schew,jmarcoli
NEO,6101,NEO: J-Train w/Coarse queuing,required,schew,jmarcoli
NEO,6102,NEO: 20x1G PFC NIC,required,schew,pbf
NEO,6108,NEO: 4x100G XFP NIC,required,schew,pbf
NEO,6109,NEO: support of clueless oversubscription services,required,vdeokar,seamans
NEO,6118,NEO: Juniper forwarding feature parity,required,vdeokar,kumarn
NEO,6119,"NEO: L2 Basic (VPLS, IRB, Mesh Group) Feature Parity (through JUNOS 9.1), and scaling & performance(TRD)",required,vdeokar,zhifang
NSR,2720,NSR: RSVP engress,required,nitinb,avneesh
ZZZZZ,4309,"Cuervo, PD-7-100XGE ",not-required,sdredge,
ZZZZZ,5068,Flamengo: (FV) PPPoE scaling,not-required,mborowicz,
ZZZZZ,6065,DMI support of IPD Profiler ,not-required,anushab,
ZZZZZ,6099,NEO: Coming Hardware,not-required,schew,
ZZZZZ,6104,"NEO: platform support for ABC, XYZ and WXY",not-required,schew,


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


patricks
New User

Nov 18, 2009, 12:10 PM

Post #7 of 7 (1254 views)
Re: [shawnhcorey] Sort array by 2 columns - comma separated [In reply to] Can't Post

I worked it again and it works for me now. Many, many thanks! I tried a many times, appreciate it!Smile

 
 


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

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