Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Dereferencing the arrays of array references

Reply
Thread Tools

Dereferencing the arrays of array references

 
 
Pradeep Patra
Guest
Posts: n/a
 
      01-29-2012
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();
}



 
Reply With Quote
 
 
 
 
sreehari
Guest
Posts: n/a
 
      01-29-2012
On Jan 29, 6:12*am, Pradeep Patra <smilesonisa...@gmail.com> 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.


 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      01-29-2012
# 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";
}

 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      01-29-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Backwards references in arrays of arrays of... Ohad Lutzky Ruby 0 10-14-2006 12:09 AM
Dereferencing Hash of Arrays Jennifer I. Drake Perl Misc 7 07-13-2006 10:54 AM
Dereferencing a pointer to an array of pointers to objects Richard Cavell C++ 4 02-16-2005 12:35 PM
dereferencing char array as int array J. Campbell C++ 4 07-17-2003 05:56 PM



Advertisments