in reply to Re^2: Initialize or Match undef without warning
in thread Initialize or Match undef without warning

# 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?

Replies are listed 'Best First'.
Re^4: Initialize or Match undef without warning
by stevieb (Canon) on Dec 15, 2015 at 00:13 UTC

    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.