Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

passing data to variable in if statment

by stuffy (Monk)
on May 06, 2008 at 20:52 UTC ( #685073=perlquestion: print w/ replies, xml ) Need Help??
stuffy has asked for the wisdom of the Perl Monks concerning the following question:

It's been a long time since I tried programming, and I'm having some problems. $gust is declared earlier and passed a value from geo::METAR. If there is no wind gust, then no value is passed. I check to see if it's nul, it if is, I want to do something, if it has a value I want to do something else. I'm getting a Use of uninitialized value in concatenation (.) or string at.... error when trying to call $gusting when $gust has no value. It seems like I'm missing something simple here!
my $wind = $m->WIND_MPH; my $w_dir = $m->WIND_DIR_ENG; my $gust = $m->WIND_GUST_MPH; my $gusting; if ($gust eq "") { $gusting ="not gusting"; } else { $gusting ="gusting to $gust"; } print "$gusting";


Stuffy
That's my story, and I'm sticking to it, unless I'm wrong in which case I will probably change it ;~)
may be reproduced under the SDL

Comment on passing data to variable in if statment
Download Code
Re: passing data to variable in if statment
by Paladin (Priest) on May 06, 2008 at 21:00 UTC
    Use defined $var to see if a variable is defined or not. "" is different from undef
Re: passing data to variable in if statment
by FunkyMonk (Canon) on May 06, 2008 at 21:02 UTC
    Are you sure WIND_GUST_MPH contains the empty string and not undef? In either case, you can use
    if ($gust) { $gusting ="gusting to $gust"; } else { $gusting ="not gusting"; }


    Unless I state otherwise, all my code runs with strict and warnings
      You are correct, it is not an empty string. Your fix did the trick. I thought about switching it around, but I thought that what I had was correct, and would have been if an empty string was being passed. Thank you for your help.


      Stuffy
      That's my story, and I'm sticking to it, unless I'm wrong in which case I will probably change it ;~)
      may be reproduced under the SDL

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://685073]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-07-29 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (218 votes), past polls