yehudithasin has asked for the wisdom of the Perl Monks concerning the following question:
I have a foreach loop, to which I pass a bunch of strings hoping to create an array of values for each position in the string, and keep all that in a hash. All the variables are declared using my at the beginning of the sub. The code works, but it gives an "Use of uninitialized value $temp_array[0] in join or string at quality_cutoff.pl line 124" error. Why?
foreach (@string_array){ @string=split(//,$_); $basecount=0; foreach $pos (@string){ $basecount++; $pos_score=ord($pos)-33; @temp_array=$base_stat{$basecount}; # line 124 push (@temp_array,$pos_score); $base_stat{$basecount}="@temp_array"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why is it uninitialized?
by davido (Cardinal) on Apr 03, 2011 at 07:38 UTC | |
What does %base_stat contain before entering the first iteration of the loop? If %base_stat contains nothing, your first iteration of the loop will be performing operations on @temp_array when it too contains nothing. Perl is warning you that you're making a mistake. See the following snippet that should clarify for you.
And the output:
This snippet more or less constructs the state your code experiences on the first iteration of the loop. I'm surprised you're not getting a warning two lines later in the script too, where you wrap @temparray in quotes. On your subsequent loop iterations %basestat and @temparray contain values, and so you no longer are attempting to use the values of uninitialized variables, so your warnings go away. I think if you dig a little deeper you'll find that there's a better way to do what you're trying to do. Dave | [reply] [d/l] [select] |
by yehudithasin (Novice) on Apr 03, 2011 at 19:24 UTC | |
works better:) | [reply] [d/l] |
|
Re: Why is it uninitialized?
by wind (Priest) on Apr 03, 2011 at 07:43 UTC | |
You %base_stat hash is not defined initially for each key $basecount. Therefore your @temp_array equals (undef) the first time, and when you include it in the "@temp_array" string, it throws a warning. It actually looks like you want an array for the hash values anyway. Also, it's good that you're declaring all of your variables with my, but you should aim to always limit the scope of your variables to the highest point possible. Observe the following code and see if it is doing what you intend:
| [reply] [d/l] |
|
Re: Why is it uninitialized?
by davido (Cardinal) on Apr 03, 2011 at 08:22 UTC | |
I keep squinting at your code and wondering how it is that you could say it works. If I give it a seven element array, it returns a hash with six elements. None of the values actually correspond directly to a set of numbers that directly resembles the input string. Take the following example I gathered using your code.
And the output:
Is that really what you want? Look at how poorly the output corresponds to the input strings. There's not a direct 1:1 correlation of string characters to values on a per-input-line basis. If all you want is a set of values corresponding to an array of input strings, how about this?
Or if you really need a hash that is indexed like an array but starting with an index of '1', (all of which seems silly to me, but you might have your reasons), you could do it like this:
Dave | [reply] [d/l] [select] |
by yehudithasin (Novice) on Apr 03, 2011 at 18:40 UTC | |
Thank you - but you seem just to translate all my strings, while i really need the silly thing...:) -i need a hash that will contain the array of values in x position in all the strings, so if the values were just letters I would need the result to look something like
| [reply] [d/l] |
by davido (Cardinal) on Apr 04, 2011 at 03:12 UTC | |
:) I'm glad I hedged with "you might have your reasons." You do. Dave | [reply] |
|
Re: Why is it uninitialized?
by Anonymous Monk on Apr 03, 2011 at 07:51 UTC | |
FWIW, you're supposed to aim for smallest scope necessary, you'll develop this skill with practice :)(Tutorials: Variables and Scoping) Compare Silenced, Loudmouth, and Smooth (always strive for Smooth)
Questions? | [reply] [d/l] |