Вопрос

I am inserting a csv file into MySQL using Perl. I have successfully inserted some data into MySQL. However, some fields in the csv file are null and the code cannot continue to run.

What is the correct way to express if statement to check if $rttjitter is not null, else print that row number, then execute?

i.e. 2 criteria: $rtt is not equal to 0 and $rttjitter is not null.

my $empty =  q{};

my @nulls = ();

if ($rtt !=0 && $rttjitter ne undef)

^ These do not work quite well.

Это было полезно?

Решение

You can use the built-in defined function to explicitly check if a variable is defined, so e.g.

if( defined( $rttjitter ) and $rttjitter ) { ...

Will check that it's defined and has a value that is not 0 or an empty string.

It seems likely though you should take another look at how you're reading in your CSV (you are using a module like Text::CSV, right?), and/or how you're doing the DB insertion, as generally with this sort of thing the undef value will cleanly insert as a NULL into the DB and should not cause any problems with your code, unless you're doing something strange.

Другие советы

if (defined($rttjitter) && defined($rtt) && $rtt != 0)
{
    ... # $rttjitter is not null (undef) and $rtt is not null (undef) and nonzero
}
else
{
    ... # at least one column fails the test
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top