Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

array naming

by jrsmith (Pilgrim)
on Aug 14, 2000 at 22:37 UTC ( [id://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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2025-03-25 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (67 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.