vnpandey has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: use of uninitialized variables??
by ZZamboni (Curate) on Jul 07, 2000 at 17:26 UTC | |
Just yesterday (RE: Before You Post ...) you promised you would not do this again. Namely: posting a very long piece of code in the expectation that the monks will debug it for you. While some may go through the trouble, you will have much better chances of having your question answered if you do your homework first by really trying to figure out what is wrong, and trying to reduce the problem to its minimum form before posting. Also, once again, please use <code> tags when submitting code. --ZZamboni | [reply] |
by vnpandey (Scribe) on Jul 10, 2000 at 12:18 UTC | |
| [reply] |
|
Re: use of uninitialized variables??
by davorg (Chancellor) on Jul 07, 2000 at 12:48 UTC | |
Difficult to know what's going on with code formatted like that. Please use the code tags - just add <code> and </code> before and after your code fragments. However, Perl is telling you that on lines 16, 29, 39 and 51 of your program you are trying to access a variable which you haven't previously given a value. It's difficult to help any more as we can't see which lines they are in the fragment which you have posted. --<http://www.dave.org.uk> European Perl Conference - Sept 22/24 2000, ICA, London <http://www.yapc.org/Europe/> | [reply] |
|
Re: use of uninitialized variables??
by turnstep (Parson) on Jul 23, 2000 at 19:22 UTC | |
Your problem is with the line that says:
Since $comparison is passed in as the second argument to the subroutine, grep may not find any matches (which will happen when the second argument is a not a single lower-case letter from 'a' to 'x'). When no matches are found, @val3 is still undefined - in otherwords, it is empty. Therefore, any further actions using it, such as:
will cause perl to complain (if -w is set) about "use of uninitalized value", which is perl's way of saying: "Hey - how I can do anything with this value if you haven't even told me what it is yet?!" Let me show you one way to track down errors like these, to help you out next time. (This is roughly what I did). First, I ran the script with a -w. This seems to work, as it did for c-era, but only because we have defined a subroutine but not actually *used* it. So, next I added a
to the top, and got a lot of unitialized value errors. Fair enough, after all, the subroutine does require some input. Instead of using:
you should use something like this, which is not only cleaner, but enforces that at least two arguemnts are sent to the subroutine:
So, I plugged in two random variables:
When ran, it produced far fewer errors, about the same as the number you reported. I then looked at the very first error reported, in other words, the first place in the script that the unititalized value appeared. In this case, it was the line:
Breaking it into three lines revealed that it was the final statement that was causing the problem. Then I looked back to find where @val3 was set, and it was in the grep statement above. The grep statement used two variables, $comparison and @complete. The latter is hard coded, but the former is based on the input to the subroutine - bingo! Then, after looking at @complete, I changed the second argument to the subroutine to "f" instead of "foo" - no more errors. Having some minor error checking throughout the program would prevent problems like this. Check to see whether or not grep returned anything, for example. I'll leave that as the proverbial "exercise for the reader", since this post has gone on long enough. :) | [reply] [d/l] [select] |
|
RE: use of uninitialized variables??
by barndoor (Pilgrim) on Jul 07, 2000 at 12:40 UTC | |
| [reply] |
|
RE: use of uninitialized variables??
by t0mas (Priest) on Jul 07, 2000 at 19:02 UTC | |
Refer to perldoc perldebug for instructions on how to use the built in debugger. /brother t0mas | [reply] |
|
Re: use of uninitialized variables??
by c-era (Curate) on Jul 07, 2000 at 15:30 UTC | |
Dear Monks, I am calling a subroutine in my perl programme, the subroutine is: While running the programme it gives me the following warnings (although the results are as expected...)the line numbers I refer here are as perl the line no of the subroutinecode with the first line as from where sub starts .. It doen't helps even if I include in my($val10,$val20.etc...) Thanks, V.N.Pandey This is c-era: I ran the program with strict and -w and recieved no errors. You may want to try setting all of your variables to 0 or null in you my statment. | [reply] [d/l] [select] |
|
Re: use of uninitialized variables??
by agoth (Chaplain) on Jul 07, 2000 at 16:08 UTC | |
-> take seeker to stocks and throw rotten tomatoes until he repents and useth code tags or -> push this repeat seeker-of-unformatted-wisdom off highest point of monastery and cheer? | [reply] |
|
(jeffa) Re: use of uninitialized variables??
by jeffa (Bishop) on Jul 07, 2000 at 16:58 UTC | |
| [reply] |