in reply to uninitialized values

G'day gaurav,

Your problem line is this:

my (@values1,@val1,@values2,@val2,$val) = 0;

While that declares all those variables, it only assigns 0 to @values1; the other four variables are all undefined. $var remains undefined and the other three arrays are still empty. [Update on that last clause thanks to AnomalousMonk.]

If you're assigning values to the arrays elsewhere in your code (you only show assignments to @values1 and @val1 here), then you can do this:

my (@values1, @val1, @values2, @val2); my $val = 0;

That's a guess because you haven't shown enough of your code!

Another separate problem is your choice of variable names. The names convey no meaningful information whatsoever (all variables can hold values) and, because they are so similar, they are highly error prone (for you, the developer) and highly problematic for any maintainer (whether you or someone else). I strongly recommend you change them to something meaningful.

-- Ken

Replies are listed 'Best First'.
Re^2: uninitialized values
by gaurav (Sexton) on Aug 19, 2013 at 13:38 UTC

    Thanks Ken for pointing me out the faults I have committed,I would keep in mind.But then how can, I get those values outside If - block.And another thing is, that those variable names are only for testing purpose ,I just wanted to check that whatever I have been doing there is write or wrong .I will give them a meaningful name once I am done with it

      The warning you are getting is about $val. It's telling you that variable is uninitialised on line 69 in some concatenation or string operation.

      The warning has nothing to do with either of the print statements you show. I suspect you're already being tripped up by your poorly named variables! The first print statement has $values1[1], that's the second element (index 1) of the @values1 array; the other print statement has $val1[0], that's the first element (index 0) of the @val1 array. Except for the declaration of $val (i.e. my (..., $val) = 0;), that variable appears nowhere in the code you have posted.

      Please read "How do I post a question effectively?" then, following its guidelines, produce a minimal script that reproduces your problem. In creating a minimal script, you may well get some insights into whatever your problem is and be able to solve it yourself; if not, then post the minimal script that reproduces your problem and we can look at it further.

      "I will give them a meaningful name once I am done with it"

      Hopefully, it's now obvious to you that that's the wrong way to do it. Furthermore, consider honestly whether — once your code is written, tested and functioning correctly — you would actually go back and rewrite it. [That's something for you to consider privately to yourself: it's not a question I want an answer to here.]

      Give them meaningful names at the outset: write your code well once, then spend the remaining time on something more interesting than rewiting your code a second time. You're making a rod for your back if you do otherwise.

      -- Ken

        I will keep these things in mind.Sorry for it.Thanks