develooper Front page | perl.beginners | Postings from February 2010

Re: 1-line datafile, need data for another routine

Thread Previous | Thread Next
From:
Jim Gibson
Date:
February 6, 2010 13:03
Subject:
Re: 1-line datafile, need data for another routine
Message ID:
p06240800c793889b7b14@[192.168.1.3]
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;


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About