Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uwm.edu!bionet!agate!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal L. Schwartz) Newsgroups: comp.lang.perl Subject: Re: using contents of a scalar variable as a variable name Message-ID: <1991Apr12.023355.2449@iwarp.intel.com> Date: 12 Apr 91 02:33:55 GMT References: Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal L. Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 35 In-Reply-To: knodel@news.colorado.edu (KNODEL DAVID) In article , knodel@news (KNODEL DAVID) writes: | | In a perl application I am writing, I would find it extremely useful | to reference variables whose names are stored in an array. The only | way which consistently worked was to use an eval statement. For example, | if I have an array: | | @names=("john","joe","paul"); | | and I want to assign values to the variables whose names appear | in the array (in this case, $john, $joe, and $paul). I found that | I could reference them via an eval statement, like: | print eval("\$$names[0]"); | | but assigning to them via this method was a little gross: | | eval("\$$names[0] = \"something\"); | | Is there a better way to reference these variables? Sure, use symbolname assignment (or whatever it's called...): *NAME = $names[0]; $NAME = "something"; Warning: this hides @NAME, %NAME, &NAME, and the NAME filehandle (did I leave anything out???), so use carefully. *NAME = *STDOUT; print NAME "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/