Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

arrays of object in OO perl

by IL_MARO (Initiate)
on Nov 19, 2008 at 04:39 UTC ( #724463=perlquestion: print w/ replies, xml ) Need Help??
IL_MARO has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,
here is my problem:
You have a class, say, called Animal. Animal has a feature called DNA, and DNA is an array. Example:
package Animal; # constructor sub new { my $this = {}; # object properties $this->{DNA} = []; bless $this; return $this; } sub getDNA { my $this = shift; return "@{$this->{DNA}}"; } sub setDNA { my $this = shift; @{$this->{DNA}} = @_; return @{$this->{DNA}}; }
Now, imagine you want to create another class, say, called Population. And you want a bag of Animals, like:
package Population; use Animal; # constructor sub new { my $this = {}; # object properties $this->{individual} = []; bless $this; return $this; } sub init { for ($i=0; $i<5; $i++) { my $empl = Animal->new(); $empl->setDNA(4, 6, 8, 10); push (@{$this->{individual}}, $empl); } }
As you can see, the method init simply pushes 5 not empty Animals into a Population object.
The problem is that I'm not able to access them! What I should do if I want, say, set the DNA of a specific Animal of the Population?
And what if want to get or set a specific value of a specific slot of the DNA of an Animal...? I lack the knowledge I need. I tryied in many ways. Just an example, if I want to get the DNA of the Animal in the first slot:
sub getTheFirst{ my $this = shift; return @{$this->{individual}}[0]->getDNA(); }
But of course it's not working.
I can't get what I want...

Comment on arrays of object in OO perl
Select or Download Code
Replies are listed 'Best First'.
Re: arrays of object in OO perl
by ikegami (Pope) on Nov 19, 2008 at 06:17 UTC

    Use use strict;! There's a major error in init.

    As for the problem with getTheFirst, since it would be
    if you were dealing with an array instead of a reference to an array, then it's
    when using an array ref. It can also be written as
    So all together, we get


      Guys, may the allmighty Spaghetti Monster bless you all!
      I used strict, as suggested; I corrected all the mistakes. And now it works.
      I know, THOU SHALL USE STRICT... but I was lazy. Now I made another step toward enlightment. Thanks again.

      EDIT: it's not working... I mean: if I use the first slot of the array, it works:
      return $this->{individual}->[0]->getDNA();
      But if I use another slot, it turns out that is undefined.
      return $this->{individual}->[2]->getDNA(); This means that I don't have an array of 'individual', despite I pushed 5 of them into the array. Mmm...
      sub init { my $i; foreach $i (0 .. 4) { my $this = shift; my $empl = Animal->new(); $empl->setDNA(4, 6, 8, 10); push (@{$this->{individual}}, $empl); print " ".$i."\n"; print $this->{individual}->[$i]->getDNA(); } }

      In this way, the output is
      0 4 6 8 10 1 Can't call method getDNA ...
      And thus definitively I don't have and array of Animal, but individual is just a scalar. Damn.
        You want to take the
        my $this = shift;
        Outside of the for-loop.


        print "profeth still\n" if /bird|devil/;
Re: arrays of object in OO perl
by gwadej (Chaplain) on Nov 19, 2008 at 04:58 UTC

    Unfortunately, the biggest problem with using references seems to be getting the syntax right. Try thi:

    sub getIndividual{ my $this = shift; my $index = shift; return $this->{individual}->[$index]->getDNA(); }

    The problem with your version was you were trying to call a method on an array slice.

    G. Wade
      I tried with the suggested code
           return $this->{individual}->[$index]->getDNA();
      but the output is always the same:   Can't call method "getDNA" on an indefined value at ...  
      I want to point out that I don't push into Population empty arrays.
Re: arrays of object in OO perl
by ikegami (Pope) on Nov 19, 2008 at 06:14 UTC
    Use use strict;! There's a major error in init.
Re: arrays of object in OO perl
by matrixmadhan (Beadle) on Nov 19, 2008 at 06:10 UTC
    Would you mind if I say a small change?

    This is c-style looping

    for ($i=0; $i<5; $i++) {

    should be as

    foreach my $i ( 0 .. 4 ) {

      The counter isn't used so the range is better expressed 1 .. 5.

      Perl reduces RSI - it saves typing
        Actually it is used, to indicate/display the running number. So, the counter is needed

        push (@{$this->{individual}}, $empl); print " ".$i."\n"; print $this->{individual}->[$i]->getDNA();

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://724463]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2015-11-28 23:48 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls