Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Name of variable is value of other variable

Reply
Thread Tools

Name of variable is value of other variable

 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-10-2004
Bart Van der Donck wrote:
> How can I rewrite this code so that it works for whatever value $pp
> may hold ?
>
> if ($pp eq 'xx') { $myvar = $xx }
> if ($pp eq 'yy') { $myvar = $yy }
> if ($pp eq 'zz') { $myvar = $zz }
>
> Basically, I am looking for something like:
> $myvar = $"$pp";
>
> Hashes seem the best choice, but I didn't find a way to put a
> variable's name in it.


%hash = ( xx => 1, yy => 2, zz => 3 );
$myvar = $hash{$pp};

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      09-10-2004
How can I rewrite this code so that it works for whatever value $pp
may hold ?

if ($pp eq 'xx') { $myvar = $xx }
if ($pp eq 'yy') { $myvar = $yy }
if ($pp eq 'zz') { $myvar = $zz }

Basically, I am looking for something like:
$myvar = $"$pp";

Hashes seem the best choice, but I didn't find a way to put a
variable's name in it.

Background: this goes back to a complex database construction that I
have to work with.

Thanks,
Bart
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-10-2004
Bart Van der Donck wrote:
> How can I rewrite this code so that it works for whatever value $pp
> may hold ?
>
> if ($pp eq 'xx') { $myvar = $xx }
> if ($pp eq 'yy') { $myvar = $yy }
> if ($pp eq 'zz') { $myvar = $zz }
>
> Basically, I am looking for something like:
> $myvar = $"$pp";



NOT AGAIN!
This question has been asked before. Actually it has been Asked Frequently
(hint, hint).
Did you check
perldoc -q "variable name"
Did you check recent postings to the same topic?
Did you ask aunt google (search for symbolic reference or symref)?

> Hashes seem the best choice,


Yes, they are.

> but I didn't find a way to put a
> variable's name in it.


You don't.
Use your own hash to store your data, not the the systems symbol table hash.

jue


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-10-2004
Bart Van der Donck <> wrote:

> Subject: Name of variable is value of other variable

^^^^ ^^^^^^^^

Those are known as "symbolic references", and they are Bad, they
can lead to hard-to-troubleshoot bugs so they should be avoided.

You are expected to check the Perl FAQ *before* posting to the
Perl newsgroup you know.

perldoc -q name
perldoc -q variable

How can I use a variable as a variable name?


> How can I rewrite this code so that it works for whatever value $pp

^^^^^^^^^^^^^^
> may hold ?



That is impossible.

$pp may hold the name of a variable that does not exist...

$pp may hold the name of a lexical variable (symrefs don't work
on lexicals).


> if ($pp eq 'xx') { $myvar = $xx }
> if ($pp eq 'yy') { $myvar = $yy }
> if ($pp eq 'zz') { $myvar = $zz }
>
> Basically, I am looking for something like:
> $myvar = $"$pp";



If you _did_ want to use a symbolic reference (but you don't!),
then it would be:

$myvar = $$pp;


> Hashes seem the best choice, but I didn't find a way to put a
> variable's name in it.



The variable's "name" becomes the hash's "key":

if ($pp eq 'xx') { $myvar = $hash{xx} }


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      09-11-2004
Tad wrote:

> If you _did_ want to use a symbolic reference (but you don't!),
> then it would be:
> $myvar = $$pp;


I imagined $"$pp" (see previous) and it's amazing how perl pre-felt
that kind of reference I was thinking about.

Despite your advice, I 'm gonna take my chances on $$pp. I believe
this is most suited to what I 'm up to (my initial code was only a
simplification).

thanks Tad & the others
Bart
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-11-2004
>>>>> "BVdD" == Bart Van der Donck <> writes:

BVdD> Tad wrote:
>> If you _did_ want to use a symbolic reference (but you don't!),
>> then it would be:
>> $myvar = $$pp;


BVdD> I imagined $"$pp" (see previous) and it's amazing how perl pre-felt
BVdD> that kind of reference I was thinking about.

BVdD> Despite your advice, I 'm gonna take my chances on $$pp. I believe
BVdD> this is most suited to what I 'm up to (my initial code was only a
BVdD> simplification).

that isn't advice but very sage wisdom that you are ignoring. it is like
buying a gun and turning down a safety training course. you will shoot
yourself in the foot (or worse) if you use symrefs for ordinary data.

maybe this will convince you. do you realize that using symrefs is just
using the symbol table as a hash tree? and that the symbol table is just
a hash with special side effects? so using a regular hash is safer,
simpler, more flexible (you can have lexical hashes, assign anything to
any slot), handle references better, etc.

the ONLY reason to mung the symbol table is when you need to mung the
symbol table. it is NOT meant for use as a general purpose hash
tree. that is why strict disallows it.

now put down the gun and step back from the keyboard. listen to what
all experienced perl hackers will tell you. DO NOT USE SYMREFS FOR
ORDINARY DATA. do you get it? it is NOT simpler however much your brain
says it is.

you have been warned.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-11-2004
Bart Van der Donck <> wrote:
> Tad wrote:
>
>> If you _did_ want to use a symbolic reference (but you don't!),


> Despite your advice, I 'm gonna take my chances on $$pp. I believe
> this is most suited to what I 'm up to



You are making the wrong decision.

You don't need symrefs, you can do just what you want by
using a hash directly rather than shoe-horning your way
into the Symbol Table hash.

You will introduce hard-to-find bugs by using symrefs.

Just say No!


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-11-2004


Bart Van der Donck wrote:

> Tad wrote:
>
>
>>If you _did_ want to use a symbolic reference (but you don't!),
>>then it would be:
>> $myvar = $$pp;

>
> Despite your advice, I 'm gonna take my chances on $$pp. I believe
> this is most suited to what I 'm up to (my initial code was only a
> simplification).


Unlike Tad and Uri I am not fanatically opposed to symrefs (and/or
eval). There are cases where the convienence is worth the hazards
implicit in using such a powerfull and hard to control tools.

However I'm ~99% sure this is not such a case.

If you won't accept the advice not to use symrefs from people who always
advise against symrefs then please accept the advice of someone who
doesn't always advise against symrefs. Don't do it.

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-11-2004
>>>>> "BM" == Brian McCauley <> writes:

BM> Bart Van der Donck wrote:

>> Tad wrote:
>>
>>> If you _did_ want to use a symbolic reference (but you don't!),
>>> then it would be:
>>> $myvar = $$pp;

>>
>> Despite your advice, I 'm gonna take my chances on $$pp. I believe
>> this is most suited to what I 'm up to (my initial code was only a
>> simplification).


BM> Unlike Tad and Uri I am not fanatically opposed to symrefs (and/or
BM> eval). There are cases where the convienence is worth the hazards
BM> implicit in using such a powerfull and hard to control tools.

i would like to see such a case where the goal is not to mung the symbol
table and is only for data structure purposes. if the symtable is just a
hash, then symrefs are just an alternate syntax to mung hash trees. and
the minor possible golf benefits of the symref syntax over regular
hashes is not worth the danger. and as i and others have said many
times, regular hashes have many advantages over symrefs but symrefs have
only two features, it mungs the symbol table (very needed) and a diff
syntax from hashes (not important).

BM> If you won't accept the advice not to use symrefs from people who
BM> always advise against symrefs then please accept the advice of
BM> someone who doesn't always advise against symrefs. Don't do it.

so when do you advise their use?

and i hope we haven't lost this newbie to the 7th hells of symrefs.

we need more than mjd's var var stuff and the faq entry. i have posted
many times on this with stuff that isn't mentioned in either place
(notably that the symtable is just a hash tree with side effects). this
question is coming up way too often these days.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-11-2004


Uri Guttman wrote:

>>>>>>"BM" == Brian McCauley <> writes:

>
>
> BM> Bart Van der Donck wrote:
>
> >> Tad wrote:
> >>
> >>> If you _did_ want to use a symbolic reference (but you don't!),
> >>> then it would be:
> >>> $myvar = $$pp;
> >>
> >> Despite your advice, I 'm gonna take my chances on $$pp. I believe
> >> this is most suited to what I 'm up to (my initial code was only a
> >> simplification).

>
> BM> Unlike Tad and Uri I am not fanatically opposed to symrefs (and/or
> BM> eval). There are cases where the convienence is worth the hazards
> BM> implicit in using such a powerfull and hard to control tools.
>
> i would like to see such a case where the goal is not to mung the symbol
> table and is only for data structure purposes.


OK, there aren't any. I was just trying to con the OP into doing the
right thing.

Well that's not entirely true. I tend to think of symrefs and
eval(STRING) togeter and I was really thinking of eval.

I can't honestly recall the last time I ever used or suggested anyone
else should use a symref other than a CODEref of a GLOBref.

> BM> If you won't accept the advice not to use symrefs from people who
> BM> always advise against symrefs then please accept the advice of
> BM> someone who doesn't always advise against symrefs. Don't do it.
>
> so when do you advise their use?


The only time I ever suggest use of symrefs is other than for doing
AUTOLOAD/import stuff is that there are some cases where using a Perl
package (special hash) as a dispatch table can aid readability. Indeed
Perl does this itself in its implementation of OO.

> and i hope we haven't lost this newbie to the 7th hells of symrefs.


I hope so too.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a variable name as the value of another variable. rdstevenson@hotmail.co.uk ASP General 6 10-13-2007 02:51 PM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Use a variable value in another variable's name Robin Corcoran Perl Misc 20 07-26-2004 12:06 PM
create a variable name with the value of another variable Brian C++ 3 04-19-2004 04:35 PM
How to get Variable value not Variable name. Pete Mahoney Javascript 11 07-25-2003 01:02 AM



Advertisments