gsc has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to print the values of different arrays using foreach variable in array name, but it does not print the value on the specified array position. My code is.

perl -le '@array0 = qw( 1 2 3 4 );@array1 = qw( 5 6 7 8 );foreach $i(0,1){print "ARRAY $i\nValue1  $array{$i}[1]\n";};'

This is the part that I am not sure how to code (or even if it is possible).

$array{$i}[1]

I want the following output

ARRAY 0 Value1 2 ARRAY 1 Value1 6

Replies are listed 'Best First'.
Re: print perl arrays using variables in array name
by choroba (Cardinal) on Feb 22, 2016 at 22:24 UTC
    Hi gsc, welcome to the Monastery!

    Please, fix your markup: use code tags for code and data samples.

    What you want is possible, but not recommended. It's far better to use an array of arrays or hash of arrays:

    my @arrays = ([qw[ 1 2 3 4 ]], [qw[ 5 6 7 8 ]], ); for my $i (0, 1) { print "ARRAY $i\nValue1 $arrays[$i][1]\n"; }

    Note that I used 1 as the index to get the desired output, as arrays are indexed from 0.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: print perl arrays using variables in array name
by kcott (Archbishop) on Feb 22, 2016 at 23:25 UTC

    G'day gsc,

    Welcome to the Monastery.

    Please wrap code and data in <code>...</code> tags. We're seeing '$array{$i}[2]' (with the '2' being a link); however, you presumably typed '$array{$i}[[2]]'. Please see "Writeup Formatting Tips" for more about this.

    You appear to be confusing arrays (e.g. @array0) with hashes ($array{$i}[2] indicates the hash, %array, exists somewhere in your code).

    Arrays are zero-based on Perl. You've used an index of 2 but write "Value1": the 1st element in the array has an index of 0; the 3rd element in the array has an index of 2.

    I suspect you want to create variable names as if they were strings. This is a bad idea and you're code isn't even close to achieving this. Instead, use a hash and create key names which are strings.

    Always use the strict and warnings pragmata. Had you done this, Perl would have told you about problems. Put these lines at the top of your scripts:

    use strict; use warnings;

    For one-liners, replace '-e' with '-wE': it's not exactly the same but is usually sufficient. You'll need at least Perl v5.10.0 to use the '-E' switch; if you don't have that, use "perl ... -we 'use strict; ...'". See "perlrun: Command Switches" for details.

    Given the number of fundamental mistakes you've made, I strongly recommend that you read "perlintro: Perl introduction for beginners" — it's not long and should get you at least started with Perl code that compiles and runs correctly.

    Here's my best guess at what your posted one-liner should look like:

    $ perl -wlE 'my %h = (a0 => [1,2,3,4], a1 => [5,6,7,8]); print qq{A $_ +\nVal3 $h{"a" . $_}[2]} for (0,1)' A 0 Val3 3 A 1 Val3 7

    Here it is again, this time as a script, with somewhat more meaningful variable and key names, and, hopefully, far more comprehensible:

    #!/usr/bin/env perl -l use strict; use warnings; my %hash = ( array0 => [1, 2, 3, 4], array1 => [5, 6, 7, 8] ); for my $i (0, 1) { print "ARRAY $i"; print 'Value3 ', $hash{'array' . $i}[2]; }

    Output:

    ARRAY 0 Value3 3 ARRAY 1 Value3 7

    — Ken

Re: print perl arrays using variables in array name
by NetWallah (Canon) on Feb 23, 2016 at 03:45 UTC
    As others have pointed out, this is a terrible idea, and you should use a hash.

    But, because this is perl, here is the noose-constructor:

    perl -le '@array0 = qw( 1 2 3 4 );@array1 = qw( 5 6 7 8 );foreach $i(0 +,1){print "ARRAY $i\nValue ". ${"array$i"}[1]. "\n";};'

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

Re: print perl arrays using variables in array name
by BillKSmith (Monsignor) on Feb 23, 2016 at 04:55 UTC
    Your design is not just a bad idea, it is not even possible under use strict. One of the advantages of strict is that it prevents us from using this structure accidentially.
    Bill