Evaluating nester variables

G

Geoff

I have an array:
@array = [\$a, \$b, \$c];

How do I evaluate what $a is? I've tried:
print $array[0];
But this gives me "$a".

I also tried using eval, but I must be using it wrong cause it doesn't
seem to do anything.

Thanks in advance.
 
G

Gunnar Hjalmarsson

Geoff said:
I have an array:
@array = [\$a, \$b, \$c];

It's an array with one element that consists of a reference to an
anonymous array with three scalar references.
How do I evaluate what $a is?

print ${$array[0][0]};
I've tried:
print $array[0];
But this gives me "$a".

Sounds very strange. It should give you something like
'ARRAY(0x15551bc)'. You'd better post some complete code that
illustrates the behaviour you describe.
 
M

Matija Papec

X-Ftn-To: Geoff

Geoff said:
I have an array:
@array = [\$a, \$b, \$c];

How do I evaluate what $a is? I've tried:
print $array[0];
But this gives me "$a".

Can you describe what do you want as a final result?
I heavily /guess/ that you really want to,
$aref = [$a, $b, $c];
print $aref->[0];
I also tried using eval, but I must be using it wrong cause it doesn't
seem to do anything.

Leave eval alone for now and go carefully trough perldoc perlref.
 
B

Ben Morrow

Matija Papec said:
Geoff said:
I have an array:
@array = [\$a, \$b, \$c];

How do I evaluate what $a is? I've tried:
print $array[0];
But this gives me "$a".

Can you describe what do you want as a final result?
I heavily /guess/ that you really want to,
$aref = [$a, $b, $c];
print $aref->[0];

Or perhaps simply

my @array = ($a, $b, $c);
print $array[0];

or then again maybe

my @array = (\$a, \$b, \$c);
print ${$array[0]};

Ben
 
G

Geoff

Alright, here's a code snippet:
<CODE>
@DB_FIELDS=(name phone etc);
my $fields = join (', ', @DB_FIELDS);
$i=0;

while ($i < $loops){
my $j=0;
foreach $field (@DB_FIELDS) {
@values[$j] = "\$" . $field . $i;
$j++;
}
....
$values .= join (', ', @values);
someFunction($db, $fields, $values);
$i++
}
</CODE>

So, $values should look like '$name0, $phone0, $etc0, $name1, $phone1,
$etc1, $name2...'.

This is used to build an SQL command. The command is:
<CODE>
my $insert_string = qq~INSERT INTO $db ($fields) VALUES ($values)~;
my $sth = $dbh->do("$insert_string") or croak $dbh->errstr;
</CODE>

This loops several times depending on the number of inputs from the user.

I'm doing this because I have no idea how many "$values" will be
inserting into the database because it's coming from a dynamic HTML form
that grows depending on the user's input.

Gunnar said:
Geoff said:
I have an array:
@array = [\$a, \$b, \$c];


It's an array with one element that consists of a reference to an
anonymous array with three scalar references.
How do I evaluate what $a is?


print ${$array[0][0]};
I've tried:
print $array[0];
But this gives me "$a".


Sounds very strange. It should give you something like
'ARRAY(0x15551bc)'. You'd better post some complete code that
illustrates the behaviour you describe.
 
U

Uri Guttman

gack!! use dbi bindings. and regular loops will do. there is no reason
for code generation there. at the worst you would need to generate some
sql but (almost) never perl in a dbi situation. go back and rethink your
design. your current code is nasty and brutish and way off base.

uri
 
G

Gunnar Hjalmarsson

Please study and comply with the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Alright, here's a code snippet:

A lot of things could be said about that code, but I'll try to limit
myself to the initial problem.

I suppose that it's the variable @values you are struggling with. Your
code populates it with:

@values = ('$name0', '$phone0', '$etc0');

which is something quite different compared to

@values = [\$name0, \$phone0, \$etc0];

So now we know that you are not dealing with references at all.

Your initial question was (the equivalent of): "How do I evaluate what
$name0 is?". Now my question to you is: Do you anywhere in your code
assign anything to the variable $name0? If you don't, there isn't much
to evaluate, is there?

As a sidenote, you would probably need symbolic references to assign
anything to that and other similar variables, and that approach is not
advisable.

But that aside, assuming that you somewhere in your program has:

$name0 = 'Some name';

you should be able to access the content of $name0 by saying:

print eval $values[0];

But I think you have a lot of debugging to do.

HTH
 
G

Geoff

OK. Well, the string that comes from the HTML looks like so:
action=add&name1=tom&phone1=123&name2=bob&phone2=456.... and so on.

Why you ask? Because I have a block of input fields like:
Name: |________________|
Phone: |________________|

If you click an "add" button, it adds a second block on input fields
like so:
Name: |________________|
Phone: |________________|

Name: |________________|
Phone: |________________|
....

If you enter data into the form, then click the add button, the
Javascript needs to save the existing data into an array, so that when
the form is redrawn, it populates the fileds you've already entered data
into.

The way I figured out to do this was to increment the variable names by
1 so that the right data goes into the right fields.
 
G

Gunnar Hjalmarsson

Have you read the posting guidelines yet? Not complying with them will
make people here ignore you.
You are correct, I am not dealing with references. The code should
generate this:
@values=($name0, $phone0, $name1, $phone1,... $namen, $phonen)

$name0 get's set by the below function that reads all incoming CGI
variables and turns them into perl variables:
sub getCGIVariables {
my $qname = '';
my $qvalue = '';
while(($qname, $qvalue) = each %in) {
$$qname = $qvalue;
}
} # end

I've tried using "print eval $values[0];", but that returns "$name0".

Then it seems as if you need to do some debugging.

I must say that creating all those scalar variables makes little sense
to me. You really should take a step back, read some _good_ book about
basic Perl programming, and reconsider the approach in your program.
 
J

Jürgen Exner

[Please do not top post]
[Please do not blindly fullquote]

"Geoff" wrote :
You are correct, I am not dealing with references.

Unfortunately you are, with symbolic references :-((
The code should generate this: [...]
$$qname = $qvalue;
[...]

Are you sure you know what you are doing and why you are using them?
And what limitations and problems they have?

I _strongly_ recommend you read the FAQ 'perldoc -q "variable name"'

jue
 
U

Uri Guttman

don't top post. see the posting guidelines (posted regularly here)

G> OK. Well, the string that comes from the HTML looks like so:
G> action=add&name1=tom&phone1=123&name2=bob&phone2=456.... and so on.

G> Why you ask? Because I have a block of input fields like:
G> Name: |________________|
G> Phone: |________________|

G> If you enter data into the form, then click the add button, the
G> Javascript needs to save the existing data into an array, so that when
G> the form is redrawn, it populates the fileds you've already entered
G> data into.

blech. that can be done server side with little effort. it is built in
to cgi.pm (sticky values i think it is called).

G> The way I figured out to do this was to increment the variable names
G> by 1 so that the right data goes into the right fields.

those aren't variable but form fields. and my comment was on the sql
part more than the html stuff. if you have multiple common fields, then
using incremental names in sql makes little sense as you can't
(easily) add columns as needed. your whole design needs a big rethink
before you get into deeper spaghetti trouble.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top