How does one access elements of a passed anonymous list?

Discussion in 'Perl Misc' started by JB, Apr 16, 2006.

  1. JB

    JB Guest

    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.
     
    JB, Apr 16, 2006
    #1
    1. Advertising

  2. JB wrote:

    > 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

    --
    _ | Peter J. Holzer | Löschung von at.usenet.schmankerl?
    |_|_) | Sysadmin WSR/LUGA |
    | | | | Diskussion derzeit in at.usenet.gruppen
    __/ | http://www.hjp.at/ |
     
    Peter J. Holzer, Apr 16, 2006
    #2
    1. Advertising

  3. JB

    Matt Garrish Guest

    "JB" <> wrote in message
    news:...
    >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
     
    Matt Garrish, Apr 16, 2006
    #3
  4. "JB" <> wrote in news:1145212314.518661.181740
    @g10g2000cwb.googlegroups.com:

    > 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 <>
    (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
     
    A. Sinan Unur, Apr 16, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    541
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    938
    Anand
    Sep 11, 2003
  3. Adam Hartshorne
    Replies:
    2
    Views:
    391
    Nitin Motgi
    Jan 27, 2006
  4. Reporter
    Replies:
    3
    Views:
    499
    Mike Schilling
    May 12, 2007
  5. Replies:
    1
    Views:
    242
Loading...

Share This Page