in reply to Why is it uninitialized?

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.

use 5.012.02; use strict; use warnings; my %base_stat; my @temp_array; my $basecount; my @string; my @string_array = qw/this that and all manner of other/; my $pos; my $pos_score; 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"; } } say "\%base_stat contains:"; foreach my $key ( sort keys %base_stat ) { say "$key => $base_stat{$key}"; }

And the output:

Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. Use of uninitialized value $temp_array[0] in join or string at test.pl + line 26. %base_stat contains: 1 => 83 83 64 64 76 78 78 2 => 71 71 77 75 64 69 83 3 => 72 64 67 75 77 71 4 => 82 83 77 68 5 => 68 81 6 => 81

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?

use 5.012.02; use strict; use warnings; my @input_string_array = qw/this that and all manner of other/; my @output_array; foreach my $string ( @input_string_array ) { push @output_array, join " ", map { ord($_) - 33 } split //, $stri +ng; } say $_ for @output_array;

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:

use 5.012.02; use strict; use warnings; my @input_string_array = qw/this that and all manner of other/; my %output_hash; foreach my $iterator ( 0 .. $#input_string_array ) { $output_hash{ $iterator + 1 } = join " ", map { ord($_) - 33 } spl +it //, $input_string_array[$iterator]; } foreach my $key ( sort keys %output_hash ) { say "$key => $output_hash{$key}"; }

Dave

Replies are listed 'Best First'.
Re^2: Why is it uninitialized?
by yehudithasin (Novice) on Apr 03, 2011 at 18:40 UTC
    Hi Dave

    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

    my @string_array=qw/this that and all manner of other/; ..... %ouput_hash=( '1'=>("t","t","a","a","m","o","o"), '2'=>("h","h","n","l","a","f","t"), ...... )

      :) I'm glad I hedged with "you might have your reasons." You do.


      Dave