Perl: array name tha contains contents of a variable

Hi there

I have a counter called

my $counter = 0;

I am trying to build an array that will have a name that is for example

my @array0 = ("some", "stuff");

but instead of hard coding the "0" in the array name i want to use whatever value the aforementioned $counter has in it...so

my @array$counter = ("some", "stuff");

but this clearly doesnt work

Is there a way of doing this ?

Hi hcclnoodles,

I don't know why you could want to do that. I'm sure it is a red flag in your program. Said that, here there is one way:

$ cat script.pl                                                                                                                                                                                                            
#!/usr/bin/env perl                                                                                                                                                                                                                          
                                                                                                                                                                                                                                             
use strict;                                                                                                                                                                                                                                  
use warnings;                                                                                                                                                                                                                                
                                                                                                                                                                                                                                             
{
        my $counter = 0;

        my $name = 'array' . $counter;

        no strict;
        no warnings;
        @{$name} = qw<some stuff>;

        printf qq|%s\n|, join q|,|, @array0;
}
$ perl script.pl
some,stuff