Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Null value in print string (http://www.velocityreviews.com/forums/t882937-null-value-in-print-string.html)

David G Anderson 10-05-2003 02:57 AM

Null value in print string
 
I wish to protect against a null value in the print string below:

foreach my $result (@{$response->{Details}}) {
print
join "\n",
$result->{ProductName}[0]||"no title",
$result->{Asin}[0] . "\t" .
$result->{ListPrice}[0] . "\n\n";
}

Error message when ListPrice is null:

"Use of uninitialized value in concatenation (.) or string at ... "

David Anderson

Tad McClellan 10-05-2003 03:25 AM

Re: Null value in print string
 
David G Anderson <anderson@glen-net.ca> wrote:
> I wish to protect against a null value in the print string below:
>
> foreach my $result (@{$response->{Details}}) {



$result->{ListPrice}[0] = '' unless defined $result->{ListPrice}[0];


> print
> join "\n",
> $result->{ProductName}[0]||"no title",
> $result->{Asin}[0] . "\t" .
> $result->{ListPrice}[0] . "\n\n";
> }
>
> Error message when ListPrice is null:
>
> "Use of uninitialized value in concatenation (.) or string at ... "



That is a *warning* message, not an error message.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Eric J. Roode 10-05-2003 11:56 AM

Re: Null value in print string
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

anderson@glen-net.ca (David G Anderson) wrote in
news:ea22dbc.0310041857.31e461a5@posting.google.co m:

> I wish to protect against a null value in the print string below:
>
> foreach my $result (@{$response->{Details}}) {
> print
> join "\n",
> $result->{ProductName}[0]||"no title",
> $result->{Asin}[0] . "\t" .
> $result->{ListPrice}[0] . "\n\n";
> }


You need more parentheses. Check the precedence of "||" and ".".

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP4AG2GPeouIeTNHoEQJLJQCeOzPRuL88PDNQtICUOCTMC7 2ICcgAoMRN
L6yKkfLOip2AyVA8UEYhNha/
=EEIU
-----END PGP SIGNATURE-----


All times are GMT. The time now is 01:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.