1

How can I print the values of an array. I have tried several ways but I am unable to get the required values out of the arrays:

@array; Dumper output is as below :

$VAR1 = [
          'a',
          'b',
          'c'
        ];
$VAR1 = [
          'd',
          'e',
          'f'
        ];
$VAR1 = [
          'g',
          'h',
          'i'
        ];
$VAR1 = [
          'j',
          'k',
          'l'
        ];



 for my $value (@array) {
  my $ip = $value->[0];
  DEBUG("DEBUG '$ip\n'");
                                  }

I am getting output as below, which means foreach instance I am only getting the first value.

    a
    d
    g
    j

I have tried several approaches :

First option :

my $size = @array;
for ($n=0; $n < $size; $n++) {
  my $value=$array[$n];
  DEBUG( "DEBUG: Element is as $value" );
}

Second Option :

 for my $value (@array) {
  my $ip = $value->[$_];
  DEBUG("DEBUG Element is '$ip\n'");
 }

What is the best way to do this?

3
  • 1
    my $ip = join "", @$value; Commented Jun 17, 2014 at 14:45
  • You get the first value of each array because my $ip = $value->[0]; dereferences the value from the first [0] index position of each array. You want to dereference (and loop over) each of the anonymous arrays inside your "outer" array (in this case @array). Your "First option" is closer (you're trying to loop) but you are looping over the "$size" of the outer array instead of each inner anonymous array. Your second option is a shorter way of making the same mistake :-) It is a common error: loops and dereferencing are different - but they often happen at the same time! Commented Jun 17, 2014 at 15:46
  • If you look at the solution provided by @mpapec it basically tells the compiler that the loop variable $value is an array (referenced by that scalar) which is why the sigil looks like: @$. If you use Perl::Critic on your code with the --cruel option you'll find that that @{ $value } is the preferred (i.e. Perl Best Practices) syntax. :-) Commented Jun 17, 2014 at 15:57

2 Answers 2

2

It is obvious that you have list of arrays. You only loop over top list and print first (0th) value in your first example. Barring any automatic dumpers, you need to loop over both levels.

for my $value (@array) {
    for my $ip (@$value) {
        DEBUG("DEBUG '$ip\n'");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You want to dereference here so you need to do something like:

my @array_of_arrays = ([qw/a b c/], [qw/d e f/ ], [qw/i j k/])
for my $anon_array (@array_of_arrays) { say for @{$anon_array} }

Or using your variable names:

use strict;
use warnings;

my @array = ([qw/a b c/], [qw/d e f/], [qw/i j k/]);

for my $ip (@array) {
   print join "", @{$ip} , "\n";  # or "say"      
}

Since there are anonymous arrays involved I have focused on dereferencing (using PPB style!) instead of nested loops, but print for is a loop in disguise really.

Cheers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.