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

hi, I am returning the ticker symbol and mkt. cap for stocks, and the relevant code is;
my @symbol=('AAXJ','MCO'); my @quotes = getquote(@symbol); foreach my $q (@quotes) { my @h = @{$q}; ($h[$i] eq 'N/A') ? 0 : $h[$i]; print "ticker is $h[0]\n"; print "mkt cap is $h[1]\n";
however, the if-else is not working,and the mkt. cap continues to show N/A. Can you please help me fix it...thx!

Replies are listed 'Best First'.
Re: N/A replacement not working
by toolic (Bishop) on Aug 10, 2011 at 15:39 UTC
    the if-else is not working
    There is no if-else in the code you posted.

    Use warnings and fix the problem:

    use warnings; use strict; use diagnostics; my $i = 0; my @h = 0 .. 3; ( $h[$i] eq 'N/A' ) ? 0 : $h[$i]; __END__ Useless use of array element in void context at (W void) You did something without a side effect in a context that + does nothing with the return value, such as a statement that doesn't re +turn a value from a block, or the left side of a scalar comma operator. +Very often this points not to stupidity on your part, but a failure of +Perl to parse your program the way you thought it would. For example, +you'd get this if you mixed up your C precedence with Python precedence +and said $one, $two = 1, 2; when you meant to say ($one, $two) = (1, 2); Another common error is to use ordinary parentheses to construct a + list reference when you should be using square or curly brackets, for example, if you say $array = (1,2); when you should have said $array = [1,2]; The square brackets explicitly turn a list value into a scalar val +ue, while parentheses do not. So when a parenthesized list is evaluat +ed in a scalar context, the comma is treated like C's comma operator, wh +ich throws away the left argument, which is not what you want. See perlref for more on this. This warning will not be issued for numerical constants equal to 0 + or 1 since they are often used in statements like 1 while sub_with_side_effects(); String constants that would normally evaluate to 0 or 1 are warned about.
Re: N/A replacement not working
by blue_cowdawg (Monsignor) on Aug 10, 2011 at 15:15 UTC

    that looks suspicious to me... I'm thinking what you really wanted was:

    $h[$i] = ($h[$i] eq 'N/A') ? 0 : $h[$i];
    But having typed that just now I have a question as to what has been assigned to $i? Either your code snippet is woefully incomplete or ... ??? Did you use strict;?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      $i is an int. the print statement works fine as I've shown in the code snippet, and I have used strict. I'm not sure how your if-else is different from mine....nevertheless, I tried yours and it still returns N/A

        The difference is that his if-else actually does something to the data while your if-else is practically a no-op (i.e. it does nothing except testing the values)

        Are you sure that what you see as 'N/A' on the screen is not really "N/A\n" or ' N/A '? Also you test $h[$i], but print $h[1].

        The ($h[$i] eq 'N/A') ? 0 : $h[$i]; is working ok. The question is what do you want to do with this? As of now the result will be lost ... You need to save it into some var and act on it or print it.

        Which one of the prints returns the N/A? If it's the first one $h[0] = ($h[$i] eq 'N/A') ? 0 : $h[$i]; otherwise $h[1] = ($h[$i] eq 'N/A') ? 0 : $h[$i];