Objects and Arrays...

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. [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....
    5. 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?...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default Re: Objects and Arrays...

    Pedro Miguel Freire Custódio wrote:
    > 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;
    > }
    print "Got object[0] as $self->{OBJECT}->[0] and object[1] as
    $self->{OBJECT}->[1]\n";
    >
    > }
    Try it with the changes made above. Each change was made for a specific reason.



    R. Joseph Newton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139