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 5838049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:25:35+00:00 2026-05-22T11:25:35+00:00

I have two data structures in Perl: An array: my @array2 = ( 1,

  • 0

I have two data structures in Perl:

An array:

my @array2 = ( "1", "2", "3");

for $elem (@array2) {
    print $elem."\n";
}

Giving me the following output:

1
2
3

And a list:

my @array = [ "1", "2", "3"];                                            

for $elem (@array) {
    print $elem."\n";
}

Giving the following output:

ARRAY(0x9c90818)

Obviously, I’d like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array?

  • 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-05-22T11:25:36+00:00Added an answer on May 22, 2026 at 11:25 am

    Lists in Perl are not data structures, they are positions in the source code, determined by the context around them. Lists are basically the transient structures that Perl uses to move data around. You interact with them with all of Perl’s syntax, but you can not work with them as a data type. The data type that is closest to a list is an array.

    my @var    =    (1, 2, 3);  # parens needed for precedence, they do not create a list
       ^ an array    ^ a list
    
    say 1, 2, 3;
        ^ a list
    
    say @var;
        ^ a list (of one array, which will expand into 3 values before `say` is called)
    

    When you write [1, 2, 3] what you are doing is creating a scalar array reference. That array reference is initialized with the list 1, 2, 3, and it is the same as creating a named array and taking a reference to it:

    [1, 2, 3]   ~~   do {my @x = (1, 2, 3); \@x}
    

    Since the [...] construct creates a scalar, you should hold it in a scalar:

    my $array = [1, 2, 3];                                            
    
    for my $elem (@$array) {   # lexical loop variable
        print $elem."\n";
    }
    

    Since you want to operate on the whole array, and not just the reference, you place a @ in front of the $array which dereferences the stored array reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a two dimensional array that I need to load data into. I
I have a two-dimensional array (of Strings) which make up my data table (of
I have two data structures in Java: One is called DebateAssignment and has 5
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two processes one will query other for data.There will be huge amount
I have two files (f1 and f2) containing some text (or binary data). How
I have two projects, the DLL project which has all my logic and data
I have two arrays of animals (for example). $array = array( array( 'id' =>
In Perl, when you have a nested data structure, it is permissible to omit

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.