Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

array naming

by jrsmith (Pilgrim)
on Aug 14, 2000 at 22:37 UTC ( #27791=perlquestion: print w/ replies, xml ) Need Help??
jrsmith has asked for the wisdom of the Perl Monks concerning the following question:

here's a simple question- say i have a variable being read in from a flat file. how can i create an array using that variable as the name?

Comment on array naming
Re: array naming
by le (Friar) on Aug 14, 2000 at 22:43 UTC
    If I get your question right, you need something like this:
    $var = "some_stupid_name"; @{$var} = qw(foo bar baz); print @some_stupid_name;
    See 'perldoc perlref' for more information.
      Also, look at these articles as to why you shouldn't (or may not want to) do this.

      Cheers,
      KM

      thanx this is what i needed.. i'm proud of myself actually, this is what i had done more or less as a test to see if it would work, and it turned out to be right :)
Re: array naming
by chromatic (Archbishop) on Aug 14, 2000 at 22:45 UTC
    You'll be best off using a special kind of an array called a hash. perldata has more details.

    The difference is that instead of using numbers to get at elements of the array, you use text. Suppose you had a file like the following:

    location=dungeon name=corwin weapon=grayswandir
    You might read it in and put it in the hash as in this snippet:
    my %data = (); # initialize hash while (<INPUT>) { # assume you have the file open already my ($key, $value) = split(/=/, $_, 2); $data{$key} = $value; }
    You'll end up with $data{name} set to 'corwin' and so forth.

    Update: If you want to keep an actual array around, you can put a reference in a hash. See perlref and perldsc for more details:

    $data{$arrname} = \@array; # or $data{$arrname} = [ @array ];
    or some such nonsense.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-08-21 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (144 votes), past polls