1

I have three arrays. I want to access elements of these three arrays by changing the array names inside a for loop.

I tried concatenating a string to the array variables inside the for loop but its not printing the array elements.

   #!/usr/bin/perl
   my $iterator;
   @data_1 = (10,20,30,40,50);
   @data_2 = ('a','b','c','d','e');
   @data_3 = (-10,-20,-30,-40,-50);

   for ($field = 1; $field < 4; $field++)
   {
    $iterator = "\$data_$field";
    print "iterator = $iterator\n";
    print "$iterator[0]";
   }

Actual output -

   iterator = $data_1
   iterator = $data_2
   iterator = $data_3

Expected output -

   iterator = $data_1
   10
   iterator = $data_2
   a
   iterator = $data_3
   -10

Please guide. Thanks.

1
  • 1
    ALWAYS use use strict; use warnings; Commented Aug 17, 2019 at 19:24

2 Answers 2

3

Using variable names with other variables is usually a bad idea.

This is why arrays and hashes have been invented. In your case you only need another array:

use warnings;
use strict;
use 5.010;

my @data_1 = (10,20,30,40,50);
my @data_2 = ("a" .. "e");
my @data_3 = (-10,-20,-30,-40,-50);
# put arrays into another array which you can access by index
my @data = \(@data_1, @data_2, @data_3);

for my $field (0 .. 2) {
    my $row = $data[ $field ]; 
    say $row->[0]; 
}

Does this work for you?

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to the pointer by tinita, I edited what I originally posted to closely match what you asked for as the output.

#!/usr/bin/perl

use warnings;
use strict;
use 5.16.3; 
# depending on what version your server is using, you would need to 
# declare this in order to use "say". On my server it would not allow
# the use of "say" unless I specifically declare which version it was 
# using.

my $output="Testing Iteration Output<br>Perl Version: " . $^V . "<br><br>"; 
# the $^V will help you find your version.

my @data_0 = (10,20,30,40,50);
my @data_1 = ("a" .. "e");
my @data_2 = (-10,-20,-30,-40,-50);

# put arrays into another array which you can access by index
# Note, I renamed the arrays to begin with 0, just for the reference in the output.
my @data = \(@data_0, @data_1, @data_2);

for my $field (0 .. 2) {
    my $row = $data[ $field ]; 
    $output.="iterator = \$data_" . $field . "<br>" . $row->[0] . "<br>"; 
}

print "Content-Type: text/html; charset=ISO-8859-1 \n\n";
print qq|$output|;
exit;

Then, this is what came as the output

Testing Iteration Output
Perl Version: v5.16.3

iterator = $data_0
10
iterator = $data_1
a
iterator = $data_2
-10

2 Comments

This is only working with package variables and not variables declared with my, and it doesn't work with use strict. So I wouldn't recommend this
Thank you for pointing that out. I appreciate it. I modified my answer to include your pointer, and to add the syntax the user was asking for. I hope that is ok with you. The way I wanted to show the output is to gather it as it's working, then print it at the end. I guess because I am always thinking about how the data might be used several times in the eventual output.

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.