On 18 Jun 2001 16:35:45 -0500, Nick Transier wrote:
> Given this function to create a new class object:
>
> sub new {
>
> my $invocant = shift;
> my $class = ref($invocant) || $invocant;
> my $self = {
> Level => 999, #values Value => 999,
> Key => 999,
> @Next, #array of pointers to next elements
> @_,
> };
> return bless $self,$class;
>
> }
>
> Can you define an array of pointers (@Next) and leave it with nothing in it
> at declaration? If so -
> Is this the proper way to access a class object's "Next" array at the
> $level'th level?
>
> $self->{Next[$level]}
>
> thanks,
> -Nick
<snip />
Ah, I see now. You are confused about references. If you were to have
had an array @Next and used it in this way it would not have stored the
array in your hash, rather it would have taken the odd values as keys
and the even values as values for thos keys. What you want are
references (or how complex data sturctures are built):
<code>
#!/usr/bin/perl
use strict;
my $foo = foo->new;
$foo->add_pointer(1);
$foo->add_pointer(2);
$foo->add_pointer(3);
$foo->print_pointers;
package foo;
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
'Level' => 999, #values
'Value' => 999,
'Key' => 999,
'next_array' => [], #empty array of pointers
};
return bless $self,$class;
}
sub add_pointer {
my ($self, $pointer) = @_;
push @{$self->{next_array}}, $pointer;
}
sub print_pointers {
my ($self) = @_;
print join(" ", @{$self->{next_array}}), "\n";
}
</code>
You may wish to read "perldoc perlreftut", "perldoc perllol", and
"perldoc perlref".
--
Today is Prickle-Prickle, the 23rd day of Confusion in the YOLD 3167
Frink!