CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Confused about accessing array elements from an input document using a variable

 



TJD
New User

Apr 1, 2004, 10:36 AM

Post #1 of 3 (642 views)
Confused about accessing array elements from an input document using a variable Can't Post

I've created an input form that allows a user to enter up to one hundred data items for a given field, although three to four would be the average amount. I'm able to retrieve the data using a specific numeric value for each element but not when I attempt to process the input as part of a loop using a variable. Is there something that I've missed, or is this impossible in Perl?

Here's the applicable code: Crazy


Code
  

# applicable section of input form :

#

for ($x=0;$x<$pr_cntgroups;$x++)

{

my $optcnt = $x+1;

print "<tr>\n";

print "<td width=135 align=right height=25><font size=2>Option $optcnt:</font></td>\n";

print "<td width=9 height=25></td>\n";

print "<td width=416 height=25><input type=text name=pr_optgroups[$x] value=\"$optgroups[$x]\" size=30></td>\n";

print "</tr>\n";

}

#

########################################

# part of the subroutine that edits input data

#

# doesn't work #

#

for ($x=0;$x<100;$x++)

{

$pr_optgroups[$x] = $INPT{'pr_optgroups[$x]'};

}

#

# works #

#

$pr_optgroups[0] = $INPT{'pr_optgroups[0]'};

$pr_optgroups[1] = $INPT{'pr_optgroups[1]'};

$pr_optgroups[2] = $INPT{'pr_optgroups[2]'};

$pr_optgroups[3] = $INPT{'pr_optgroups[3]'};

$pr_optgroups[4] = $INPT{'pr_optgroups[4]'};

$pr_optgroups[5] = $INPT{'pr_optgroups[5]'};

$pr_optgroups[6] = $INPT{'pr_optgroups[6]'};

$pr_optgroups[7] = $INPT{'pr_optgroups[7]'};

$pr_optgroups[8] = $INPT{'pr_optgroups[8]'};

$pr_optgroups[9] = $INPT{'pr_optgroups[9]'};



# etc., etc., up to value 99




davorg
Thaumaturge / Moderator

Apr 1, 2004, 11:18 AM

Post #2 of 3 (640 views)
Re: [TJD] Confused about accessing array elements from an input document using a variable [In reply to] Can't Post


In Reply To

Code
# doesn't work # 
#

for ($x=0;$x<100;$x++)
{
$pr_optgroups[$x] = $INPT{'pr_optgroups[$x]'};
}



Variables aren't expanded in single-quoted strings. You are looking for a hundred inputs that are all called 'pr_optgroups[$x]'.

Replace the single quotes with double quotes.

Code
$pr_optgroups[$x] = $INPT{"pr_optgroups[$x]"};


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


TJD
New User

Apr 1, 2004, 1:17 PM

Post #3 of 3 (637 views)
Re: [davorg] Confused about accessing array elements from an input document using a variable [In reply to] Can't Post

So the difference is in the interpolation of the input value. Unsure I spent hours searching through my Perl reference books for an answer and that never even occurred to me. I feel so 'blonde' right now. Blush

Many, many thanks, Dave. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives