in reply to creating 100 arrays

Here is how you do it:

use strict; use warnings; for( 1 .. 100 ) { no strict 'refs'; @{'H' . $_} = (); }

And here's "Why it's stupid to `use a variable name as a variable name'" (in other words, why you shouldn't), in three parts:

  1. Part I: Why it's stupid to `use a variable name as a variable name'
  2. part II: A More Direct Explanation of the Problem
  3. part III: What if I'm Really Careful?

And here's what you probably should be doing instead:

use strict; use warnings; my @H; for( 1 .. 100 ) { push @H, []; }

The latter gives you an array, "@H" containing elements 0 through 99, each of which contains a reference to an array. In both cases, presumably you would actually have something to put into the arrays more meaningful than an empty list, so obviously these are just examples. Now it's time to read perllol.


Dave

Replies are listed 'Best First'.
Re^2: creating 100 arrays
by AnomalousMonk (Archbishop) on Jul 04, 2012 at 17:45 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'