On Wed, Jun 16, 2010 at 18:02, Greg J <[email protected]> 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: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/