473,591 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Skipping null/empty fields caught by split()

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)
  6.    # line = <textonly>,<text+num>,<ip>,<whatIwant>,
  7.    chomp($_); 
  8.    @a_splitLine = split (/,/, $_);
  10.    # move OS info out of string to avoid accidentally
  11.    # parsing over stuff
  12.    $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 overcomplicatin g it.
Jun 19 '09 #1
5 11964
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
  4. use strict;
  5. use warnings;
  6. use Data::Dumper;
  8. my $str = 'a,,,b,,,,6,,';
  9. my @fields = split /,/, $str;
  10. print Dumper @fields;
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';
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. ....
Jun 19 '09 #2
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
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  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";
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!
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
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. }
Jun 19 '09 #4
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.


Jun 20 '09 #5
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

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
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
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 <> ''
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>
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.
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...
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...
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...
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.
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...
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,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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...
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();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.