Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Initialize or Match undef without warning

by u65 (Chaplain)
on Dec 14, 2015 at 22:35 UTC ( [id://1150298]=note: print w/replies, xml ) Need Help??


in reply to Initialize or Match undef without warning

I'm not sure I understand your exact problem but, assuming your $row ref comes from, say, Text::CSV, I would loop through existing fields (or whatever number of fields you want for the copied extended row), testing each for being defined. If a field is not defined, set it to the empty string, otherwise, extract or copy what you need.

It would help greatly to see your program and some sample CSV data.

Update: You should be able to set a non-existent cell in an array reference like this:

$row->[15] = '';

Replies are listed 'Best First'.
Re^2: Initialize or Match undef without warning
by JobC (Acolyte) on Dec 14, 2015 at 23:30 UTC

    That! Solved the problem.

    It also created a problem. I had remarked out the assignment later on in the code. After adding it back in. It has made some of the fields undefined again. I think I need an OR in the extract regex now.

    # this initializes the field correctly $row->[15] = ''; # This overwrites the empty field with undefined if it doesn't match. ($row->[15]) = $row->[5] =~ /(#[^ ]+)/;
      # I changed this $row->[15] = ''; ($row->[15]) = $row->[5] =~ /(#[^ ]+)/; # to this and it does what I want $row->[15] = ''; if ($row->[5] =~ /#[^ ]+/) { ($row->[15]) = $row->[5] =~ /(#[^ ]+)/; }

      Is there a better, more compact way to do this that is still easy to read?

        replace:

        $row->[15] = ''; if ($row->[5] =~ /#[^ ]+/) { ($row->[15]) = $row->[5] =~ /(#[^ ]+)/; }

        with (untested):

        ($row->[15]) = $row->[5] =~ /(#[^ ]+/ ? $row->[5] : '';

        more readable, logically? same line as above, but split across a few lines:

        ($row->[15]) = $row->[5] =~ /(#[^ ]+/ ? $row->[5] : '';

        ternary (aka. conditional) operator.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-16 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found