How to distinguish between "0" and NULL in perl? -
here looking string "reftext" in given file. line next contains string 3 integers. extracting them in @all_num. printing value of @all_num[2] if not null. logic used here doesn't print @all_num[2] if has 0.
#!/usr/bin/perl open( readfile, "<myfile.txt" ); @list = <readfile>; $total_lines = scalar @list; ( $count = 0; $count < $total_lines; $count++ ) { if (@list[ $count =~ /reftext/ ) { @all_num = @list[ $count + 1 ] =~ /(\d+)/g; if ( @all_num[2] != null ) { print "@all_num[2]\n"; } } }
perl not include null, line
if(@all_num[2]!= null) is nonsensical in perl. (more accurately, attempts locate sub named null , run value compare against @all_num[2], fails because (presumably) haven't defined such sub.) note that, if had enabled use strict, cause fatal error instead of pretending work. 1 of many reasons always use strict.
side note: when pull value out of array, it's single value, should $all_num[2] rather @all_num[2] when referring third element of array @all_num. (yes, little confusing used to. hear it's been changed in perl 6, i'm assuming you're using perl 5 here.) note that, if had enabled use warnings, have told "scalar value @all_num[2] better written $all_num[2]". 1 of many reasons always use warnings.
if want test whether $all_num[2] contains value, proper way express in perl is
if (defined $all_num[2])
Comments
Post a Comment