Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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
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 Population.pm ...  
      I want to point out that I don't push into Population empty arrays.
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();
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 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
    $array[0]->getDNA()
    if you were dealing with an array instead of a reference to an array, then it's
    ${$array_ref}[0]->getDNA()
    when using an array ref. It can also be written as
    $array_ref->[0]->getDNA()
    So all together, we get
    $this->{individual}->[0]->getDNA()</c>

    See

      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.

        GreetZ!,
          ChOas

        print "profeth still\n" if /bird|devil/;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-09-30 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls