I am attempting to parse a CSV, but am not allowed to install the CSV parsing module because of "security reasons" (what a joke), so I'm attempting to use 'split' to break up a comma-delimited file.
My issue is that as soon as an "empty" field comes up (two commas in a row), split seems to think the line is done and goes to the next one.
Everything I've read online says that split will return a null field, but I don't know how to get it to go to the next element and not just skip to the next line. -
while (<INFILE>) {
-
# use 'split' to avoid module-dependent functionality
-
# split line on commas, OS info in [3] (4th group, but
-
# counting starts first element at 0)
-
-
# line = <textonly>,<text+num>,<ip>,<whatIwant>,
-
chomp($_);
-
@a_splitLine = split (/,/, $_);
-
-
# move OS info out of string to avoid accidentally
-
# parsing over stuff
-
$s_info = $a_splitLine[3];
-
Could anyone see either a better way to accomplish what I'm trying to do, or help get split to capture all the elements?
I was thinking I could run a simple substitution before parsing of a known string (something ridiculous that'll never show up in my data - like &^%$#), then split, and then when printing, if that matches the current item, just print some sort of whitespace, but that doesn't sound like the best method to me - like I'm overcomplicating it.
5 11896 RonB 589
Expert Mod 512MB
My issue is that as soon as an "empty" field comes up (two commas in a row), split seems to think the line is done and goes to the next one.
No it doesn't. You have a flawed impression of what's happening. -
C:\TEMP>type test.pl
-
#!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
use Data::Dumper;
-
-
my $str = 'a,,,b,,,,6,,';
-
my @fields = split /,/, $str;
-
print Dumper @fields;
-
-
C:\TEMP>test.pl
-
$VAR1 = 'a';
-
$VAR2 = '';
-
$VAR3 = '';
-
$VAR4 = 'b';
-
$VAR5 = '';
-
$VAR6 = '';
-
$VAR7 = '';
-
$VAR8 = '6';
-
- C:\TEMP>perldoc -f split
-
split /PATTERN/,EXPR,LIMIT
-
split /PATTERN/,EXPR
-
split /PATTERN/
-
split Splits the string EXPR into a list of strings and returns that
-
list. By default, empty leading fields are preserved, and empty
-
trailing ones are deleted. (If all fields are empty, they are
-
considered to be trailing.)
-
....
-
....
-
....
-
Interesting, so then how would I access the b or the 6? -
#!/bin/perl
-
-
use strict;
-
use warnings;
-
use Data::Dumper;
-
-
my $str = 'a,,,b,,,,6,,';
-
my @fields = split /,/, $str;
-
my $n = 0;
-
print Dumper @fields;
-
while ($fields[$n]) {
-
print "$n: $fields[$n]\n";
-
$n++;
-
}
-
print "done!\n";
-
-
$ ./splitTest.pl
-
$VAR1 = 'a';
-
$VAR2 = '';
-
$VAR3 = '';
-
$VAR4 = 'b';
-
$VAR5 = '';
-
$VAR6 = '';
-
$VAR7 = '';
-
$VAR8 = '6';
-
0: a
-
done!
-
In the above, my attempt to print with a while loop stops as soon as the first empty set is reached. I'm guessing I'd have to check each one to see which are valid and which are not, but what am I looking for - null?
RonB 589
Expert Mod 512MB
If you know which field/index you want, then simply print that field.
If you want/need to loop over the array elements, then use a for or foreach loop, not a while loop. -
for my $i ( 0..$#fields ) {
-
# only print fields that have a value
-
print "induce $i = '$fields[$i]'\n" if length $fields[$i];
-
}
-
I have to agree with Ron. Since this is a csv file, you should already know which field is what. All you would have to do is reference it by its index. Otherwise, you can use the code above to iterate through each one and pull out the variables with values other than null.
Regards,
Jeff
Cool, thanks. I am really only interested in one of those fields, but then have to make sure once I edit that field, I re-append all the others back on, so I will play around with that.
Thanks again!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chuck Anderson |
last post by:
I have a function with 7 inputs. The last three have default values.
I want to call that function specifying the first four, skip two and
then specify the last.
I thought I could write this...
|
by: Douglas Alan |
last post by:
Is there a canonical way of iterating over the lines of a file that
are null-separated rather than newline-separated? Sure, I can
implement my own iterator using read() and split(), etc., but...
|
by: Peter Neumaier |
last post by:
Hi!
I got a column, with different dates in it (Ddata type "nvarchar")...
when running a SELECT on this column, I'm trying to filter those
recordsets out, WHERE this column is NULL (I checked...
|
by: AFN |
last post by:
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:
...
|
by: David Logan |
last post by:
We need an additional function in the String class. We need the ability
to suppress empty fields, so that we can more effectively parse. Right
now, multiple whitespace characters create multiple...
|
by: john |
last post by:
Last week I posted about making a unique index on multiple fields to prevent
importing identical records twice. I still have trouble with the nulls in
the index. The only way that I can make it...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: gingawarrior |
last post by:
Hi All,
I've written some code to act as a validator for a set of form fields - it accepts a string of '~' delimited item names and then loops through to evaluate if they are either empty or =='999'...
|
by: BibI |
last post by:
Hi there,
I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX.
I have a data file with the following...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |