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

Hi Guys I want to create 100 arrays named H1 to H100. Can i create them using for loop. Please suggest

Replies are listed 'Best First'.
Re: creating 100 arrays
by davido (Cardinal) on Jul 04, 2012 at 17:08 UTC

      davido: I certainly agree with your points about arrays-of-arrays, hashes, etc., but if saurabh2k26 still wants to go the global variable route,  use vars ...; (see vars) is another way to create default-initialized arrays (and package – i.e., global – variables in general). (Non-default initialization must be done separately.) It also avoids generation of various pesky warnings down the line.

      >perl -wMstrict -le "use constant AYCHES => 1 .. 100; use vars map '@H' . $_, AYCHES; ;; for(AYCHES) { no strict 'refs'; @{'H' . $_} = (0 .. $_); } ;; print qq{'$H50[50]' '$H100[-1]'}; " '50' '100'
Re: creating 100 arrays
by Khen1950fx (Canon) on Jul 05, 2012 at 01:31 UTC

    Note: I did this for "entertainment" purposes. Don't use it in production:). I would want to use a hash instead of an 100 arrays.

    You'll need to install List::Gen.
    #!/usr/bin/perl -l use strict; use warnings; use List::Gen ':all'; print '@' . "H@$_" for every 1 => 1 .. 100;
Re: creating 100 arrays
by talexb (Chancellor) on Jul 05, 2012 at 12:54 UTC

    You know about auto-vivification, right? You could just start writing your code and not worry about whether the array you want to use exists or not. When it's needed, auto-vivification will cause it to spring into existence.

    Can you explain in more detail what you're trying to do?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds