For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > need help building variables dynamically









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 need help building variables dynamically
Jm

2006-11-01, 6:57 pm

i'm trying to build variables dynamically from data passed to a
subroutine. i pass a text value (a field from a database) and want to
build an associated variable name by appending a prefix to the field
name ($oob_<fieldname> ). that constructed variable would then acquire
the value of the variable as defined prior to calling the sub.

################################
#!/usr/bin/perl

my $oob_state = "IL";
my $oob_lata = "732";
my $oob_name = "SomeName";

build_vars("state", "lata", "name");

sub build_vars
{
while (my $field = shift)
{
my $variable = '$oob_' . $field;
my $var = \$variable;
my $var2 = $$var;
print "\nfield = \"$field\"\nvariable = \"$variable\" = \"$var\" ($var2)\n";
}
}
################################

which gives the following results:

field = "state"
variable = "$oob_state" = "SCALAR(0x804ce58)" ($oob_state)

field = "lata"
variable = "$oob_lata" = "SCALAR(0x804ce58)" ($oob_lata)

field = "name"
variable = "$oob_name" = "SCALAR(0x804ce58)" ($oob_name)


what i want is this:

field = "state"
variable = "$oob_state" = "IL"

field = "lata"
variable = "$oob_lata" = "732"
--
field = "name"
variable = "$oob_name" = "SomeName"


manipulations like these have always given me headaches; all thanks
for any help.

joe

since this is a gmail account, please verify the mailing list is
included in the reply to addresses
DJ Stunks

2006-11-01, 6:57 pm


Jm wrote:
> i'm trying to build variables dynamically from data passed to a
> subroutine.


> <snip>


> manipulations like these have always given me headaches; all thanks
> for any help.


it gives you (and everyone else) a headache because it's a Bad Idea.

please read perldoc -q "How can I use a variable as a variable name?"

use a hash and put away the Excedrin.

-jp

John W. Krahn

2006-11-01, 6:57 pm

jm wrote:
> i'm trying to build variables dynamically from data passed to a
> subroutine. i pass a text value (a field from a database) and want to
> build an associated variable name by appending a prefix to the field
> name ($oob_<fieldname> ). that constructed variable would then acquire
> the value of the variable as defined prior to calling the sub.
>
> ################################
> #!/usr/bin/perl
>
> my $oob_state = "IL";
> my $oob_lata = "732";
> my $oob_name = "SomeName";
>
> build_vars("state", "lata", "name");
>
> sub build_vars
> {
> while (my $field = shift)
> {
> my $variable = '$oob_' . $field;
> my $var = \$variable;
> my $var2 = $$var;
> print "\nfield = \"$field\"\nvariable = \"$variable\" = \"$var\"
> ($var2)\n";
> }
> }
> ################################


Use a hash:

my %oob = (
state => 'IL',
lata => 732,
name => 'SomeName',
);

If you don't think that you need a hash then be aware that what you are trying
to do is actually using a hash anyways (the %main:: hash.)



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Tom Phoenix

2006-11-01, 6:57 pm

On 11/1/06, jm <jm5379@gmail.com> wrote:

> i'm trying to build variables dynamically from data passed to a
> subroutine. i pass a text value (a field from a database) and want to
> build an associated variable name by appending a prefix to the field
> name ($oob_<fieldname> ).


Perl allows you to do this. (Perl allows many things which may be
either clever or stupid depending upon context.) But to use a hash is
better in every way. This page explains in more detail:

http://perl.plover.com/varvarname.html

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Jm

2006-11-01, 6:57 pm

On 11/1/06, John W. Krahn <krahnj@telus.net> wrote:

>
> Use a hash:
>
> my %oob = (
> state => 'IL',
> lata => 732,
> name => 'SomeName',
> );
>
> If you don't think that you need a hash then be aware that what you are trying
> to do is actually using a hash anyways (the %main:: hash.)
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


thanks to all for the replies. i think i was probably too concise in
my explanation; i'll try to elaborate without going overboard...

the current code goes like this:

&build_oracle_args_ne(FIELD => "ooba_element_host_name", VARIABLE =>
$ne_ooba_element_host_name);
# and several other similar lines of code

sub build_oracle_args_ne
{
my %options = @_;
# $options{FIELD} comma-separated list of fields to build into oracle argument
# $options{DB_ARGS} string of Oracle field comparisons to be
dynamically constructed
# $options{VARIABLE} variable to compare to ORACLE field

# $oracle_args_oob defined elsewhere in program as a "global" variable
$oracle_args_ne .= ($oracle_args_ne =~ /where/ ? " and " : "where ")
.. "upper($options{FIELD}) like '%" . uc($options{VARIABLE}) . "%'" if
$options{VARIABLE} ne "";
} # end of sub build_oracle_args_ne

#####################
the purpose is to dynamically create a list of fields and values (from
a web interface) to compare in an oracle database (it has to be
dynamic due to the need to search any field, as well as the fact that
the person creating the database built 3 tables which store the same
data but with slightly different field structures/sequences -
different problem, just some background). the result would be
something like

"... where upper(state) like '%IL%' and upper(lata) like '%732%' ..."

for however many fields had been populated in the form. in the
updated routine, i wanted to pass only the oracle fieldname and create
the variable that held the desired data to build the sql query, not
pass the variable as a second argument, either as a hash or another
scalar.

i'll look at hashes but i don't think that's the best solution for
this. if someone could, just for arguments sake, tell me how to do
this the "wrong" way, i'd really appreciate it, just for knowing
another alternative, whether i actually do that or not.

thanks again.

joe


--
since this is a gmail account, please verify the mailing list is
included in the reply to addresses
Mumia W.

2006-11-01, 6:57 pm

On 11/01/2006 01:44 PM, jm wrote:
> On 11/1/06, John W. Krahn <krahnj@telus.net> wrote:
>
>
> thanks to all for the replies. i think i was probably too concise in
> my explanation; i'll try to elaborate without going overboard...
>
> the current code goes like this:
>
> &build_oracle_args_ne(FIELD => "ooba_element_host_name", VARIABLE =>
> $ne_ooba_element_host_name);
> # and several other similar lines of code
>
> sub build_oracle_args_ne
> {
> my %options = @_;
> # $options{FIELD} comma-separated list of fields to build
> into oracle argument
> # $options{DB_ARGS} string of Oracle field comparisons to be
> dynamically constructed
> # $options{VARIABLE} variable to compare to ORACLE field
>
> # $oracle_args_oob defined elsewhere in program as a "global"
> variable
> $oracle_args_ne .= ($oracle_args_ne =~ /where/ ? " and " : "where ")
> . "upper($options{FIELD}) like '%" . uc($options{VARIABLE}) . "%'" if
> $options{VARIABLE} ne "";
> } # end of sub build_oracle_args_ne
>
> #####################
> the purpose is to dynamically create a list of fields and values (from
> a web interface) to compare in an oracle database (it has to be
> dynamic due to the need to search any field, as well as the fact that
> the person creating the database built 3 tables which store the same
> data but with slightly different field structures/sequences -
> different problem, just some background). the result would be
> something like
>
> "... where upper(state) like '%IL%' and upper(lata) like '%732%' ..."
>
> for however many fields had been populated in the form. in the
> updated routine, i wanted to pass only the oracle fieldname and create
> the variable that held the desired data to build the sql query, not
> pass the variable as a second argument, either as a hash or another
> scalar.
>
> i'll look at hashes but i don't think that's the best solution for
> this. if someone could, just for arguments sake, tell me how to do
> this the "wrong" way, i'd really appreciate it, just for knowing
> another alternative, whether i actually do that or not.
>
> thanks again.
>
> joe
>
>


Make sure the perl documentation and its utitilty, perldoc, are
installed on your system and, at a command prompt, type these:

perldoc -q "variable as"
perldoc perl


Jm

2006-11-02, 7:56 am

thanks to everyone for the suggestions


On 11/1/06, Mumia W. <mumia.w.18.spam+nospam@earthlink.net> wrote:
> On 11/01/2006 01:44 PM, jm wrote:
>
> Make sure the perl documentation and its utitilty, perldoc, are
> installed on your system and, at a command prompt, type these:
>
> perldoc -q "variable as"
> perldoc perl
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
since this is a gmail account, please verify the mailing list is
included in the reply to addresses
Uri Guttman

2006-11-02, 9:57 pm

>>>>> "J" == Jm <jm5379@gmail.com> writes:

J> thanks to everyone for the suggestions

you should only thank those who told you not to use symrefs. you seem
bent on that evil path and you aren't listening. as someone else said,
symrefs are still using a hash, just a special one with different
syntax. you think this is a good thing but there are no benefits to
using symrefs for basic data munging and plenty of negative issues. this
question comes up all the time from newbies and we have to beat it out
of their branes with a large cluebat. just don't do it with symrefs,
use a hash. if you have trouble with a hash, ask here how to do it.

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com