Dereferencing the arrays of array references

Discussion in 'Perl Misc' started by Pradeep Patra, Jan 29, 2012.

  1. Hi,
    I am tryting out a perl program and I am having issue in
    dereferencing the arrays or arrayreferences.
    I am referring to the "growing your own" from perldoc perllol.

    Regards
    Pradeep

    my $array_refs;
    my @array;
    for ($i=0;$i < 3; $i++)
    {
    @array = func1($i)
    push(@{$array_refs},\@array);
    }

    for my $s (@{$array_refs})
    {
    my $c = $s->func2(); # This function works on actual arrays
    }


    I am getting an error "cannot call func2() on unblessed reference".
    When I tried with below it works but I am getting the last array
    values(from for loop i <3 as above) which I dont want. I tried with
    @@{$array_refs} but i am getting some error"Global symbol
    @@{$array_refs} in package". Can anybody please help me to figure out
    what i am doing wrong?

    Works:
    =====
    for my $s (@array){
    {
    my $c = $s->func2();
    }
     
    Pradeep Patra, Jan 29, 2012
    #1
    1. Advertising

  2. Pradeep Patra

    sreehari Guest

    On Jan 29, 6:12 am, Pradeep Patra <> wrote:
    > Hi,
    >   I am tryting out a perl program and I am having issue in
    > dereferencing the arrays or arrayreferences.
    > I am referring to the "growing your own" from perldoc perllol.
    >
    > Regards
    > Pradeep
    >
    > my $array_refs;
    > my @array;
    > for ($i=0;$i < 3; $i++)
    > {
    >    @array = func1($i)
    >    push(@{$array_refs},\@array);
    >
    > }
    >
    > for my $s (@{$array_refs})
    > {
    >    my $c = $s->func2(); # This function works on actual arrays
    >
    > }
    >
    > I am getting an error "cannot call func2() on unblessed reference".
    > When I tried with below it works but I am getting the last array
    > values(from for loop i <3 as above) which I dont want. I tried with
    > @@{$array_refs} but i am getting some error"Global symbol
    > @@{$array_refs} in package". Can anybody please help me to figure out
    > what i am doing wrong?
    >
    > Works:
    > =====
    > for my $s (@array){
    > {
    >    my $c = $s->func2();
    >
    >
    >
    > }


    Hi,
    What you are trying to do is calling a method on an array reference
    which is wrong.
    Assuming that your func1() returns an array of objects, then

    for my $s (@{$array_refs})
    {
    for my $o (@$s) {
    my $c = $o->func2();
    }
    }

    If the func1() doesn't return an array of objects, then you can't call
    methods on those
    References in the first place.
     
    sreehari, Jan 29, 2012
    #2
    1. Advertising

  3. # I think this is what you want

    sub func1 {"blue$_[0]","sky$_[0]"}
    my $func2 = sub {"watch @{$_[0]}"};

    my $array_refs;
    for ($i=0;$i < 3; $i++)
    {
    my @array = func1($i);
    push @{$array_refs}, \@array;
    }

    for my $s (@{$array_refs})
    {
    my $c = $func2->($s);
    print "$c\n";
    }
     
    George Mpouras, Jan 29, 2012
    #3
  4. Am 29.01.2012 12:12, schrieb Pradeep Patra:
    > Hi,
    > I am tryting out a perl program and I am having issue in
    > dereferencing the arrays or arrayreferences.


    First question: why are you creating a reference to an array of arrayrefs?

    > my $array_refs;
    > my @array;
    > for ($i=0;$i < 3; $i++)
    > {
    > @array = func1($i)
    > push(@{$array_refs},\@array);
    > }


    I think $array_refs should better be @array_refs. First, it matches the
    name better and also I don't see a point in using a reference here. In
    addition, you don't need the @array outside of the loop. So keep it inside.

    Code could be:
    my @array_refs;
    for my $i (0..2) {
    my @array = func1($i);
    push @array_refs, \@array;
    }

    or even better - no need to use a loop, make use of map:
    my @array_refs = map [func1($_)], (0..2);

    > for my $s (@{$array_refs})
    > {
    > my $c = $s->func2(); # This function works on actual arrays


    Exactly. It is working on an array. The the array does not have a
    function called "func2". Your objects in the array might have this
    function. You need to loop over the array elements.

    It is always good to provide minimal code which one can run, including
    your func1 and func2.

    So try this:
    --------------------8<--------------------
    package xxx;
    sub new { bless {('name' => $_[1])}, $_[0]; }
    sub func2 { $self=shift; print "Hi, I am func2 $self->{name}\n" }

    package main;
    sub func1 { return (xxx->new($_[0]), xxx->new($_[0] . ", version2")) }

    my @array_refs = map [func1($_)], (0..2);

    for my $arrayref (@array_refs) {
    for my $element (@$arrayref) {
    $element->func2();
    }
    }
    --------------------8<--------------------

    - Wolf
     
    Wolf Behrenhoff, Jan 29, 2012
    #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. J. Campbell
    Replies:
    4
    Views:
    1,177
    Simon G Best
    Jul 17, 2003
  2. Richard Cavell
    Replies:
    4
    Views:
    7,024
    Karl Heinz Buchegger
    Feb 16, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,141
    Philipp
    Jan 20, 2009
  4. Ohad Lutzky
    Replies:
    0
    Views:
    112
    Ohad Lutzky
    Oct 14, 2006
  5. Jennifer I. Drake

    Dereferencing Hash of Arrays

    Jennifer I. Drake, Jul 12, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    138
    -berlin.de
    Jul 13, 2006
Loading...

Share This Page