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

  • Home
  • SEARCH
  • 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 6899703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:27:19+00:00 2026-05-27T07:27:19+00:00

I have an array like so: @switch_ports = () and then want to add

  • 0

I have an array like so:

@switch_ports = ()

and then want to add 50 instances of this hash, to the switch_ports array.

%port = (data1 => 0, data2 => 0, changed => 0)

However, if I push my hash to the array:

push(@switch_ports, %port)

and I do print @switch_ports, I just see:

data10data20changed0

so it just seems to be adding them to the array, (joining them)
and if I try and loop the array and print the keys, it also fails.

  1. Can you store a hash in an array?

  2. Can you have an array of hashes?

I’m trying to get this:

switchports
    0
        data1
        data2
        changed
    1
        data1
        ....

thus:

foreach $port (@switchport) {
    print $port['data1']
}

would return all of the data1 for all of the hashes in the 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-27T07:27:20+00:00Added an answer on May 27, 2026 at 7:27 am

    In Perl, array and hash members must be a single value. Before Perl 5.0, there was no (easy) way to do what you want.

    However, in Perl 5 you can now use a reference to your hash. A reference is simply the memory location where the item is being stored. To get a reference, you put a backslash in front of the variable:

    use feature qw(say);
    
    my $foo = "bar";
    say $foo;    #prints "bar"
    say \$foo;   #prints SCALAR(0x7fad01029070) or something like that
    

    Thus:

    my @switch_ports = ();
    my %port = ( data1 => 0, data2 => 0, changed => 0 );
    my $port_ref = \%port;
    
    push( @switch_ports, $port_ref );
    

    And, you don’t have to create $port_ref:

    my @switch_ports = ();
    my %port = ( data1 => 0, data2 => 0, changed => 0 );
    
    push( @switch_ports, \%port );
    

    To get the actual value of the reference, simply put the symbol back on front:

    #Remember: This is a REFERENCE to the hash and not the hash itself
    $port_ref = $switch_ports[0];
    %port = %{$port_ref};      #Dereferences the reference $port_ref;
    
    print "$port{data1}  $port{data2}  $port{changed}\n";
    

    Another shortcut:

    %port = %{$port[0]};   #Dereference in a single step
    print "$port{data1}  $port{data2}  $port{changed}\n";
    

    Or, even shorter, dereferencing as you go along:

    print ${$port[0]}{data1} . " " . ${$port[0]}{data2} . " " . ${$port[0]}{changed} . "\n";
    

    And a little syntactic sweetener. It means the same, but is easier to read:

    print $port[0]->{data1} . " " . $port[0]->{data2} . " " . $port[0]->{changed} . "\n";
    

    Take a look at Perldoc’s perlreftut and perlref. The first one is a tutorial.

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

Sidebar

Related Questions

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have an array of array like below with all numeric values. I want
I have an array like this: object[] args and need to insert those args
I have an array like this: $sports = array( 'Softball - Counties', 'Softball -
I have an array like this: var arr1 = [a, b, c, d]; How
Lets say I have an array like this: string [] Filelist = ... I
When you have an array like this: int foo[3][2][2]; and you make: int *bar
I have an array coming in with sub arrays like this Array ( [0]
I have an array of strings like this: tableArray: { Map, Web, Help +
An array is defined of assumed elements like I have array like String[] strArray

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.