Operating System - HP-UX
1826004 Members
3324 Online
109690 Solutions
New Discussion

Re: Perl split function question

 
SOLVED
Go to solution
Hakki Aydin Ucar
Honored Contributor

Perl split function question

I need to get array values to the variable from a .csv data file. it is OK the following code to test but every line has a newline at the line end,also neeed to get rid of this somehow; as shown in ARRAY 68 there is line end (newline) character, and need to split also this array ,tried as you can see in commented out below, but I could not as far :
CODE:
#!/usr/bin/perl -w
$FILE = 'pnm_om.06_14_2010.0030.ISBLTUGS01D.OT.CSV';

open(FILE) or die ("Could not open file.");
{ local $/; $content = }
@column = split(/[\s]*[,][\s]*/,$content);
#@column = split(/\n/,$column);
for ( $I = 0; $I <= 80; $I++ ) {
print "ARRAY $I : $column[$I]\n";
}
RESULT:
ARRAY 0 : Date
ARRAY 1 : Time
ARRAY 2 : Switch Name
ARRAY 3 : Group Name
.....
ARRAY 65 : Reg31 Name
ARRAY 66 : Reg31 Value
ARRAY 67 : Reg32 Name
ARRAY 68 : Reg32 Value
06-14-2010
ARRAY 69 : 00:30:08
5 REPLIES 5
Hakki Aydin Ucar
Honored Contributor

Re: Perl split function question

The solution is:
#!/usr/bin/perl -w
$FILE = 'pnm_om.06_14_2010.0030.ISBLTUGS01D.OT.CSV';

open(FILE) or die ("Could not open file.");
{ local $/; $content = }
@column = split(/[\s]*[,\n][\s]*/,$content);
#@column = split(/\n/,$column);
for ( $I = 0; $I <= 80; $I++ ) {
print "ARRAY $I : $column[$I]\n";
}

does it sound logical ??
Thanks in advance.
James R. Ferguson
Acclaimed Contributor

Re: Perl split function question

Hi:

> does it sound logical ??

If this works for your data then, OK.

You should use (as Merijn pointed out in an earlier thread of yours) the 'Text::CSV' module to parse CSV data.

Why are you slurping the whole file rather than reading a one line at a time?

You should be using 'use strict'. This will save you from making stupid mistakes and wasting hours chasing them down.

Regards!

...JRF...
Hakki Aydin Ucar
Honored Contributor

Re: Perl split function question

>You should use (as Merijn pointed out in an earlier thread of yours) the 'Text::CSV' module to parse CSV data.

Because its dependency is some C stuff like cc that is not available in customer environment,

>Why are you slurping the whole file rather than reading a one line at a time?

Speed!
I already wrote a code with test purpose Text::CSV (older version of Merijn wrote) ,and tried, it was so slow to process my input file .
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl split function question

Hi (again):

You could do things more Perl-ishly and rigorously like:

#!/usr/bin/perl
use strict;
use warnings;
my $file = '/tmp/mycsv';
my ( $fh, $content, @column );
open( $fh, '<', $file ) or die("Could not open '$file'");
{
local $/;
$content = <$fh>;
}
@column = split( /\s*[,\n]\s*/, $content );
my $j = 0;
for my $i (@column) {
print "ARRAY", $j++, " : ", "$i\n";
}
1;

Regards!

...JRF...
Geoff Gariepy
New Member

Re: Perl split function question

The Text::xSV module does not have C-language dependencies; it is pure Perl. I would strongly recommend you look into using this module for all of your CSV file parsing needs. It's available on CPAN as well as PPM.

--Geoff