Dereferencing the arrays of array references


P

Pradeep Patra

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();
}
 
Ad

Advertisements

S

sreehari

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.
 
G

George Mpouras

# 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";
}
 
Ad

Advertisements

W

Wolf Behrenhoff

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
 

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

Top