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...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.