Home > Archive > PERL Beginners > September 2006 > using a variable to create a new variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
using a variable to create a new variable
|
|
| happytuna@gmail.com 2006-09-04, 9:57 pm |
| hi,
i'd like to create a new variable by using another variable name:
for example. i would like to create a variable called "$gumbo_new" by
using "$string":
$string = "gumbo";
${{$string}_new} = "yummy"; #$gumbo_new = "yummy"
the above of course doesn't work. is there a way to do this?
thanks in advance
| |
| Paul Lalli 2006-09-04, 9:57 pm |
| happytuna@gmail.com wrote:
> i'd like to create a new variable by using another variable name:
No you wouldn't. You *think* that's what you want to do. What you
actually want to do, you haven't told us, and instead are asking for
help with what you think is a good way to solve your problem. It's
not. This is called an XY problem.
Please see: perldoc -q "variable name"
> for example. i would like to create a variable called "$gumbo_new" by
> using "$string":
>
> $string = "gumbo";
>
> ${{$string}_new} = "yummy"; #$gumbo_new = "yummy"
>
> the above of course doesn't work. is there a way to do this?
Yes. But again, you don't want to. What you want to do is use hashes.
my %new;
my $string = "gumbo";
$new{$string} = "yummy";
Paul Lalli
| |
| happytuna@gmail.com 2006-09-05, 3:57 am |
| haha... actually yes i would. but thanks for your help anyway!
Paul Lalli wrote:
> happytuna@gmail.com wrote:
>
> No you wouldn't. You *think* that's what you want to do. What you
> actually want to do, you haven't told us, and instead are asking for
> help with what you think is a good way to solve your problem. It's
> not. This is called an XY problem.
>
> Please see: perldoc -q "variable name"
>
>
> Yes. But again, you don't want to. What you want to do is use hashes.
>
> my %new;
> my $string = "gumbo";
> $new{$string} = "yummy";
>
> Paul Lalli
| |
| Uri Guttman 2006-09-05, 3:57 am |
| >>>>> "h" == happytuna <happytuna@gmail.com> writes:
h> haha... actually yes i would. but thanks for your help anyway!
no you don't. please learn this lesson, symrefs (what you are trying to
do) are for munging the symbol table. they are NOT for use as a data
structure which is what you are trying to do. the symbol table is just a
hash tree like any you can create but its values affect the symbols used
by perl. since no data structure needs the useless side effect of
munging symbols, why would you do that? also regular hash trees are
safer (total isolation, the symbol table is global), easier to pass
around, easier to copy and manipulate.
do you still want to use symrefs? do you have any valid reason to defend
your need? do you understand hash trees?
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| usenet@DavidFilmer.com 2006-09-05, 3:57 am |
| happytuna@gmail.com wrote:
> haha... actually yes i would. but thanks for your help anyway!
Paul and Uri have forgotten more Perl than I will ever know. I hope
you will take the advice of these expert programmers (and me, FWIW) and
use hashes, as Paul advised. Unless you WANT to write crap code.
--
David Filmer (http://DavidFilmer.com)
| |
| happytuna@gmail.com 2006-09-05, 7:57 am |
| i do understand hashes and in this instance i don't want to use them.
i do appreciate the coding advice. but i would like to use another
variable to name another one in this case.
usenet@DavidFilmer.com wrote:
> happytuna@gmail.com wrote:
>
> Paul and Uri have forgotten more Perl than I will ever know. I hope
> you will take the advice of these expert programmers (and me, FWIW) and
> use hashes, as Paul advised. Unless you WANT to write crap code.
>
> --
> David Filmer (http://DavidFilmer.com)
| |
| Paul Lalli 2006-09-05, 7:57 am |
| happytuna@gmail.com wrote:
> i do understand hashes and in this instance i don't want to use them.
> i do appreciate the coding advice. but i would like to use another
> variable to name another one in this case.
Have you read the perldoc I pointed you to yet? While it contains the
same warning that three different people have given you now, it also
contains the answer you continue to foolishly request.
WHY do you think you want to do this?! Post your *real* problem, that
you think 'using a variable to name another variable' is the answer to,
and we can show you both *why* that is a poor solution, and how to
implement the real solution! Why are you stubbornly refusing to do
this?
Paul Lalli
| |
| Uri Guttman 2006-09-05, 6:57 pm |
| >>>>> "h" == happytuna <happytuna@gmail.com> writes:
h> i do understand hashes and in this instance i don't want to use them.
h> i do appreciate the coding advice. but i would like to use another
h> variable to name another one in this case.
that is not a valid reason. your wanting to do it is not an excuse for
bad coding. if you really want bad code then we can't stop you.
so try to explain WHY you think symrefs are a good idea here. show some
example code where you think it will help. you must have some thoughts
on it beyond your wanting to do it this way. if you don't then we can't
do anything more to help now. you are welcome to come back when your
program starts to exhibit nasty bugs and becomes impossible to
maintain. you seem to be on the path of 'i will do it the wrong way, not
listen to good advice and will learn my lesson later when i get burnt
badly'
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| happytuna@gmail.com 2006-09-06, 7:57 am |
| i suppose i could do it with hashes... but i really don't want to. i
just wanted to know how i could create new variable names using other
variables, if that was possible at all... i've done it using tcl, but
the syntax isnt the same.
i would prefer to see what happens when i do it the "wrong way" (but i
still dont know how to implement the wrong way!). just like you all
have had 1st hand experience with doing it the wrong way, i would like
to see it myself.
no problema if you don't want to post bad coding advice on the group...
it makes sense. anyways thanks again
| |
| Paul Lalli 2006-09-06, 7:57 am |
| happytuna@gmail.com wrote:
> i suppose i could do it with hashes... but i really don't want to.
Yet you continue to refuse to tell us WHY!!!
Do you even *KNOW* why at this point? Or are you just wearing
blinders?
> i
> just wanted to know how i could create new variable names using other
> variables, if that was possible at all... i've done it using tcl, but
> the syntax isnt the same.
>
> i would prefer to see what happens when i do it the "wrong way" (but i
> still dont know how to implement the wrong way!). just like you all
> have had 1st hand experience with doing it the wrong way, i would like
> to see it myself.
Are you kidding me? Are you even *reading* these responses? I've
already told you, twice now, where to find the answer. Are you
refusing to do any research on your own? I pointed you to it in my
first response in this thread, then I reminded you about it in my
second response. No, I will not copy and paste the perldoc for you
here. That would be wasteful.
> no problema if you don't want to post bad coding advice on the group...
> it makes sense. anyways thanks again
Unbelievable.
Paul Lalli
| |
| Uri Guttman 2006-09-06, 7:57 am |
| >>>>> "h" == happytuna <happytuna@gmail.com> writes:
h> i suppose i could do it with hashes... but i really don't want to. i
h> just wanted to know how i could create new variable names using other
h> variables, if that was possible at all... i've done it using tcl, but
h> the syntax isnt the same.
there is no need for you to know it. none. nada. nil. so drop that
thought from your head. symrefs are for munging the symbol table and
very little code needs to do that. since perl hash proper hashes there
is no need for symrefs to do data munging. no reason. get it?
h> i would prefer to see what happens when i do it the "wrong way" (but i
h> still dont know how to implement the wrong way!). just like you all
h> have had 1st hand experience with doing it the wrong way, i would like
h> to see it myself.
nothing happens but BAD CODE. you can get symrefs to work just fine. and
if you rtfm'ed even a little you could learn the syntax as it is not
hidden (though i would like it to be given newbie wackiness like yours).
do you want to know what it feels like to be stabbed or would you trust
me (who has never been stabbed) to tell you it is a bad thing? i have
not been bitten by nasty symrefs in any perl i have written since i
don't use them (except as i said to mung symbol tables which i have
done). but i do know why they are bad and so i obey that rule. so if you
want to break every rule of life just to experience it yourself, go
ahead. but don't be coming here asking for moral or technical
support. bad code is bad code and you should avoid writing it. you have
been told why it is bad and a proper way to do it. how much more do you
need? and rtfm as you have been asked already.
h> no problema if you don't want to post bad coding advice on the group...
h> it makes sense. anyways thanks again
and do you still think it is a good idea to keep asking how to do it
when it is bad coding advice? and if you wanted to learn how it would be
easy to rtfm? as paul said, unbelievable.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| fatal.serpent 2006-09-06, 6:57 pm |
| Sooo many people want you to read the page, and you don't seem to be;
so I've looked it up and am now posting it.
Found in /usr/libdata/perl5/pod/perlfaq7.pod
How can I use a variable as a variable name?
Beginners often think they want to have a variable contain
the name of a variable.
$fred = 23;
$varname = "fred";
++$$varname; # $fred now 24
This works sometimes, but it is a very bad idea for two
reasons.
The first reason is that this technique only works on
global variables. That means that if $fred is a lexical
variable created with my() in the above example, the code
wouldn't work at all: you'd accidentally access the global
and skip right over the private lexical altogether.
Global variables are bad because they can easily collide
accidentally and in general make for non-scalable and con-
fusing code.
Symbolic references are forbidden under the "use strict"
pragma. They are not true references and consequently are
not reference counted or garbage collected.
The other reason why using a variable to hold the name of
another variable is a bad idea is that the question often
stems from a lack of understanding of Perl data struc-
tures, particularly hashes. By using symbolic references,
you are just using the package's symbol-table hash (like
%main::) instead of a user-defined hash. The solution is
to use your own hash or a real reference instead.
$USER_VARS{"fred"} = 23;
$varname = "fred";
$USER_VARS{$varname}++; # not $$varname++
There we're using the %USER_VARS hash instead of symbolic
references. Sometimes this comes up in reading strings
from the user with variable references and wanting to
expand them to the values of your perl program's vari-
ables. This is also a bad idea because it conflates the
program-addressable namespace and the user-addressable
one. Instead of reading a string and expanding it to the
actual contents of your program's own variables:
$str = 'this has a $fred and $barney in it';
$str =~ s/(\$\w+)/$1/eeg; # need double eval
it would be better to keep a hash around like %USER_VARS
and have variable references actually refer to entries in
that hash:
perl v5.8.6 2006-09-06 1
CMVHBECAVX(1) User Contributed Perl Documentation CMVHBECAVX(1)
$str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all
That's faster, cleaner, and safer than the previous
approach. Of course, you don't need to use a dollar sign.
You could use your own scheme to make it less confusing,
like bracketed percent symbols, etc.
$str = 'this has a %fred% and %barney% in it';
$str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all
Another reason that folks sometimes think they want a
variable to contain the name of a variable is because they
don't know how to build proper data structures using
hashes. For example, let's say they wanted two hashes in
their program: %fred and %barney, and that they wanted to
use another scalar variable to refer to those by name.
$name = "fred";
$$name{WIFE} = "wilma"; # set %fred
$name = "barney";
$$name{WIFE} = "betty"; # set %barney
This is still a symbolic reference, and is still dled
with the problems enumerated above. It would be far bet-
ter to write:
$folks{"fred"}{WIFE} = "wilma";
$folks{"barney"}{WIFE} = "betty";
And just use a multilevel hash to start with.
The only times that you absolutely must use symbolic ref-
erences are when you really must refer to the symbol
table. This may be because it's something that can't take
a real reference to, such as a format name. Doing so may
also be important for method calls, since these always go
through the symbol table for resolution.
In those cases, you would turn off "strict 'refs'" tem-
porarily so you can play around with the symbol table.
For example:
@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs'; # renege for the block
don't know how to build proper data structures using
hashes. For example, let's say they wanted two hashes in
their program: %fred and %barney, and that they wanted to
use another scalar variable to refer to those by name.
$name = "fred";
$$name{WIFE} = "wilma"; # set %fred
$name = "barney";
$$name{WIFE} = "betty"; # set %barney
This is still a symbolic reference, and is still dled
with the problems enumerated above. It would be far bet-
ter to write:
$folks{"fred"}{WIFE} = "wilma";
$folks{"barney"}{WIFE} = "betty";
And just use a multilevel hash to start with.
The only times that you absolutely must use symbolic ref-
erences are when you really must refer to the symbol
table. This may be because it's something that can't take
a real reference to, such as a format name. Doing so may
also be important for method calls, since these always go
through the symbol table for resolution.
In those cases, you would turn off "strict 'refs'" tem-
porarily so you can play around with the symbol table.
For example:
@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs'; # renege for the block
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
}
All those functions (red(), blue(), green(), etc.) appear
to be separate, but the real code in the closure actually
was compiled only once.
So, sometimes you might want to use symbolic references to
perl v5.8.6 2006-09-06 2
CMVHBECAVX(1) User Contributed Perl Documentation CMVHBECAVX(1)
directly manipulate the symbol table. This doesn't matter
for formats, handles, and subroutines, because they are
always global--you can't use my() on them. For scalars,
arrays, and hashes, though--and usually for subroutines--
you probably only want to use hard references.
(END)
There you go. Now you don't have to look it up.
happytuna@gmail.com wrote:
> hi,
> i'd like to create a new variable by using another variable name:
>
> for example. i would like to create a variable called "$gumbo_new" by
> using "$string":
>
> $string = "gumbo";
>
> ${{$string}_new} = "yummy"; #$gumbo_new = "yummy"
>
> the above of course doesn't work. is there a way to do this?
>
> thanks in advance
|
|
|
|
|