Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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?

Replies are listed 'Best First'.
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.
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.


      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 :)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2016-10-26 11:01 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (340 votes). Check out past polls.