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:
Using TEXT:CSV_XS to push headers

 



Hannibal_32
New User

Feb 22, 2012, 9:11 AM

Post #1 of 5 (1377 views)
Using TEXT:CSV_XS to push headers Can't Post

I have a .csv file that is formatted like this:


Quote
# SystemId: G9 Gateway.125
# STARTTIMESTAMP , STOPTIMESTAMP , SYSGRPNUM , GROUPTYPE , INCOMINGATTEMPTS , OUTGOINGATTEMPTS , SIGFAILURES , TOTALUSAGESEC , OVERFLOWS , OVERFLOWSEC , MINRESOURCEAVAIL , MINRESOURCEAVAILVALID
2012-02-16 21:30:00,2012-02-16 22:00:00,1,4,0,0,0,0,0,0,0,1



What I am trying to do is copy the headers out into another file where I will push them into an SQL database. I would like them pushed into the new file like this:


Code
header1,header2,header3,header4,header5


This is what I have so far, but am stumped on what to do next:


Code
#!/usr/local/bin/perl 

use Text::CSV_XS;
use strict;
use warnings;


print "-------------------------------\n";
print "-------------------------------\n";
print ".\n";
print "..\n";
print "Please choose which file you would like parse:\n";
$choice=<STDIN>;
chomp $choice;
print "-------------------------------\n";
print "-------------------------------\n";
print "What would you like the table to be titled?\n\n";
$tablename=<STDIN>;

our $data= "/opt/home/Xenesis/PM/files/$choice";
open(POST, "</opt/home/Xenesis/PM/files/$choice") or die "Could not open '$data'\n";

my $csv = Text::CSV_XS->new({ sep_char => ',' });
while (my $line = <POST>) {

if ($csv->parse($line)) {

my @fields = $csv->fields();
print "$line\n";

} else {
warn "Line unable to be parsed: $line\n";
}
}


I am using the TEXT::CSV_XS module as you can see, but I am lost on how to use it correctly. Any help is appreciated.

Thanks,

H


wickedxter
User

Feb 22, 2012, 1:25 PM

Post #2 of 5 (1370 views)
Re: [Hannibal_32] Using TEXT:CSV_XS to push headers [In reply to] Can't Post

did you look at the examples that in the POD for that module b/c i think the way your useing it is wrong

http://search.cpan.org/~hmbrand/Text-CSV_XS-0.86/CSV_XS.pm


Hannibal_32
New User

Feb 24, 2012, 8:17 AM

Post #3 of 5 (1318 views)
Re: [wickedxter] Using TEXT:CSV_XS to push headers [In reply to] Can't Post

I KNOW the way I'm using is wrong. Thanks for the resource. I'll see if I can make sense of it. If anyone has more direct advice, I'd appreciate it.

Thanks,


BillKSmith
Veteran

Feb 25, 2012, 10:39 AM

Post #4 of 5 (1304 views)
Re: [Hannibal_32] Using TEXT:CSV_XS to push headers [In reply to] Can't Post

I am not clear exactly what you want. The documentation for the module Text::CSV_XS references a specification for a CSV file. This document specifies that the first record may be a header record that specifies the field names. In your example, the second record seems to fill this roll. In all other ways, your file seems to conform. You can use the module, your script has to recognize the header record.

I assume that you want to print the first six fields from the second record and that the leading # character is part of the first field. The only method required from Text::CSV_XS is getline.

Note that I allow the user to override the default directory from the command line. I then prompt with a list of all the .csv files in that directory. Error checking requires the user to specify one of these names. The activestate prompt is the only one I know works on my platform. Similar modules are available for other systems.


Code
#!/usr/local/bin/perl 
use strict;
use warnings;
use Text::CSV_XS;
use ActiveState::Prompt qw( prompt );
use Readonly;
Readonly::Scalar my $DIR => defined $ARGV[0] ? $ARGV[0]
: "/opt/home/Xenesis/PM/files"
;
my @choices = glob $DIR ? "$DIR/*.csv" : "*.csv";
Readonly::Hash my %OPT => {
must_match => \@choices,
trim_space => 1,
no_match_msg => "You must choose from available files.",
};

my $choice = prompt( join("\n", @choices)."\nChoose: ", %OPT);
my $tablename = prompt("Enter output tabe header: ");

my $data = "$DIR/$choice";
my $csv = Text::CSV_XS->new( { sep_char => ',' } );
open my $POST, '<', $choice or die "Could not open '$data': $!";

print "\n\n", $tablename, "\n\n";


while ( my $line = $csv->getline( $POST ) ) {
print join( ',', @{$line}[0..5] ), "\n" if $. == 2;
}

Good Luck,
Bill


Hannibal_32
New User

Feb 27, 2012, 6:07 AM

Post #5 of 5 (1271 views)
Re: [BillKSmith] Using TEXT:CSV_XS to push headers [In reply to] Can't Post

Bill,

Thanks very much for the reply. I think you're solution will work. I will post a reply to this with my final code in case someone else is struggling with this.

Thanks guys...

H

 
 


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

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