Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:02:28+00:00 2026-06-13T18:02:28+00:00

I am trying to use a reference to an array in various ways that

  • 0

I am trying to use a reference to an array in various ways that I understand can be done.
Here’s one of my trial: –

my $RoR = [[2, 3], [4, 5, 7]];
my @AoA = ([2, 3], [[2, 3], [4, 5, 7]]);
my @AwR = ([2, 3], @{$RoR});

print $AoA[1][1][1], "\n";      # 1
print $AoA[1]->[1][1], "\n";    # 2

#print $RoR[0][1], "\n";        # 3
print $RoR->[0][1], "\n";       # 4

print $AwR[1][1][1], "\n";          # 5
print $AwR[1]->[1][1], "\n";        # 6

As you can see, I have three different arrays.

  • $RoR -> Is a reference to an anonymous array, which contains two anonymous arrays

  • @AoA -> Is an array containing 2 anonymous array. 2nd anonymous array is same as the array pointed to by the reference $RoR

  • @AwR -> Is almost same as @AoA, but with a difference that, rather than having a new anonymous array, I have added the reference @{$RoR} itself, inside it. So, basically I think, the last two arrays are practically the same.

Now, the problem is, they are not behaving in the same way.

  • The print statements 1, and 2 are working fine, and printing out the value 5 as result.

  • The print statement 3 is giving error, which I know why, so no problem in it. The print statement 4 is printing the value 3, as expected.

  • Now, the print statement 5 and 6 are throwing some errors when I try to run the program.

    "Can't use string ("2") as an ARRAY ref while \"strict refs\" in use at 
    D:/_Perl Practice/3perldsc/array_of_array.pl line 15."
    

Then I tried using: –

print $AwR[1][1]->[1], "\n";    
print @{$RoR}[1]->[1];

Where, I replaced $AwR[1] with @{$RoR}, as $AwR[1] contains @{$RoR}. The second one is working fine, while the first one is still throwing error.

So, what exactly is the difference between the 1st two and the last two print statements? I thought they are the same, and hence should give same result for same index access. But it is not. Am I using the reference in some wrong way?


Ok, now above doubt is clear. So, I have posted an answer accumulating the general stuff related to this question. Please take a look at it below. In that, I have a doubt in the 5th and 6th print statement, and to understand it I tried the below test: –

print $AwR[1][1];   # Allowed as `$AwR[1]` is same as `$$RoR`.
print $$RoR[1];     # Allowed as in 3(b)
print $AwR[1]->[1]; # Confused here. It is allowed
print $$RoR->[1];   # But this is not.

The first three statement are printing the same reference value. But the last one is failing. So, its sure that :- $AwR[1] is same as $$RoR. But, why $$RoR->[1] is not working while $AwR[1]->[1] is working? It’s throwing me this error: –

Not a SCALAR reference at D:/_Perl Practice/3perldsc/array_of_array.pl line 24.

So, what’s the difference in using it in two different way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:02:30+00:00Added an answer on June 13, 2026 at 6:02 pm

    In assignement to @AwR you dereference your reference to anonymous array $RoR, so you get basicly this behaviour:

    my @AwR = ([2, 3], [2, 3], [4, 5, 7]);
    

    not expected:

    my @AwR = ([2, 3], [[2, 3], [4, 5, 7]]);
    

    UPDATE

    If we have multidimensional array (array of arrays, AoA), then every dimension after first has to be a reference. So there is no need to explicitly to use dereferencing arrow, but it is allowed. So, if we have AoA as reference, we need to dereference first dimension explicilty and we may do it two ways $$AoA or $AoA->, but we can’t dereference it twice as you noticed ($$RoR->[1] in your example).

    Any following dimension we may omit dereferencing arrow, when we need address to certain element, so there is two equivalent use:

    my @AoA = ([2, 3], [ [2, 3], [4, 5, 7, [11, 12, [111, 112] ] ] ], [0]);
    print "A: $AoA[1][1][3][2][0] \n";          # == 111
    print "B: $AoA[1]->[1]->[3]->[2]->[0] \n";  # == 111
    

    or AoA as reference

    my $AoA = [ [2, 3], [ [2, 3], [4, 5, 7, [11, 12, [111, 112] ] ] ], [0] ];
    print "A: $$AoA[1][1][3][2][0] \n";           # == 111
    print "B: $AoA->[1]->[1]->[3]->[2]->[0] \n";  # == 111
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use reference parameters. There are several examples
I'm getting a Can't use an undefined value as a HASH reference error trying
how can one use a Safearray to pass an array of custom types (a
I'm trying to use an array to hold inputs for a survey that will
I am trying to reference a query from an array and use it in
I am trying to use the http_get function. But I get a undefined reference
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
I'm trying to use Type.InvokeMember(String, BindingFlags, Binder, Object, array []) with the default binder.
I'm attempting to write a wrapper so that my C# application can use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.