At 5:48 AM -0500 2/6/10, Chris Coggins wrote:

1. another script uses data passed into it from an html form. Some of the fields are empty, and my "print report file" subroutine prints the empty variables because I don't know how to filter them out, and they need to be removed from the report. Can someone give me the perfectly formed "if" statement that lets me skip over the one or two empty variables during the print action? The data being printed to the file is stored in an array. See code below.

print FILE ("Report summary: $taskarray[0] \n"); #this value is good
print FILE ("Data for task 3: $taskarray[3] \n"); #this value is good
print FILE ("Data for task 7: $taskarray[7] \n"); #this value is empty, don't print this data print FILE ("Data for task 2: $taskarray[2] \n"); #this value is empty, don't print this data print FILE ("Data for task 11: $taskarray[11] \n"); #this value is good but needs to be modified, see #2 below

If a scalar variable is empty, i.e. is either undef or an empty string '', it will have a false value in a logical expression. Therefore, you can test the value of the variable and only print it if it is true:

  if( $string ) {
    print "$string\n";
  }

or the shorter equivalent:

  print "$string\n" if $string;


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to