F
filippo2991
I have the following piece of code that doesn't work properly.
<MYFILE> is .csv file (semicolon delimited fields).
I split each row into an array
I put a reference of each array into another array
I want to order data by the seventh column of .csv data. The order
(ascending/descending) depends on $order value. My code (below) doesnt
work. Why?
is there a less redundant (and working) way to do that?
my @lines;
while (<MYFILE>) {
push @lines, [split /;/];
}
if ($order == 1) {
sub by_seventh {
$a->[6] <=> $b->[6];
}
foreach (sort by_seventh @lines) {
print DATA_SORT join (';', @{$_});
}
} else {
sub by_seventh {
$b->[6] <=> $a->[6];
}
foreach (sort by_seventh @lines) {
print DATA_SORT join (';', @{$_});
}
}
<MYFILE> is .csv file (semicolon delimited fields).
I split each row into an array
I put a reference of each array into another array
I want to order data by the seventh column of .csv data. The order
(ascending/descending) depends on $order value. My code (below) doesnt
work. Why?
is there a less redundant (and working) way to do that?
my @lines;
while (<MYFILE>) {
push @lines, [split /;/];
}
if ($order == 1) {
sub by_seventh {
$a->[6] <=> $b->[6];
}
foreach (sort by_seventh @lines) {
print DATA_SORT join (';', @{$_});
}
} else {
sub by_seventh {
$b->[6] <=> $a->[6];
}
foreach (sort by_seventh @lines) {
print DATA_SORT join (';', @{$_});
}
}