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:
Parsing CSV file - Add Concatenated field/sort

 

First page Previous page 1 2 3 Next page Last page  View All


BigRedEO
Novice

Apr 12, 2016, 9:51 AM

Post #51 of 53 (2637 views)
Re: [FishMonger] Parsing CSV file - Add Concatenated field/sort [In reply to] Can't Post

I just found a more concise way to get that work for those three date fields. Newest version below worked just fine. Now to figure out how to do that with the DATETIME fields -


Code
#!/usr/bin/perl/ 

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

my $filename = 'tested.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 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];
$_ = join '-', (split /\//)[2,0,1] for $fields[14];
$_ = join '-', (split /\//)[2,0,1] for $fields[20];
$_ = join '-', (split /\//)[2,0,1] for $fields[23];
push @data, \@fields;
}
close $FH;

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

open my $OFH, '>', '/swpkg/shared/batch_processing/mistints/parsedMistints.csv';
print $OFH join(',', @$_), $/ for @data;
close $OFH;

exit;


FishMonger
Veteran / Moderator

Apr 12, 2016, 9:59 AM

Post #52 of 53 (2634 views)
Re: [BigRedEO] Parsing CSV file - Add Concatenated field/sort [In reply to] Can't Post


Quote
Now to figure out how to do that with the DATETIME fields


Write a subroutine which uses the Time::Piece module as Borodin showed you on your SO crosspost. That single sub could handle both of your date formats.

If you didn't want to go that route, then you could use a slightly modified version of this split/join approach.


(This post was edited by FishMonger on Apr 12, 2016, 10:02 AM)


BigRedEO
Novice

Apr 12, 2016, 10:00 AM

Post #53 of 53 (2632 views)
Re: [FishMonger] Parsing CSV file - Add Concatenated field/sort [In reply to] Can't Post

That's just what I'm attempting right now (and sorry for the cross posts - I posted both at the same time to see who might get back to me and you both replied around the same time).

First page Previous page 1 2 3 Next page Last page  View All
 
 


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

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