tsvik_t has asked for the wisdom of the Perl Monks concerning the following question:

Hi, how can I create variables dymanicly, according to the data I get when I run the script? Thanks Tsvika.

Replies are listed 'Best First'.
Re: dynamic variables
by Biker (Priest) on Dec 20, 2001 at 18:01 UTC

    You should probably use a hash to store your data. In a hash you decide the 'names' (i.e. the keys) to your data.

    Update: You may also want to follow this thread to read more about it.

    "Livet är hårt" sa bonden.
    "Grymt" sa grisen...

Re: dynamic variables
by Juerd (Abbot) on Dec 20, 2001 at 18:13 UTC
    It's possible, but you don't want to. Instead, you want to use a hash. You can think of the elements in the hash as if they're variables on their own.

    $foo = 'bar'; $hash{$foo} = "Hello, World!\n"; print $hash{bar}; # prints Hello, World! and a linefeed


    (Note: Variables are put in a hash themselves (%package:: (or just %::)), You could use ${$foo} = "Hello, World!\n" (maybe ${package::}{foo} and $::{foo} can help understanding this) and then print $bar, but it's ugly. Fortunately, use strict prevents you from doing so (unless you use no strict 'refs'))

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$