NetWallah has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to assign an existing array to an object member variable whose type is array. This should be possible, but I cant figure out how. I will also need to extract the entire array (I know how to get an individual item by indexing). Here is sample code for what I'm trying to do - of course, this code does not produce the desired result(6):

use Class::Struct qw(struct); struct S=>[a=>'$',b=>'@']; #"b" is an array my $a=S->new; my @q=(4,6,7,99); # A local array $a->b(@q); # What I'd like to do - gives syntax err print $a->b(1) # Prints NOTHING .. hoping to see "6" $a->b($q); # Try to assign the $$q to $a->b print $a->b(2) # Prints NOTHING .. hoping to see "7" # Would also like to do my @x = $a->b; % Extract entire array

I guess I'm looking for a overloaded method "b" that accepts an array parameter. I know that $a->b($scalar,$scalar) works fine.

How should I do this ???

Edited 2003-02-28 by Ovid

Replies are listed 'Best First'.
Re: Accessing ARRAYS inside objects
by xmath (Hermit) on Feb 28, 2003 at 19:15 UTC
    The documentation of Class::Struct says that $a->b in this case returns an array reference, so you can do:
    @{$a->b} = @q;
    and
    @x = @{$a->b};
    see documetation like perlreftut, perllol, and perlref for more details

    Update: just wanted to note you should realize that these statements really copy the elements from one array to the other. It's often more efficient to pass array references around instead; see the docs I linked to.

      Very helpful. I spent far too long trying to work this out myself.
    A reply falls below the community's threshold of quality. You may see it by logging in.