in reply to Re^2: How to swap columns in a csv file ?
in thread How to swap columns in a csv file ?
"to be able to sort alphabetically 2 columns"
Hi, it's not completely clear what you want. Maybe it's as simple as the following, which sorts by column 1 and if there multiple instances of a value, then by column 3. See sort.
use strict; use warnings; use Text::CSV_XS 'csv'; my $aoa = csv( in => *DATA ); my @headers = shift @$aoa; my @sorted = sort { $a->[0] cmp $b->[0] || $a->[2] cmp $b->[2] } @$aoa +; csv( in => \@sorted, headers => @headers ); __DATA__ column1,column2,column3 b,foo,v a,bar,y c,baz,w b,qux,z b,nif,x
Output:
column1,column2,column3 a,bar,y b,foo,v b,nif,x b,qux,z c,baz,w
Hope this helps.
Update: added link to doc
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: How to swap columns in a csv file ?
by xuo (Acolyte) on Jan 07, 2025 at 20:03 UTC |