Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

"Use of uninitialized value" due to empty elements in an array

by michaelp (Initiate)
on Mar 31, 2010 at 13:34 UTC ( [id://832047]=perlquestion: print w/replies, xml ) Need Help??

michaelp has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I have read in a tab-delimited file, and split each line by  @line_split= split(/\t/,$line). To detect fields with empty strings I use  if (($line_split[0] eq "") {...}. From this I always get the "use of uninitialized value" warning. I´m not sure if this is a problem, but at least it spoils the output.. What am I doing wrong / how can I avoid that? Cheers, Michael

Replies are listed 'Best First'.
Re: "Use of uninitialized value" due to empty elements in an array
by chuckbutler (Monsignor) on Mar 31, 2010 at 14:14 UTC

    The defined function is what you need. It returns true if the value being tested IS NOT the special undef value. So something like:

    unless (defined($line_split[0])) {...} ...

    ahead of your other tests should get the job done.

    Update: Fixed typo..

    Good luck. -c

Re: "Use of uninitialized value" due to empty elements in an array
by roboticus (Chancellor) on Mar 31, 2010 at 14:32 UTC

    michaelp:

    If you simply want to turn undefined elements into an empty string, you could add a step to perform the conversion immediately after the split, like so:

    @line_split = map { defined($_) ? $_ : '' } split (/\t/, $line);

    ...roboticus

Re: "Use of uninitialized value" due to empty elements in an array
by ikegami (Patriarch) on Mar 31, 2010 at 15:27 UTC
    Tell split not to remove empty trailing fields.
    my @line_split = split(/\t/, $line, -1);
    And if you want, you can check if you got enough fields
    if (@line_split != 3) { die("Not enough fields in line $.\n"); }
      Thanks for all the help!
Re: "Use of uninitialized value" due to empty elements in an array
by JavaFan (Canon) on Mar 31, 2010 at 15:06 UTC
    $line_split[0] will only be undefined if $line doesn't contain any other character than \t (including being empty). And the fact it's undefined stems from @line_split not having any entries. So, you may do something like:
    if (@line_split && $line_split[0] eq "") { # Has empty leading fields }
    or
    if (!@line_split || $line_split[0] eq "") { # Has empty leading fields, or no fields at all }
    depending on what you want to do if there are no fields at all.
Re: "Use of uninitialized value" due to empty elements in an array
by GrandFather (Saint) on Mar 31, 2010 at 19:57 UTC

    'tab-delimited file' translates to CSV file (comma separated files are common) and there are standard ways to handle such beasts. It may well be that your files are trivial and split will work fine, but in the general case there are many nasty edge cases to take care of with such files. Take a look at Text::CSV and Text::xSV.


    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://832047]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found