Ask a Question related to PERL Beginners, Design and Development.
-
Pedro miguel freire custódio #1
Objects and Arrays...
Hi,
this is probably a stupid question. The question is that I want a
particular class of objects to have several attributes, one of them a
list (array), the problem is that I can't get it to work, it only
stores the last value, and when I try to join another array it doesn't
work. If somebody could help me on this, I've tried some references and
stuff but i couldn't figure it out:
package SpeechAct;
sub new {
my $classname = shift;
my $self = {};
bless($self, $classname);
$self->_init(@_);
return $self;
}
sub _init {
my $self = shift;
$self->{OBJECT} = ("xpto1","xpto2"); ---> THE PROBLEM! It
only stores xpto2
if (@_) {
my %extra = @_;
@$self{keys %extra} = values %extra;
}
}
And when i try to use this method:
sub object {
my $self = shift;
return $self->{OBJECT} unless @_;
my @list = $self->{OBJECT};
print "List: ", @list, " with ", scalar(@list) ," element\n";
#push(@list, @_);
print "new list: ", @list, "\n";
#$self->{OBJECT} = @list;
#print "New obj: ", $self->object ,"\n" ;
return $self->{OBJECT};
}
When called like this
$sa->object("xpto3","xpto4");
i get :
newlist: xpto2xpto3xpto4 ----> (lost the xpto1)
New obj; 3 ----> SIZE?!?
Thanks for any possible tip.. i'm lost... :(
Pedro miguel freire custódio Guest
-
Merge/Concat two arrays of objects
Hi, This seems a simple thing but I can not find a solution. Has I run through some data I create an inner array and outer array The outer... -
Storing Objects/Arrays in Stored Objects
Hello All, I recently came across a very frustrating issue when trying to create and store arrays within objects in a Shared object. It took me... -
How to store arrays in hashes or objects?
I had emailed this query out previously but since I never saw my own email in the digest, I'm assuming that it never made it to the... -
[PHP-DEV] Objects as Arrays
Hello internals, IIRC it was intended to use object properties as arrays. Currently most array_*() functions do explicitly check for IS_ARRAY.... -
Using & for arrays of objects
On Fri, 27 Jun 2003 09:48:56 +0900 "Krishna Dole" <kpdole@ucdavis.edu> wrote: & uses hashes under the hood. You need to define #hash and #eql?... -
Drieux #2
Re: Objects and Arrays...
On Jan 25, 2004, at 7:47 AM, Pedro Miguel Freire Custódio wrote:
[..][..]>
> sub _init {
> my $self = shift;
> $self->{OBJECT} = ("xpto1","xpto2"); ---> THE PROBLEM! It
> only stores xpto2
> if (@_) {
> my %extra = @_;
> @$self{keys %extra} = values %extra;
> }
> }
>
> And when i try to use this method:
>
> sub object {
> my $self = shift;
> return $self->{OBJECT} unless @_;
>
> my @list = $self->{OBJECT};
have we thought about doing that as an array ref?
$self->{OBJECT} = ["xpto1","xpto2"];
....
my @list = @{$self->{OBJECT}};
ciao
drieux
---
Drieux Guest
-
Pedro miguel freire custódio #3
Re: Objects and Arrays...
hehe
thanks, i really tried a lot of stuff.. but i didn't figure out the @{}
operator... :)
It worked! ;)
Thanks,
Pedro
On 25 de jan de 2004, at 15:55, drieux wrote:
>
> On Jan 25, 2004, at 7:47 AM, Pedro Miguel Freire Custódio wrote:
> [..]> [..]>>
>> sub _init {
>> my $self = shift;
>> $self->{OBJECT} = ("xpto1","xpto2"); ---> THE PROBLEM!
>> It only stores xpto2
>> if (@_) {
>> my %extra = @_;
>> @$self{keys %extra} = values %extra;
>> }
>> }
>>
>> And when i try to use this method:
>>
>> sub object {
>> my $self = shift;
>> return $self->{OBJECT} unless @_;
>>
>> my @list = $self->{OBJECT};
>
> have we thought about doing that as an array ref?
>
> $self->{OBJECT} = ["xpto1","xpto2"];
> ...
> my @list = @{$self->{OBJECT}};
>
>
> ciao
> drieux
>
> ---
>
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>Pedro miguel freire custódio Guest
-
R. Joseph Newton #4
Re: Objects and Arrays...
Pedro Miguel Freire Custódio wrote:
print "Got object[0] as $self->{OBJECT}->[0] and object[1] as> Hi,
>
> this is probably a stupid question. The question is that I want a
> particular class of objects to have several attributes, one of them a
> list (array), the problem is that I can't get it to work, it only
> stores the last value, and when I try to join another array it doesn't
> work. If somebody could help me on this, I've tried some references and
> stuff but i couldn't figure it out:
>
> package SpeechAct;
>
> sub new {
> my $classname = shift;
> my $self = {};
> bless($self, $classname);
> $self->_init(@_);
> return $self;
> }
>
> sub _init {
> my $self = shift;
> $self->{OBJECT} = ['xpto1','xpto2'];
> while (my $key = shift ) {
> $self->{$key} = shift;
> }
$self->{OBJECT}->[1]\n";
Try it with the changes made above. Each change was made for a specific reason.>
> }
R. Joseph Newton Guest



Reply With Quote

