Hi -

> -----Original Message-----
> From: Olivier Wirz [mailto:[EMAIL PROTECTED]
> Sent: Sunday, February 23, 2003 12:26 AM
> To: [EMAIL PROTECTED]
> Subject: Array Object in an instance variable
>
>
> Hello,
>
> In the _init method of my class, I have something like that:
>
> sub _init
> {
>    my ($self, %parameters) = @_;
>    $self -> Classes::_MyClass::_init(%parameters);
>    $self -> {_definitions} = ( { IN       => "file1.txt",
>                                              OUT    =>
> "file1.dat", } );
> }
>
>
> It works, I can get the informations via IN and OUT.
>
> I would like to have more than one anonymous hash in the instance variable
> _defitions, for example:
>
> $self -> {_definitions} = ( { IN       => "file1.txt",
>                                          OUT    => "file1.dat",},
>                                        { IN       => "file2.txt",
>                                          OUT    => "file2.dat",} );
> }
>
> It doesnt' work, I  just have 1 hash in _definitions (the second one with
> file2.txt and file2.dat).
>
> Do somebody have a solution for this problem ? Thank you.
>
> Olivier
>

Oliver, I think this is what is happening:

You are assigning a list ( ... ) to a scalar: $self->{_definitions}

This assignment assigns each element in the list, in turn, to the
scalar; your first assignment is overwritten by the second, and if
you had more elements, the process would continue until the scalar
ends up with the value of the last item in the list. Not a very
useful assignment.

Why don't you, instead, change the list to a list reference (or even
a hash reference)? For example, to assign to a list (array) reference:

 $self -> {_definitions} = [ { IN       => "file1.txt",
                               OUT    => "file1.dat",},
                             { IN       => "file2.txt",
                               OUT    => "file2.dat",},
                           ];

(Notice [] instead of () ). Now $self->{_definitions} is an reference
to an array of two hash references. To access the first hash:

 $self->{_definitions}->[0]->{IN} ...

the second:

 $self->{_definitions}->[1]->{IN} ...

You could probably make this more readable using a hash reference:

 $self -> {_definitions} = {file1 => { IN       => "file1.txt",
                                       OUT    => "file1.dat",},
                            file2 => { IN       => "file2.txt",
                                       OUT    => "file2.dat",},
                           };

Then access looks like:

 $self -> {_definitions}->{file1}->{IN} ...

Aloha => Beau;



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to