I wrote a subroutine to create a hash with numbers of transitions between states makred by binary codes in string. Length of codes is passed by parameter $rank and the second parameter is the string to analize. That is the code:
sub get_prob{
my ($file_content,$rank)=@_;
my %hash;
my %row;
$start_portion=get_portion_of_string($file_content,0,$rank);
$actual_portion=$start_portion;
$hash{$actual_portion}->{$actual_portion}=0;
$i=1;
$size=length($file_content);
while($i+$rank<=$size){
$next_portion=substr($actual_portion,1,$rank-1);
$next_portion.=substr($file_content,$i+$rank-1,1);
if (exists $hash{$actual_portion}{$next_portion}){
# there is transition in hash
$hash{$actual_portion}->{$next_portion}+=1;
} else { #there isn't transition in hash
my %row;
$hash{$next_portion}=$row;
while( my ($k, $v) = each %hash ) {
$row->{$k}=0;
}
$hash{$next_portion}=%row;
while ( my ($k, $v) = each %hash ){
$hash{$k}->{$next_portion}=0;
#if ($actual_portion ne $start_portion){
$hash{$start_portion}->{$next_portion}=0;}
}
$hash{$actual_portion}->{$next_portion}=1;
}
$i++;
$actual_portion=$next_portion;
}
return %hash;
}
I think that the problem is that I'm adding to the %hash the reference to %row instead of hash and when i'm changing value of field in row i'm changing value in all added rows. I tried to resolve that but I only get an errors. Can anybody know how to do this correct??
PS I'm sorry but my english isn't perfect.
sub get_prob{
my ($file_content,$rank)=@_;
my %hash;
my %row;
$start_portion=get_portion_of_string($file_content,0,$rank);
$actual_portion=$start_portion;
$hash{$actual_portion}->{$actual_portion}=0;
$i=1;
$size=length($file_content);
while($i+$rank<=$size){
$next_portion=substr($actual_portion,1,$rank-1);
$next_portion.=substr($file_content,$i+$rank-1,1);
if (exists $hash{$actual_portion}{$next_portion}){
# there is transition in hash
$hash{$actual_portion}->{$next_portion}+=1;
} else { #there isn't transition in hash
my %row;
$hash{$next_portion}=$row;
while( my ($k, $v) = each %hash ) {
$row->{$k}=0;
}
$hash{$next_portion}=%row;
while ( my ($k, $v) = each %hash ){
$hash{$k}->{$next_portion}=0;
#if ($actual_portion ne $start_portion){
$hash{$start_portion}->{$next_portion}=0;}
}
$hash{$actual_portion}->{$next_portion}=1;
}
$i++;
$actual_portion=$next_portion;
}
return %hash;
}
I think that the problem is that I'm adding to the %hash the reference to %row instead of hash and when i'm changing value of field in row i'm changing value in all added rows. I tried to resolve that but I only get an errors. Can anybody know how to do this correct??
PS I'm sorry but my english isn't perfect.