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: Re: [BigRedEO] Parsing CSV file - Add Concatenated field/sort: Edit Log



FishMonger
Veteran / Moderator

Mar 24, 2016, 1:31 PM


Views: 39020
Re: [BigRedEO] Parsing CSV file - Add Concatenated field/sort

See if this sorting test does what you want.

Code
#!/usr/bin/perl/  

use strict;
use warnings;
use Data::Dumper;

my $filename = '/swpkg/shared/batch_processing/mistints/test.csv';
$filename = 'test.csv';

open my $FH, $filename
or die "Could not read from $filename <$!>, program halting.";

# Read the header line.
chomp(my $line = <$FH>);
my @fields = split(/,/, $line);
print "Field Names:\n", Dumper(@fields), $/;

my @data;
# Read the lines one by one.
while($line = <$FH>) {

# split the fields, concatenate the first three fields,
# and add it to the beginning of each line in the file
chomp($line);
my @fields = split(/,/, $line);
unshift @fields, join '_', @fields[0..2];
push @data, \@fields;
}
close $FH;
print "Unsorted:\n", Dumper(@data), $/;

@data = sort {
$a->[0] cmp $b->[0] ||
$a->[20] cmp $b->[20] ||
$a->[23] cmp $b->[23] ||
$a->[26] cmp $b-> [26]
} @data;

print "Sorted:\n", Dumper(@data);

exit;



(This post was edited by FishMonger on Mar 24, 2016, 1:41 PM)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 24, 2016, 1:41 PM
Post edited by FishMonger (Veteran) on Mar 24, 2016, 1:41 PM


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

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