$$x does not work.

J

JDS

The following is not working for me. What I want to do is this- my
cgiparms have a variable Test_1 or Test_2 .... generated depending on
what is chosen on a form. I want to check the value of
$cgiparm::Test_1 or $cgiparm::Test_2 .... to see if it matches a known
value. To do this I tried a simple string substitution, but got errors
during compiling. Any input would be greatly appreciated.

Thanks,
Jai.

1. Did not work:
-----------
#!c:\perl\bin\perl.exe -w

use strict;
my $index=18;
my $Test_18="Another Test";

my $x="Test_$index";
print "$$x\n" # Does not work, I expected it to print "Another Test"
------------
Output:
Can't use string ("Test_18") as a SCALAR ref while "strict refs" in
use at test1
..pl line 8.

2. The following kind of works.
------------
#!c:\perl\bin\perl.exe -w

#use strict;
$index=18;
$Test_18="Another Test";

$x="Test_$index";
print "$$x\n" # kind of works.
------------
Output:
#!c:\perl\bin\perl.exe -w

#use strict;
$index=18;
$Test_18="Another Test";

$x="Test_$index";
print "$$x\n" # Does not work
 
G

Gunnar Hjalmarsson

JDS said:
The following is not working for me. What I want to do is this- my
cgiparms have a variable Test_1 or Test_2 .... generated depending
on what is chosen on a form. I want to check the value of
$cgiparm::Test_1 or $cgiparm::Test_2 .... to see if it matches a
known value.

Use a hash for that.

my %tests = (
Test_1 => 'First Test',
Test_2 => 'Next Test',
Test_18 => 'Another Test',
);
my $formvar = 'Test_18';
print "$tests{$formvar}\n";
To do this I tried a simple string substitution, but got errors
during compiling.

use strict;
my $index=18;
my $Test_18="Another Test";

my $x="Test_$index";
print "$$x\n" # Does not work, I expected it to print "Another Test"

You are attempting to use a symbolic reference, which is unnecessary.
If you are interested, read about symbolic references in "perldoc
perlre", but don't use them (at least not for this task).
 
J

JDS

Well, that was a simplistic statement of the problem. The variables
Test_1 .... are generated at run time and there can theoretically be
lots of them- these names are generated for different elements of a
form to distinguish them. When a form is submitted, I know the base
string "Test" and I know what extension it has "_xx". So I know that
the value that I am interested in is in variable Test_xx. I now want
to access that value.

The scheme that you suggest requires pre-definitions of the values,
and it still would require me to figure out which variable is defined
when the form was submitted. e.g., potentially Test_1, ..., Test_100
can be defined when a form is submitted. In one case only Test_99
might be defined- I will still have to somehow figure out which one-
and might have to do it a number of times.

I looked at the symbolic references and some messages on that it works
only for global variable. Moreover, it seems it is not recommended for
use. Is there any other way to do this.

Thanks,
Jai.
 
J

Jürgen Exner

JDS said:
Well, that was a simplistic statement of the problem. The variables
Test_1 .... are generated at run time and there can theoretically be
lots of them- these names are generated for different elements of a
form to distinguish them. When a form is submitted, I know the base
string "Test" and I know what extension it has "_xx". [...]
I looked at the symbolic references and some messages on that it works
only for global variable. Moreover, it seems it is not recommended for
use. Is there any other way to do this.

Certainly there is:
use CGI;

jue
 
G

Gunnar Hjalmarsson

JDS said:
Well, that was a simplistic statement of the problem. The variables
Test_1 .... are generated at run time and there can theoretically
be lots of them-

And that's one good reason for not creating a lot of scalar variables.
The scheme that you suggest requires pre-definitions of the values,

I didn't know exactly what you want to do with the data (still don't),
but my main point is that a hash is probably sufficient for storing
them, and there is no need for symrefs. Example:

use CGI 'param';
my %tests;
$tests{$_} = param($_) for param;
 
J

JDS

Well, I am using CGI. The parameters are in CGI. I figured that the
parameter I need should be in CGI so I should be able to get it from
there. In the webserver, I get the following message when I try to
print the key and the value:

(key=pattern_18, val=*cgiparm::pattern_18)

What I need is the value that is held in there- how can I get to it.
Seems like I am getting a reference to the value.

Thanks in advance,

Jai.

I tried to isolate the problem in a program as follows:

-----
#!c:\perl\bin\perl.exe -w
use CGI;
use strict;

my $cgiobj=new CGI;
$cgiobj->import_names( "cgiparm" );
$cgiobj->delete_all;
$cgiobj->param(pattern_18 => "Web Developer");
my $btn_suffix=18;
&debug($cgiobj,$btn_suffix);

# -----------------
sub debug {
my ($cgiobj, $btn_suffix) = @_;

my $pattern = ($btn_suffix eq "") ? "pattern" :
"pattern_$btn_suffix";
my %hash = %cgiparm::;

print " (key=$pattern, val=$hash{$pattern})\n";

while (my($parm,$parm_value)=each %cgiparm::) {
print "($parm,$parm_value)\n";
}

}
--------------------
This however gave me the following output in a DOS window, webserver
did not run this because of the uninitialized stuff. However,
--
Use of uninitialized value in concatenation (.) or string at
C:\sviews\jds_dexce
nter_v5.0_integration\dexcenter\uidb\cgi-bin\win\test3.pl line 19.
(key=pattern_18, val=)
--
 
J

JDS

Gunnar Hjalmarsson said:
And that's one good reason for not creating a lot of scalar variables.


I didn't know exactly what you want to do with the data (still don't),
but my main point is that a hash is probably sufficient for storing
them, and there is no need for symrefs. Example:

use CGI 'param';
my %tests;
$tests{$_} = param($_) for param;

Ok. I am trying to handle buttons and text for forms and subforms.
Here is what I am trying to do (maybe there is a better way to do all
this):

I have a function that prints some buttons and some text areas and
some options. On the form the name of each element helps distinguish
them. When a button is pressed, I can handle those events.

However this function can be called re-cursively to print subforms.
When I do this, I need a way to distinguish the buttons on the subform
from the main form. I do this by adding a suffix to the name when it
is printed as a subform.

So, it goes like follows:
--------------------------------------------------------------------------
MAIN FORM:

Button: name=Button value=Test_Button
Text: name=Text_Text value=

SUBFORM1:
Button: name=Button value=Test_Button_1
Text: name=Text_Button_1 value=

SUBFORM2:
Button: name=Button value=Test_Button_2
Text: name=Text_Button_2 value=
--------------------------------------------------------------------------

Now if the user presses Test_Button_2, I get in the main event loop.
Here I look if name=Button and value=Test_Button_2, then I know that
the button on subform 2 was clicked. Now I want to get the value in
the Text area for subform 2. So I look at the value Test_Button_2 and
split it by '-' and get the index 2.

Now I know that the text value will be in the variable Text_Button_2.
So I create a string "Text_Button_2" and then try to get the value
stored in it by using the reference. Another approach is to look at
the CGI parameter hash table and get access to the value for
Text_Button_2. But so far have been unsuccessful.

Jai.
 
J

Joe Smith

JDS said:
Ok. I am trying to handle buttons and text for forms and subforms.

Put all the data into a hash as Gunnar suggested, then use pattern matching
on the keys of that hash.

$tests{$_} = param($_) for param; # Copy info to %tests
foreach my $button_name (keys %tests) {
if (my($name,$level) = $button_name =~ /^(\w+)_(\d+)$/) {
process_sub_form($name,$level,$test{$button_name});
} else {
process_main_form($button_name,$test{$button_name});
}
}

sub process_sub_form {
my($name,$level,$value) = @_;
print "Subform #$level was selected; $name='$value'\n";
}

If Text_Button_2 has data, the above will print
Subform #2 was selected; Text_Button='whatever was type in'

Or you can use

if (defined $tests{Button}) { # First check if it was pressed
my $button = $tests{Button}; # Can be "Test_Button_2"
my $text = $test{$button};
print "You selected $button; the text is '$text'\n";
}

-Joe
 
J

JDS

Thanks to all for your input- I was able to figure out things, solve
the problem... and ^.^...^......^ move ahead. :)

Jaidev.
 

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

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top