Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

passing data to variable in if statment

by stuffy (Monk)
on May 06, 2008 at 20:52 UTC ( [id://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

Replies are listed 'Best First'.
Re: passing data to variable in if statment
by FunkyMonk (Chancellor) 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
Re: passing data to variable in if statment
by Paladin (Vicar) on May 06, 2008 at 21:00 UTC
    Use defined $var to see if a variable is defined or not. "" is different from undef

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found