How does one access elements of a passed anonymous list?

J

JB

I am trying to use the AI::Genetic module, but I cannot
figure out how to access data it is passing. In short,
to use the module, it is necessary to write a 'fitness'
function that will be passed an anonymous list. I can't
seem to properly access the passed list.

Here's an example:

sub fitness()
{
print Dumper(@_);
}

results in:
$VAR1 = [
1,
0,
1
];


Yet if I try:

sub fitness()
{
@temp = @_;
print $temp[0];
}

I get:
ARRAY(0x18343d8)


The only way I can view the passed list is by
using 'Dumper' but that doesn't do me much good.

How do I access the elements of the passed list?

Thanks in advance for any responses.
 
P

Peter J. Holzer

JB said:
I am trying to use the AI::Genetic module, but I cannot
figure out how to access data it is passing. In short,
to use the module, it is necessary to write a 'fitness'
function that will be passed an anonymous list. I can't
seem to properly access the passed list.

Here's an example:

sub fitness()
{
print Dumper(@_);
}

results in:
$VAR1 = [
1,
0,
1
];

So, $_[0] is a reference to an array,

Read perldoc perlreftut on how to access an array through a reference.


hp
 
M

Matt Garrish

JB said:
I am trying to use the AI::Genetic module, but I cannot
figure out how to access data it is passing. In short,
to use the module, it is necessary to write a 'fitness'
function that will be passed an anonymous list. I can't
seem to properly access the passed list.

Here's an example:

sub fitness()
{
print Dumper(@_);
}

results in:
$VAR1 = [
1,
0,
1
];


Yet if I try:

sub fitness()
{
@temp = @_;
print $temp[0];
}

I get:
ARRAY(0x18343d8)

Don't know the module, but you're obviously getting an array ref passed to
the sub, not an array of vaues. Try the following:

sub fitness {
my $aref = shift;
foreach (@$aref) {
print "$_\n";
}
}

Matt
 
A

A. Sinan Unur

I am trying to use the AI::Genetic module, but I cannot
figure out how to access data it is passing. In short,
to use the module, it is necessary to write a 'fitness'
function that will be passed an anonymous list. I can't
seem to properly access the passed list.

Here's an example:

sub fitness()

Did you really intend to declare fitness as taking no arguments?

{
print Dumper(@_);
}

results in:
$VAR1 = [
1,
0,
1
];

When posting code, use copy & paste, don't retype code.

Apparently, in the version of the code you ran, a single reference to an
array was passed to the routine.

Read

* the posting guidelines for this group
* perldoc perlref
* perldoc -q "What is the difference between a list and an array?"

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top