in reply to Declaring variables recursively

I would like to declare variables recursively, this way I could save lots of time in associating data of arrays hashes etc. What I would like to to is the following: foreach $name (@array){ ${$name}=X } is it possible con perl?, because its posible with linux shell. Thanks
  1. yes, it's possible, and it fundamentally amounts to accessing the keys of one particular, special hash, namely the symbol table. These animals are called "symbolic references" (or more shortly, "symrefs").
  2. since you're manipulating a hash, just use a "real", generic one:
    $hash{$_} = X for @array;
    takes only a few more keystrokes... (well, actually less, but that's only because I used for as a statement modifier.)

    In particular there are tons of mails/posts/nodes/articles explaining why it is generally recommended not to use symrefs, which is also the reson why they're ruled out by use strict; (hey, you're using it, aren't you?)

  3. WRT "is it possible con perl": in inglese dovresti usare "with" ;-)

Replies are listed 'Best First'.
Re^2: Declaring variables recursively
by sciarnold (Initiate) on Feb 25, 2005 at 16:05 UTC
    >>3.WRT "is it possible con perl": in inglese dovresti usare "with" ;-) >>>> Right, but also in spanish :)-my mother tongue