Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

use of uninitialized variables??

by vnpandey (Scribe)
on Jul 07, 2000 at 12:26 UTC ( [id://21425]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: use of uninitialized variables??
by ZZamboni (Curate) on Jul 07, 2000 at 17:26 UTC
    Dear vnpandey,

    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

      Dear ZZamboni, Thanks!! I'll use sumbit withincodes from now onwards my probs..as far as code being long is concerned As I had seen longer codes as queries on the sites(this is some 60-65 lines while there are codes about 150 lines etc..) so I did not think of it..But Of course I do accept that I could have made the problem formatted & less lengthy(which would have been wiser for me)...I'll do this from next time...so pls excuse me........ V.N.Pandey
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/>
Re: use of uninitialized variables??
by turnstep (Parson) on Jul 23, 2000 at 19:22 UTC

    Your problem is with the line that says:

    @val3=grep(/\d{1,2}$comparison/,@complex);

    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:

    chop($val3[0]); $val4=$val1[0]+24-$val3[0];

    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

    &Get_Calibrator_Filename();

    to the top, and got a lot of unitialized value errors. Fair enough, after all, the subroutine does require some input. Instead of using:

    $calib1=$_[0]; $comparison=$_[1];

    you should use something like this, which is not only cleaner, but enforces that at least two arguemnts are sent to the subroutine:

    $calib1 = shift or die "Missing first argument to sub GCF!\n"; $comparison = shift or die "Missing second argument to sub GCF!\n";

    So, I plugged in two random variables:

    &Get_Calibrator_Filename("foo", "bar");

    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:

    chop($val1[0]);chop($val2[0]);chop($val3[0]);

    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. :)

RE: use of uninitialized variables??
by barndoor (Pilgrim) on Jul 07, 2000 at 12:40 UTC
    Sorry, but with the code formated like this it's almost impossible to work out what each line contains to get to the line numbers your errors refer to. Can you resubmit with the code markers. See 'Site How To' for details.
RE: use of uninitialized variables??
by t0mas (Priest) on Jul 07, 2000 at 19:02 UTC
    Try running your program in debug mode and step trough it. Watch wich variable is undefined at the time you get the warning.
    Refer to perldoc perldebug for instructions on how to use the built in debugger.

    /brother t0mas
Re: use of uninitialized variables??
by c-era (Curate) on Jul 07, 2000 at 15:30 UTC
    Let's see if this works

    Dear Monks, I am calling a subroutine in my perl programme, the subroutine is:

    sub Get_Calibrator_Filename{ my(@complex,$temp,$calib1,@val1,@val2,$tempx,$tempy,@tempxy,@val3,$cal +ib2,$comparison,$val4,$val); #local($tempxy[0],$val2[0],$val1[0],$val3[0]); @complex=("1a","2b","3c","4d","5e","6f","7g","8h","9i","10j","11k","12 +l","13m","14n","15o","16p","17q","18r","19s","20t","21u","22v","23w", +"24x"); $calib1=$_[0]; $comparison=$_[1]; for ($temp=0;$temp<=3;$temp++) { chop($calib1); } $tempx=chop($calib1); $tempy=chop($calib1); @val1=grep(/\d{1,2}$tempx/,@complex); @val2=grep(/\d{1,2}$tempy/,@complex); @val3=grep(/\d{1,2}$comparison/,@complex); chop($val1[0]);chop($val2[0]);chop($val3[0]); if ($val1[0] <$val3[0]) { $val4=$val1[0]+24-$val3[0]; } else { if($val1[0]>$val3[0]) { $val4=$val1[0]-$val3[0]; } } if ($val2[0] > $val4) { $val=$val2[0]-$val4; @tempxy=grep(/$val\S/,@complex); $tempxy[0]=chop($tempxy[0]); $tempxy[0]=$tempxy[0].$comparison; $calib2=$calib1.$tempxy[0]."1.1d"; } else { print "VAL1=",$val1[0]," VAL2=",$val2[0],"VAL3=",$val3[0],"\n"; if ($val1[0]<$val3[0]) { $val4=$val3[0]-$val1[0]; } else { if($val1[0]>$val3[0]) { $val4=24-$val1[0]+$val3[0]; } } $val=$val2[0]+$val4; @tempxy=grep(/$val\S/,@complex); $tempxy[0]=chop($tempxy[0]); $tempxy[0]=$tempxy[0].$comparison; $calib2=$calib1.$tempxy[0]."1.1d"; } print "\n filegot=$_[0] \n fileout=$calib2\n"; return ($calib2); }
    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 ..
    Use of uninitialized value at checkdataperl.pnew1 line 16, <STDIN> chu +nk 1162. Use of uninitialized value at checkdataperl.pnew1 line 29, <STDIN> chu +nk 1162. Use of uninitialized value at checkdataperl.pnew1 line 39, <STDIN> chu +nk 1162. Use of uninitialized value at checkdataperl.pnew1 line 51, <STDIN> chu +nk 1162.
    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.

Re: use of uninitialized variables??
by agoth (Chaplain) on Jul 07, 2000 at 16:08 UTC
    where is the node for:
    -> 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?
(jeffa) Re: use of uninitialized variables??
by jeffa (Bishop) on Jul 07, 2000 at 16:58 UTC
    didn't mean to post, sorry

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found