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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:15:44+00:00 2026-06-02T23:15:44+00:00

I have a reference to an array of hases that I pass to a

  • 0

I have a reference to an array of hases that I pass to a subroutine in my perl script

This is the code:

sub mySub {
    (my $resultref) = @_;
    my @list = @$resultref;
    print Dumper(@list);
    foreach my $result (@list) {
        print Dumper($result);
    }
}

And this is the output:

$VAR1 = [
          {
            'portName' => '1.1',
            'ips' => [
                       '192.168.1.242'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        '00:16:76:9e:63:47'
                      ]
          },
          {
            'portName' => '1.10',
            'ips' => [
                       '192.168.1.119',
                       '192.168.1.3'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        'd0:67:e5:f8:7e:7e',
                        'd0:67:e5:f8:7e:76'
                      ]
          },
        ];

$VAR1 = [
          {
            'portName' => '1.1',
            'ips' => [
                       '192.168.1.242'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        '00:16:76:9e:63:47'
                      ]
          },
          {
            'portName' => '1.10',
            'ips' => [
                       '192.168.1.119',
                       '192.168.1.3'
                     ],
            'switchIp' => '192.168.1.20',
            'macs' => [
                        'd0:67:e5:f8:7e:7e',
                        'd0:67:e5:f8:7e:76'
                      ]
          },
        ];

The loop is putting the whole array into the $result variable. I have tried dereferencing it as @$result[0] with no success.

How do I loop those hashes individually?

Thanks!

  • 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-02T23:15:48+00:00Added an answer on June 2, 2026 at 11:15 pm

    The arguments to Data::Dumper‘s Dumper function should be references. E.g.:

    use Data::Dumper;
    my @array = ([1,2,3], [11,22,33]); # Two-dimensional array
    print Dumper @array;               # print array
    print Dumper \@array;              # print reference to array
    

    The output:

    $VAR1 = [
              1,
              2,
              3
            ];
    $VAR2 = [
              11,
              22,
              33
            ];
    
    $VAR1 = [
              [
                1,
                2,
                3
              ],
              [
                11,
                22,
                33
              ]
            ];
    

    The second print gives us the entire structure in one variable. When you print the array directly, it expands into all its elements, so…

    print Dumper @array;
    

    Is equivalent to:

    print Dumper $array[0], $array[1], ..., $array[$#array];
    

    So, in your case, just do:

    sub mySub {
        my ($resultref) = @_;
        print Dumper $resultref;
    }
    

    Accessing the inner variables:

    Just take a look at Data::Dumper‘s output:

    $VAR1 = [    # bracket denotes start of an array ref
              {  # curly brackets = hash ref
                'portName' => '1.1',
                'ips' => [
                           '192.168.1.242'
                         ],
                'switchIp' => '192.168.1.20',
                'macs' => [
                            '00:16:76:9e:63:47'
                          ]
              }, # hash ref ends, comma = new array element begins
              {  # new hash ref 
                'portName' => '1.10',
                'ips' => [
                           '192.168.1.119',
                           '192.168.1.3'
                         ],
                'switchIp' => '192.168.1.20',
                'macs' => [
                            'd0:67:e5:f8:7e:7e',
                            'd0:67:e5:f8:7e:76'
                          ]
              }, # end of hash
            ];   # end of array
    

    Important to note here is that all elements of an array, and all the values of a hash are scalars. Therefore, all hashes and arrays can easily be broken up into a list of scalars.

    for my $aref (@$resultref) {  # starting array ref
        for my $aref2 (@$aref) {  # second level array ref
            for my $href (@$aref2)  # here begins the hash
                local $\ = "\n";    # add newline to print for simplicity
                print $href->{portName};    # printing a scalar
                print for @{$href_>{ips}};  # printing an array ref w post-script loop
                print $href->{switchIp};
                print for @{$href->{macs}};
            }
        }
    }
    

    Note the use of the arrow operator to dereference a reference. If you have a hash or array you would do $array[0] or $hash{$key}, but by using a reference, you “point” to the address contained in the reference instead: $array->[0] or $hash->{$key}.

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

Sidebar

Related Questions

I have this code in Perl: sub f { return [1,2,3] } print f;
Suppose we have a subroutine returning a reference sub aspirin { my @items =
I have a perl object (reference to array of references) like the below: my
This question builds off of a previously asked question: Pass by reference multidimensional array
I have a List that, when serialized as JSON gives me an array of
I have an array witch I pass to a function by reference to sort
So, in Perl, I have an array within an object (so, a reference to
I have a reference application that I use to work through DDD issues, and
I have a reference to call that may or may not be there. When
In C++ I have a reference to an object that wants to point back

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.