-6

I have read this post and saw that there are various ways to create a dynamic naming of arrays in perl. http://en.allexperts.com/q/Perl-CGI-1045/dynamic-naming-array-1.htm

as per the link, the code will create dynamic arrays like these:

@yearsSoFar2004, 
@yearsSoFar2005,                                                                               
@yearsSoFar2006 etc.

My requirement is as follows. I want to create a dynamic array like this:

my @a0=();
my @a1=();
my @a2=();
my @a3=();

I currently have it static in my code but i want to make it dynamic. Here is the static code below. Please help me guys. I am a newbie to PERL

my @a=();
my @b=();
my @c=();
my @d=();
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Ash
  • 1
  • 2
  • 2
    The name of the language is "Perl", not "PERL" or "perl" (though the latter can be used to refer to the executable). – ikegami Aug 21 '15 at 21:58
  • 1
    @ikegami- i just saw ur profile. Soreadytohelp..... it said... you should think of correcting that.. you can write as follows. Onlyreadytofindfaults – Ash Aug 21 '15 at 22:03
  • 2
    How is showing you the correct spelling of the language you are trying to learn not helpful?! – ikegami Aug 21 '15 at 22:08
  • @ikegami- the spelling is right. Perl, PERL and perl are all spelt the same way. I would rather need your help in finding me a solution for this. If at all there is a solution. Please help if you are soreadytohelp – Ash Aug 21 '15 at 22:09
  • 2
    [No](https://www.perl.org/about/style-guide.html). It's not an abbreviation. – ikegami Aug 21 '15 at 22:13
  • @ikegami- okay, Perl... got it. :) will keep a note of it from now on... – Ash Aug 21 '15 at 22:15

2 Answers2

3

Three problems with your request:

  1. my declares a lexical variable at compile-time, so asking to pass a name to my at run-time makes no sense.
  2. Symbolic references can't be used to access lexical variables.
  3. It's a stupid thing to do.
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • @ikegami- Agreed, its stupid thing to do this way. but can i have a solution for this please. The code has been developed already and i need to make it dynamic now on that same code. – Ash Aug 21 '15 at 22:01
  • 1
    I gave you two reasons why it's impossible. I'm not sure what you hope to gain by pleading. – ikegami Aug 21 '15 at 22:08
  • 1
    @ikegami- thanks for the ultimatum. i was just not sure if i could make it dynamic or not. will have to find other way then. – Ash Aug 21 '15 at 22:11
  • 2
    If you tell us what you are actually trying to accomplish, we can help you with that. – ikegami Aug 21 '15 at 22:13
2

My requirement is as follows. I want to create a dynamic array like this:

my @a0=();
my @a1=();
my @a2=();
my @a3=();

As I have said before, "When you find yourself adding an integer suffix to variable names, think I should have used an array."

So, instead, use

my @data = (
    [ ... ],
    [ ... ],
    [ ... ],
);

If you wanted index each year's data array by the year (instead of integers 0, 1, 2, ..., n), then use a hash:

my %data = (
    2005 => [ ... ],
    2006 => [ ... ],
    2007 => [ ... ],
);

What you have now is compounded stupidity. Don't do that.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339