Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

creating 100 arrays

by saurabh2k26 (Initiate)
on Jul 04, 2012 at 17:00 UTC ( #979892=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://979892]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2022-08-14 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?