l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 10: LUGOD Installfests coming again soon
Page last updated:
2001 Dec 30 17:04

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] perl question - variable holding a variable name
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] perl question - variable holding a variable name


  • Subject: Re: [vox-tech] perl question - variable holding a variable name
  • From: Henry House <hajhouseMAPS@houseag.com>
  • Date: Sat, 07 Apr 2001 14:24:01 -0700
  • References: 20010407132214.A11971@dirac.org

On Sat, Apr 07, 2001 at 01:22:14PM -0700, Peter Jay Salzman wrote:
[...]
> i'd like to read the file in and set variables according to what is in the
> file.  here's my attempt:
[Attempt to dereference by name stored in scalar snipped]
> the trouble is, $Legacy is undefined.   i was under the impression that in
> perl, if you have a variable $hello which stored the word "dolly", then you
> could say:
> 
>       my $$hello = "clementine";

I can't remember how to do this off the top of my head. This the answer to
this is in the Camel book, around page 15 if I recall correctly. Also try the
index entries for 'symbolic references' or 'soft references'. Here's
something similar that may help:

        #!/usr/bin/perl
        while ($LINE = <STDIN>) {
                if ($LINE =~ m/^(\w+)\s*=\s*(\w+)/) { &$1($2) }
        }
        sub mycmd { print $_[0] . "\n" }
        sub foo { print $_[0] . "\n" }

When run with input of:

        foo = bar
        mycmd = value

the output is:

        bar
        value

Sort of useless, but the possibilities should be obvious.

Out of curiosity, why don't you use a hash? That would be much easier!
Example:

        #!/uasr/bin/perl
        my %h;
        while ($LINE = <STDIN>) {
                if ($LINE =~ m/^(\w+)\s*=\s*(\w+)/) { $h{$1} = $2; }
        }

-- 
Henry House
OpenPGP key available from http://hajhouse.org/hajhouse.asc


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.