Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This warning seems quite clear to me. It says that you are doing a string comparison with an undefined value. You are only doing one string comparison in this code (and I assume that's on line 9). The comparison looks like this:

$need[$incr] eq ''

The empty string isn't undefined, so it must be that the value in $need[$incr] becomes undefined at some point. This is almost certainly caused by your unconventional looping mechanism. I think that you think that once you go past the end of the @need array then your string comparison will be true and your loop will exit. And that is what is happening, but when you go past the end of an array, the next (non-existant) element is "undef", not the empty string. Now, in a string comparison, "undef" is converted to the empty string, but that conversion isn't silent and you'll get that warning to tell you what Perl has done.

To avoid the warning, check that $needs[$incr] is defined, not that it is an empty string. Actually this whole approach is a little fragile as an array can contain undefined elements (and empty strings) and your loop could finish early. You might be better off using a foreach loop to iterate across the array.

Perl's warning and error messages are generally far clearer that the ones that you get from many other languages. If you read them carefully then you can usually work out what they are trying to tell you. If you need more help, then look at the perldiag manual page or add use diagnostics to your program (but remove it again once the program is finished).

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg


In reply to Re: Why "use of uninitialized value" warning comes? by davorg
in thread Why "use of uninitialized value" warning comes? by sanjay nayak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2022-09-24 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (115 votes). Check out past polls.

    Notices?