Hi everyone,

I am new to Perl and I am having some trouble working with hashes of hashes.

I am trying to write a simple Markov Chain text generator and thought
it might be a good idea to keep track of state transitions in a hash.

Here is some of the code I have..

====================================
use Data::Dumper;

my %StateTable = ();

sub InsertWord
{
  my( $state, $next ) = shift @_;
  if( $StateTable{$state} )
  {
     if( $StateTable{$state}{$next} )
     {
        $StateTable{$state}{$next}++;
     }else{
       $StateTable{$state} = { $next => 1 };
     }
  }else{
    $StateTable{$state} = { $next => 1 };
  }
}

InsertWord( "Hello", "World" );
InsertWord( "Hello", "There" );

print Dumper(\%StateTable);
====================================

Currently this does not work as intended.  When I dump the hash all I
get is this:

$VAR1 = {
          'Hello' => {
                       '' => 2
                     }
        };

What I want is this:

$VAR1 = {
          'Hello' => {
                       'World' => 1
                       'There' => 1
                     }
        };

I am looking forward to hearing all your comments.

Thanks,
Greg

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to