Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Null in Array

by Pyramid (Initiate)
on Jul 29, 2003 at 01:55 UTC ( [id://278688]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I am new to perl scripting. I am having a problem when there is a null in my array. The array is created from a text file and sometimes the field (place holder 5 in the array) is blank. When I use the length function on the blank $day[5] Perl returns nothing. The ELSE part of the IF statement does not work. It returns nothing instead of printing "00:00:00". I have tried a number of things and still do not get it. Thank you for your help.

$len = length($day[5]); if ($len =~ /8/) { print "$day[5]"; } else { print "00:00:00"; }

Edit: Added code tags. Minor html formatting. larsen

Replies are listed 'Best First'.
Re: Null in Array
by Zaxo (Archbishop) on Jul 29, 2003 at 02:20 UTC

    If, by 'null' and 'blank', you mean undef, note that length undef is zero. That's also true of the empty string, ''. This may work for you,

    if (length $day[5]) { print $day[5]; } else { print '00:00:00'; }
    or just, print length($day[5])? $day[5]: '00:00:00'; See trinary op, ?:, in perlop.

    After Compline,
    Zaxo

      Thank you for your help! I guess that I was not using the undef correctly? Now, when the filed in the array is blank "00:00:00" is put into the array. My code now works and looks like this:
      #!/usr/bin/perl -w use DBI; my $dbh = DBI->connect('DBI:ODBC:PERL', {AutoCommit => 1},) or die "Couldn't connect to database: " . DBI->errstr; $log = print "Enter FS log name> "; chomp($log); open($LogDate, ">>LogDate.txt"); while ($log = <>) { open(DATA,$log); @file = <DATA>; close(DATA); foreach $line (@file) { $line =~ s/"//g; @day = split(/ /,$line); $func = shift @day; if ($func =~ /^Initiated|^Execution/) { if ($day[2] =~/on/) { chomp($log); print "\n$log,$day[3],$day[1],"; print $LogDate "\n$log,$day[3],$day[1],"; } if ($day[2] =~ /terminated/) { print "$day[5]"; print $LogDate "$day[5]"; } } } close($LogDate); open(DATA,"LogDate.txt"); @file = <DATA>; close(DATA); foreach $line (@file) { chomp($line); @array = split(/,/,$line); ########## This is where the problem was ########## if (length $array[3]) { # do nothing } else { $array[3] = '00:00:00'; } ################################################## $dbh->do("INSERT INTO tblLogDate (Log,Dte,Start,Stop)values('$ +array[0]','$array[1]','$array[2]','$array[3]')"); } $dbh->disconnect; exit }
Re: Null in Array
by sgifford (Prior) on Jul 29, 2003 at 05:48 UTC
    The code you have works properly on undef values:
    #!/usr/bin/perl use vars qw(@day); $day[5] = ''; printday(); $day[5]="\n"; printday(); $day[5]=undef; printday(); sub printday { my $len = length($day[5]); if ($len =~ /8/) { print "$day[5]"; } else { print "00:00:00"; } print "\n"; }
    outputs:
    00:00:00
    00:00:00
    00:00:00
    
    as expected. So there error is likely to be elsewhere in your code.

    Using $len =~ /8/ to see if the length is 8 is very strange. It says "if the string representation of the length contains the digit 8", so will match 8, 18, 3008, 8000000, etc. You probably just want $len == 8.

      Thank you for your suggestion to check for errors elsewhere in my code. I did that and now it works!
Re: Null in Array
by bobn (Chaplain) on Jul 29, 2003 at 02:57 UTC

    If the value in $day[5] is a string of spaces 8 characters long, that is what will print out. Any chance of that happening?

    A little more code would have been helpful.

    --Bob Niederman, http://bob-n.com
Re: Null in Array
by mpd (Monk) on Jul 29, 2003 at 02:22 UTC
    Checking the length seems to be a waste. Just test the variable directly w/ a regexp. I might do something closer to:
    print (($day[5] =~ /^\d{2}:\d{2}:\d{2}$/) ? $day[5] : '00:00:00');
    NB: TMTOWTDI. Code is untested. Regexp used may not be correct, but it's tough to tell without context.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found