Null value in print string

Discussion in 'Perl Misc' started by David G Anderson, Oct 5, 2003.

  1. 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
    David G Anderson, Oct 5, 2003
    #1
    1. Advertising

  2. David G Anderson <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 5, 2003
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (David G Anderson) wrote in
    news::

    > 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/AwUBP4AG2GPeouIeTNHoEQJLJQCeOzPRuL88PDNQtICUOCTMC72ICcgAoMRN
    L6yKkfLOip2AyVA8UEYhNha/
    =EEIU
    -----END PGP SIGNATURE-----
    Eric J. Roode, Oct 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    7,283
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,486
    Mike Schilling
    Mar 29, 2006
  3. Col
    Replies:
    1
    Views:
    610
    peter koch
    Apr 21, 2006
  4. keto
    Replies:
    0
    Views:
    897
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    335
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page