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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:37:14+00:00 2026-05-16T22:37:14+00:00

I’m trying to iterate over a 2D array that is structured in this specific

  • 0

I’m trying to iterate over a 2D array that is structured in this specific way. Whether or not this is a good way to structure the array is another question – I still need to be able to iterate over it (if it is possible).

@row1 = ( "Current Scan", "Last Month");
@row2 = ( "240", "0");
@row3 = ( "226", "209");
@row4 = ( "215", "207");

@array = (\@row1, \@row2, \@row3, \@row4);
print Dumper(@array);
printarray(@array);

Dumper gives me the following output:

$VAR1 = [
          'Current Scan',
          'Last Month'
        ];
$VAR2 = [
          '240',
          '0'
        ];
$VAR3 = [
          '226',
          '209'
        ];
$VAR4 = [
          '215',
          '207'
        ];

I’ve tried several for loops with no success. Each only prints the first row ($VAR1) and quits. Here is my most recent attempt:

sub printarray {
  @array = shift;
  $rowi = 0;
  foreach my $row (@array) {
    for (my $coli = 0; $coli <= @$row; $coli++) {
      print "$array[$rowi][$coli]\n";
    }
    $rowi++;
  }
}

I’m obviously overlooking something simple. What am I doing wrong? Thanks in advance!

  • 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-16T22:37:14+00:00Added an answer on May 16, 2026 at 10:37 pm

    If you want just print the array, try following code:

    foreach my $row (@array) {
       foreach my $elem (@$row) {
           print $elem; ## print elements without separator
       }
       print "\n"; ## new line after row
    }
    

    If you need indexes for some purpose, here we go:

    for(my $row_i = 0; $row_i < @array; $row_i++) {
        for(my $column_i = 0; $column_i < @{ $array[$row_i] }; $column_i++) {
            print $array[$row_i][$column_i];
        }
    }
    

    The idea is that @array in scalar context returns number of elements in array. And @{ $array[$row_i] } is a little more tricky. It dereference array stored in $array[$row_i].

    Update for subroutine:

    In perl you can pass array by reference:

     printarray(\@array); ## pass reference
    
     sub printarray {
         my $array_ref = shift; ## no copy created
    
         foreach my $row (@$array_ref) { ## now we need to dereference
             ...
         }
     }
    

    You can also pass a copy of array:

     printarray(@array);
    
     sub printarray {
         my @array_copy = @_; ## store local copy of array
         ...
     }
    

    For more details take a look at How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? manual page.

    And please add use strict; at the begining of programm. It’ll force you to declare all variables, but will save bunch of time if you type something incorrectly.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.