CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Can't dereference an array (simple)

 



perlFun
Novice

Jun 2, 2013, 1:18 PM

Post #1 of 7 (633 views)
Can't dereference an array (simple) Can't Post

Hi, I am having trouble with a very simple process of dereferencing an array in perl.

use strict;
use warnings;

my @rabbits=['rex','himalayan','dwarf'];
my $rabbitRef=\@rabbits;
print "referenced: $rabbitRef\n";
my @realRab=@$rabbitRef;
print "dereferenced: @realRab\n";


If I do this, the following is the output:

referenced: ARRAY(0x10dcfa0)
dereferenced: ARRAY(0x10dc2a0)


I used this method to dereference as per the following page

http://www.perlmeme.org/howtos/using_perl/dereferencing.html

method 1

I am not sure what I'm doing wrong!


FishMonger
Veteran / Moderator

Jun 2, 2013, 1:29 PM

Post #2 of 7 (627 views)
Re: [perlFun] Can't dereference an array (simple) [In reply to] Can't Post


Code
my @rabbits=['rex','himalayan','dwarf'];

That is not doing what you think.

@rabbits is being assigned a single element and that element is a reference to an array.

You meant to use ( ) parens instead of the [ ] brackets.


perlFun
Novice

Jun 2, 2013, 1:32 PM

Post #3 of 7 (625 views)
Re: [FishMonger] Can't dereference an array (simple) [In reply to] Can't Post

oh wow how could I make such a mistake! But this brings up something I didn't realize, so if I use [] instead of () when declaring an array, it gives me an array reference? If that's the case then it makes sense why my example was bad, because from what I understand, @rabbits was just a reference to an array, so when I dereferenced $rabbitRef, it was dereferencing to a reference?


Laurent_R
Veteran / Moderator

Jun 2, 2013, 1:33 PM

Post #4 of 7 (625 views)
Re: [perlFun] Can't dereference an array (simple) [In reply to] Can't Post

 

Code
my @rabbits=['rex','himalayan','dwarf'];


This is wrong, or let us say that this is most probably not what you intend to do.

Square brackets around a list like these create a reference to an anonymous array; to create an array you want to use normal parens:


Code
my @rabbits=('rex','himalayan','dwarf');


And to create a reference to an anonymous array, you would want to use a scalar variable:


Code
my $rabbits_ref = ['rex','himalayan','dwarf'];



perlFun
Novice

Jun 2, 2013, 1:36 PM

Post #5 of 7 (623 views)
Re: [Laurent_R] Can't dereference an array (simple) [In reply to] Can't Post


In Reply To

And to create a reference to an anonymous array, you would want to use a scalar variable:


Code
my $rabbits_ref = ['rex','himalayan','dwarf'];




Thanks so much, yeah it was a dumb mistake I ment to have the parens. Just out of curiosity, why didn't perl yell at me when I incorrectly used @ instead of $ on the rabbits variable?


Laurent_R
Veteran / Moderator

Jun 2, 2013, 1:38 PM

Post #6 of 7 (622 views)
Re: [perlFun] Can't dereference an array (simple) [In reply to] Can't Post


In Reply To
oh wow how could I make such a mistake! But this brings up something I didn't realize, so if I use [] instead of () when declaring an array, it gives me an array reference? If that's the case then it makes sense why my example was bad, because from what I understand, @rabbits was just a reference to an array, so when I dereferenced $rabbitRef, it was dereferencing to a reference?


Yes, you basically had in $rabbitRef a reference to a reference to an array. Dereferencing only once, gave you a reference.


FishMonger
Veteran / Moderator

Jun 2, 2013, 1:43 PM

Post #7 of 7 (619 views)
Re: [perlFun] Can't dereference an array (simple) [In reply to] Can't Post

Use of Data::Dumper would have shown you the exact data structure. Whenever in doubt about what a var holds, use Data::Dumper to dump it out.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives