On Wed, Jun 16, 2010 at 18:02, Greg J <greg.jar...@gmail.com> wrote:
snip
> sub InsertWord
> {
>   my( $state, $next ) = shift @_;
snip

This is your big problem.  You need to say

my ($state, $next) = @_;

or

my $state = shift;
my $next  = shift;

snip
>   if( $StateTable{$state} )
>   {
>      if( $StateTable{$state}{$next} )
>      {
>         $StateTable{$state}{$next}++;
>      }else{
>        $StateTable{$state} = { $next => 1 };
>      }
>   }else{
>     $StateTable{$state} = { $next => 1 };
>   }
> }
snip

You don't need to create anonymous hashes, Perl autovivifies them for
you.  Just say

$StateTable{$state}{$next}++;

and Perl will do the right thing.  Global variables (%StateTable) are
evil.  Try using OO to make it nicer:

#!/usr/bin/perl

use strict;
use warnings;

{
        package StateTable;

        sub new {
                my $class = shift;
                return bless {}, $class;
        }

        sub insert_word {
                my $self = shift;
                my ($state, $next) = @_;

                $self->{$state}{$next}++
        }
}

my $state_table = StateTable->new;

$state_table->insert_word("Hello", "World");
$state_table->insert_word("Hello", "There");

use Data::Dumper;
print Dumper $state_table;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
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