473,776 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Skipping null/empty fields caught by split()

sicarie
4,677 Recognized Expert Moderator Specialist
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.

Expand|Select|Wrap|Line Numbers
  1. while (<INFILE>) {
  2.    # use 'split' to avoid module-dependent functionality
  3.    # split line on commas, OS info in [3] (4th group, but
  4.    # counting starts first element at 0)
  5.  
  6.    # line = <textonly>,<text+num>,<ip>,<whatIwant>,
  7.    chomp($_); 
  8.    @a_splitLine = split (/,/, $_);
  9.  
  10.    # move OS info out of string to avoid accidentally
  11.    # parsing over stuff
  12.    $s_info = $a_splitLine[3];
  13.  
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 overcomplicatin g it.
Jun 19 '09 #1
5 12002
RonB
589 Recognized Expert Moderator Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. C:\TEMP>type test.pl
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6. use Data::Dumper;
  7.  
  8. my $str = 'a,,,b,,,,6,,';
  9. my @fields = split /,/, $str;
  10. print Dumper @fields;
  11.  
Expand|Select|Wrap|Line Numbers
  1. C:\TEMP>test.pl
  2. $VAR1 = 'a';
  3. $VAR2 = '';
  4. $VAR3 = '';
  5. $VAR4 = 'b';
  6. $VAR5 = '';
  7. $VAR6 = '';
  8. $VAR7 = '';
  9. $VAR8 = '6';
  10.  
Expand|Select|Wrap|Line Numbers
  1. C:\TEMP>perldoc -f split
  2.     split /PATTERN/,EXPR,LIMIT
  3.     split /PATTERN/,EXPR
  4.     split /PATTERN/
  5.     split   Splits the string EXPR into a list of strings and returns that
  6.             list. By default, empty leading fields are preserved, and empty
  7.             trailing ones are deleted. (If all fields are empty, they are
  8.             considered to be trailing.)
  9. ....
  10. ....
  11. ....
  12.  
Jun 19 '09 #2
sicarie
4,677 Recognized Expert Moderator Specialist
Interesting, so then how would I access the b or the 6?

Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my $str = 'a,,,b,,,,6,,';
  8. my @fields = split /,/, $str;
  9. my $n = 0;
  10. print Dumper @fields;
  11. while ($fields[$n]) {
  12.    print "$n: $fields[$n]\n";
  13.    $n++;
  14. }
  15. print "done!\n";
  16.  
Expand|Select|Wrap|Line Numbers
  1. $ ./splitTest.pl
  2. $VAR1 = 'a';
  3. $VAR2 = '';
  4. $VAR3 = '';
  5. $VAR4 = 'b';
  6. $VAR5 = '';
  7. $VAR6 = '';
  8. $VAR7 = '';
  9. $VAR8 = '6';
  10. 0: a
  11. done!
  12.  
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?
Jun 19 '09 #3
RonB
589 Recognized Expert Moderator Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. for my $i ( 0..$#fields ) {
  2.     # only print fields that have a value
  3.     print "induce $i = '$fields[$i]'\n" if length $fields[$i];
  4. }
  5.  
Jun 19 '09 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
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
Jun 20 '09 #5
sicarie
4,677 Recognized Expert Moderator Specialist
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!
Jun 23 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2439
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 as : $retval = myfunction(arg1, arg2, arg3, arg4,,,arg7); .... but Php does not seem to want to let me do this. I get a parse
17
4241
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 considering that using "find -print0" is so common, it seems like there should be a more cannonical way. |>oug
3
3746
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 the table, there are "empty" fields in the column): "SELECT bla FROM bla WHERE myColumn NOT NULL" but I still receive those "empty" fields in my resultset ... so I tried it with: SELECT bla FROM bla WHERE myColumn <> ''
12
4055
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: <field5></field5> or <field5 /> but instead it doesn't even show the field at all for those records where field5 is Null! Instead it just shows: <field4>Whatever</field4>
19
10921
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 empty strings in the resulting string array.
6
6242
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 work is to make all the index' fields required fields and set the allow nulls setting to false. But then every field in the index should always have some kind of value, and I have to put a default value like "0" in them or "empty". Is this indeed the...
23
7417
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
3
1535
gingawarrior
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' (default not selected for a dropdown list). So far so good...however, although the value=='999' bit works, those fields that post a null or empty value (radio buttons, textareas) don't seem to get picked up. Any ideas on what I'm doing wrong...
4
7243
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 header that has been created on a Windows XP machine: My goal is to get rid of the header and the empty lines to finally have an output file with only the number entries.
0
9628
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.