Why can"t I access an Array like this?
Why can"t I access an Array like this?
am 26.08.2007 14:16:32 von Bill H
This has always vexed me. When trying to access an array in the
following manner it never works and I end up assigning a variable. Can
someone point me to some docs on what I am doing wrong?
Example to show the issue, not my real code:
for($i = 0;$i < 4;$i++)
{
$temp = substr("0000".$i,-4);
$in{'VALUE$i'} = $i; # This never works
}
The $in{'VALUE$i'} never works, I have tried a number of different
ways:
$in{"VALUE$i"}
$in{"VALUE".$i}
ect
The only thing that seems to work is:
$a = "VALUE$i";
$in{$a} = $i;
But this can be cumbersome when I have a lot of variables I am trying
to set. I am sure there is some simple perl way of doing this, I just
have never found it yet.
Bill H
Re: Why can"t I access an Array like this?
am 26.08.2007 14:33:39 von jurgenex
Bill H wrote:
> This has always vexed me. When trying to access an array in the
> following manner it never works and I end up assigning a variable. Can
> someone point me to some docs on what I am doing wrong?
>
> Example to show the issue, not my real code:
>
> for($i = 0;$i < 4;$i++)
for my $i (0..3)
> {
> $temp = substr("0000".$i,-4);
> $in{'VALUE$i'} = $i; # This never works
There is no array here. Did you mean
$in['VALUE$i']
instead? 'VALUE$i' is the literal string without $i expanded. The numerical
value of that string would be 0. So that statement would be the same as
$in[0].
regardless of the value of $i.
jue
Re: Why can"t I access an Array like this?
am 26.08.2007 16:23:38 von Gunnar Hjalmarsson
Bill H wrote:
>
> for($i = 0;$i < 4;$i++)
> {
> $temp = substr("0000".$i,-4);
> $in{'VALUE$i'} = $i; # This never works
> }
>
> The $in{'VALUE$i'} never works,
Sure it does. It assigns to the hash key 'VALUE$i' - not expanded -
every time.
> I have tried a number of different ways:
>
> $in{"VALUE$i"}
> $in{"VALUE".$i}
Please post a short but complete program to show us what problem you
have with those.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Why can"t I access an Array like this?
am 26.08.2007 17:39:07 von Bill H
On Aug 26, 10:23 am, Gunnar Hjalmarsson wrote:
> Bill H wrote:
>
> > for($i = 0;$i < 4;$i++)
> > {
> > $temp = substr("0000".$i,-4);
> > $in{'VALUE$i'} = $i; # This never works
> > }
>
> > The $in{'VALUE$i'} never works,
>
> Sure it does. It assigns to the hash key 'VALUE$i' - not expanded -
> every time.
>
> > I have tried a number of different ways:
>
> > $in{"VALUE$i"}
> > $in{"VALUE".$i}
>
> Please post a short but complete program to show us what problem you
> have with those.
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl
Here is a simple working example of what I am trying to do:
$in{'TEST0001'} = "Hello 0001";
$in{'TEST0002'} = "Hello 0002";
$in{'TEST0003'} = "Hello 0003";
$in{'TEST0004'} = "Hello 0004";
for($i = 1;$i < 5;$i++)
{
$a = substr("0000".$i,-4);
print "TEST$a = $in{'TEST$a'}\n";
}
for($i = 1;$i < 5;$i++)
{
$a = substr("0000".$i,-4);
$b = "TEST$a";
print "$b = $in{$b}\n";
}
The 1st for loop doesnt work as expected, but the second does.
Here is the output:
TEST0001 =
TEST0002 =
TEST0003 =
TEST0004 =
TEST0001 = Hello 0001
TEST0002 = Hello 0002
TEST0003 = Hello 0003
TEST0004 = Hello 0004
I am not sure why I need to go to the extra step of assigning a
variable (in this case $b) to access the data.
Bill H
Re: Why can"t I access an Array like this?
am 26.08.2007 18:09:33 von xhoster
Bill H wrote:
> On Aug 26, 10:23 am, Gunnar Hjalmarsson wrote:
> >
> > Please post a short but complete program to show us what problem you
> > have with those.
> >
>
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> print "TEST$a = $in{'TEST$a'}\n";
> }
You are using non-interpolating quotes for the hash key, so not
surprisingly they don't interpolate. You need to use interpolating
quotes, and need ones that don't interfere with the outer quotes,
such as this:
print "TEST$a = $in{qq'TEST$a'}\n";
But once we are using qq construct, I'd prefer to change the character
that goes with it:
print "TEST$a = $in{qq{TEST$a}}\n";
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: Why can"t I access an Array like this?
am 26.08.2007 18:17:11 von Ron Bergin
On Aug 26, 8:39 am, Bill H wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> print "TEST$a = $in{'TEST$a'}\n";
>
> }
Due to the single quotes, you won't get the needed variable
interpolation.
>
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> $b = "TEST$a";
> print "$b = $in{$b}\n";
>
> }
>
> The 1st for loop doesnt work as expected, but the second does.
>
> Here is the output:
>
> TEST0001 =
> TEST0002 =
> TEST0003 =
> TEST0004 =
> TEST0001 = Hello 0001
> TEST0002 = Hello 0002
> TEST0003 = Hello 0003
> TEST0004 = Hello 0004
>
> I am not sure why I need to go to the extra step of assigning a
> variable (in this case $b) to access the data.
>
You wouldn't need to if you used proper quoting.
for($i = 1;$i < 5;$i++)
{
$a = substr("0000".$i,-4);
print qq(TEST$a = $in{"TEST$a"}\n);
}
My preference would be to write it like this:
for my $i (1..4) {
my $key = sprintf("TEST%0.4d", $i);
print "$key = $in{$key}\n";
}
Re: Why can"t I access an Array like this?
am 26.08.2007 18:34:28 von Ron Bergin
On Aug 26, 8:39 am, Bill H wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
>
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> $b = "TEST$a";
> print "$b = $in{$b}\n";
>
> }
>
Since you're working with a hash, why not simply iterate over it with
"each"?
foreach my $key ( sort keys %in ) {
print "$key = $in{$key}\n";
}
Re: Why can"t I access an Array like this?
am 26.08.2007 18:45:34 von Bill H
On Aug 26, 12:09 pm, xhos...@gmail.com wrote:
> Bill H wrote:
> > On Aug 26, 10:23 am, Gunnar Hjalmarsson wrote:
>
> > > Please post a short but complete program to show us what problem you
> > > have with those.
>
> > Here is a simple working example of what I am trying to do:
>
> > $in{'TEST0001'} = "Hello 0001";
> > $in{'TEST0002'} = "Hello 0002";
> > $in{'TEST0003'} = "Hello 0003";
> > $in{'TEST0004'} = "Hello 0004";
>
> > for($i = 1;$i < 5;$i++)
> > {
> > $a = substr("0000".$i,-4);
> > print "TEST$a = $in{'TEST$a'}\n";
> > }
>
> You are using non-interpolating quotes for the hash key, so not
> surprisingly they don't interpolate. You need to use interpolating
> quotes, and need ones that don't interfere with the outer quotes,
> such as this:
>
> print "TEST$a = $in{qq'TEST$a'}\n";
>
> But once we are using qq construct, I'd prefer to change the character
> that goes with it:
>
> print "TEST$a = $in{qq{TEST$a}}\n";
>
> Xho
>
> --
> --------------------http://NewsReader.Com/------------------ --
> Usenet Newsgroup Service $9.95/Month 30GB- Hide quoted text -
>
> - Show quoted text -
Xho,
I knew there was some way of doing it Thanks for pointing out using
the qq construct!
Ron,
I use the keys hash alot, but for this usage I needed to do it
differently. Basically what the processor is doing is reading in form
values and all I know about the form values is that there are 10
fields that start with different words and end in ???? where ???? is
0001 - 9999. I also know how many groups of these values there are so
I have to do a for loop to read in the values for each group based on
the starting word and a counter, checking that some of the groups
aren't empty etc.
Bill H
Re: Why can"t I access an Array like this?
am 26.08.2007 18:59:02 von jurgenex
Bill H wrote:
> Here is a simple working example of what I am trying to do:
>
> $in{'TEST0001'} = "Hello 0001";
> $in{'TEST0002'} = "Hello 0002";
> $in{'TEST0003'} = "Hello 0003";
> $in{'TEST0004'} = "Hello 0004";
As I mentioned in an earlier reply you don't have arrays. Those are hashes.
> for($i = 1;$i < 5;$i++)
Better written as
for my $i (1..4)
> {
> $a = substr("0000".$i,-4);
> print "TEST$a = $in{'TEST$a'}\n";
I you had used strict and warnings this would have generated
Use of uninitialized value in concatenation (.) or string at ...
As others have pointed out using single quotes doesn't interpolate the
variable, so you were looking for the literal key 'TEST$a' which doesn't
exist, of course.
It appears that
print "TEST$a =" . $in{"TEST$a"} . "\n";
will do what you seem to be looking for.
But looping through a hash is much easier done with
for (keys %in) {
print "$in{$_}\n";
}
Also using consecutively numbered variables (or hash keys in this case)
usually is a strong indicator that you are using the wrong data structure. A
hash of array would probably be a better choice.
jue
Re: Why can"t I access an Array like this?
am 26.08.2007 19:18:41 von jurgenex
Bill H wrote:
> I use the keys hash alot, but for this usage I needed to do it
> differently. Basically what the processor is doing is reading in form
> values and all I know about the form values is that there are 10
> fields that start with different words and end in ???? where ???? is
> 0001 - 9999.
Are you talking about HTML/HTTP/Web forms by any chance? You are using the
CGI module, aren't you?
> I also know how many groups of these values there are so
> I have to do a for loop to read in the values for each group based on
> the starting word and a counter, checking that some of the groups
> aren't empty etc.
If yes, then @names = $query->param will return all the parameter names and
you can then use $value = $query->param('foo') to get
each individual value. No need to awkwardly read in your own copies.
jue
Re: Why can"t I access an Array like this?
am 26.08.2007 19:48:50 von Michele Dondi
On Sun, 26 Aug 2007 08:39:07 -0700, Bill H wrote:
>$in{'TEST0001'} = "Hello 0001";
>$in{'TEST0002'} = "Hello 0002";
>$in{'TEST0003'} = "Hello 0003";
>$in{'TEST0004'} = "Hello 0004";
>
>
>for($i = 1;$i < 5;$i++)
>{
> $a = substr("0000".$i,-4);
Why this substr() madness? What's wrong with sprintf()?
> print "TEST$a = $in{'TEST$a'}\n";
>}
>
>for($i = 1;$i < 5;$i++)
>{
> $a = substr("0000".$i,-4);
> $b = "TEST$a";
> print "$b = $in{$b}\n";
>}
How 'bout
$in{"$TEST$_"} = "$Hello $_" for '0001'..'0004'; # ?
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Why can"t I access an Array like this?
am 26.08.2007 19:51:24 von Michele Dondi
On Sun, 26 Aug 2007 09:34:28 -0700, Ron Bergin wrote:
>Since you're working with a hash, why not simply iterate over it with
>"each"?
>
>foreach my $key ( sort keys %in ) {
> print "$key = $in{$key}\n";
>}
Just a minor nitpick: you're *not* "iterating over it with C",
but iterating overt its *keys* with C.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Why can"t I access an Array like this?
am 26.08.2007 21:12:55 von Ron Bergin
On Aug 26, 10:51 am, Michele Dondi wrote:
> On Sun, 26 Aug 2007 09:34:28 -0700, Ron Bergin wrote:
> >Since you're working with a hash, why not simply iterate over it with
> >"each"?
>
> >foreach my $key ( sort keys %in ) {
> > print "$key = $in{$key}\n";
> >}
>
> Just a minor nitpick: you're *not* "iterating over it with C",
> but iterating overt its *keys* with C.
>
> Michele
> --
> {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
> (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
> .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
> 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Ya, I changed my thought pattern while I was writing. I guess I
should proof read before hitting the send key.
Re: Why can"t I access an Array like this?
am 26.08.2007 21:28:59 von Ron Bergin
On Aug 26, 9:45 am, Bill H wrote:
> > > Here is a simple working example of what I am trying to do:
>
> > > $in{'TEST0001'} = "Hello 0001";
> > > $in{'TEST0002'} = "Hello 0002";
> > > $in{'TEST0003'} = "Hello 0003";
> > > $in{'TEST0004'} = "Hello 0004";
>
> > > for($i = 1;$i < 5;$i++)
> > > {
> > > $a = substr("0000".$i,-4);
> > > print "TEST$a = $in{'TEST$a'}\n";
> > > }
>
> > --
> > --------------------http://NewsReader.Com/------------------ --
> > Usenet Newsgroup Service $9.95/Month 30GB- Hide quoted text -
>
> > - Show quoted text -
>
> Xho,
>
> I knew there was some way of doing it Thanks for pointing out using
> the qq construct!
>
> Ron,
>
> I use the keys hash alot, but for this usage I needed to do it
> differently. Basically what the processor is doing is reading in form
> values and all I know about the form values is that there are 10
> fields that start with different words and end in ???? where ???? is
> 0001 - 9999. I also know how many groups of these values there are so
> I have to do a for loop to read in the values for each group based on
> the starting word and a counter, checking that some of the groups
> aren't empty etc.
>
> Bill H
The substr approach isn't going to work as expected when you have
varying amounts of leading zeros. I can't say for sure without seeing
your actual data, but you'd either want to use the sprintf approach
like I've shown, or use a regex to capture the digits.
Something like this:
foreach my $key ( sort keys %in ) {
if ( $key =~ /\D(\d{4})$/ ) {
print "$key = $in{$key}\n";
}
}
Re: Why can"t I access an Array like this?
am 26.08.2007 22:04:33 von 1usa
Michele Dondi wrote in
news:t3f3d3145p67gs2a0r35ec3ap1pj1ud3m7@4ax.com:
> On Sun, 26 Aug 2007 08:39:07 -0700, Bill H wrote:
>
>>$in{'TEST0001'} = "Hello 0001";
>>$in{'TEST0002'} = "Hello 0002";
>>$in{'TEST0003'} = "Hello 0003";
>>$in{'TEST0004'} = "Hello 0004";
>>
>>
>>for($i = 1;$i < 5;$i++)
>>{
>> $a = substr("0000".$i,-4);
>
> Why this substr() madness? What's wrong with sprintf()?
Maybe he wants to be able to make it to the front page of the Daily WTF.
(I know, it is "Worse than Failure" now, but I prefer the former name).
>> print "TEST$a = $in{'TEST$a'}\n";
>>}
>>
>>for($i = 1;$i < 5;$i++)
>>{
>> $a = substr("0000".$i,-4);
>> $b = "TEST$a";
>> print "$b = $in{$b}\n";
>>}
>
> How 'bout
>
> $in{"$TEST$_"} = "$Hello $_" for '0001'..'0004'; # ?
There is a philosophical issue here. Assuming he is implementing a
sparse data structure using hashes (and that's why he is not using
arrays despite the fact that his keys are integers), a simple name
change would make all the interpolation completely and utterly
unnecessary as well as making *ALL* of his code much more readable:
my %test;
$test{ $_ } = "Hello $_" for '0001' .. '0004';
print "$test{0003}\n";
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:
Re: Why can"t I access an Array like this?
am 26.08.2007 23:12:48 von hjp-usenet2
On 2007-08-26 15:39, Bill H wrote:
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> print "TEST$a = $in{'TEST$a'}\n";
> }
>
> for($i = 1;$i < 5;$i++)
> {
> $a = substr("0000".$i,-4);
> $b = "TEST$a";
> print "$b = $in{$b}\n";
> }
>
>
> The 1st for loop doesnt work as expected, but the second does.
[...]
> I am not sure why I need to go to the extra step of assigning a
> variable (in this case $b) to access the data.
You don't. There is another difference between your two loops. Look more
closely.
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Re: Why can"t I access an Array like this?
am 27.08.2007 00:07:45 von Michele Dondi
On Sun, 26 Aug 2007 12:12:55 -0700, Ron Bergin wrote:
>On Aug 26, 10:51 am, Michele Dondi wrote:
[sniè]
>> Just a minor nitpick: you're *not* "iterating over it with C",
>> but iterating overt its *keys* with C.
^
^
>Ya, I changed my thought pattern while I was writing. I guess I
>should proof read before hitting the send key.
/me too. Except that I do. But I still fail! ;-)
(For what can I smile on a day like this...)
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Why can"t I access an Array like this?
am 27.08.2007 00:09:09 von Michele Dondi
On Sun, 26 Aug 2007 20:04:33 GMT, "A. Sinan Unur"
<1usa@llenroc.ude.invalid> wrote:
>Maybe he wants to be able to make it to the front page of the Daily WTF.
>
>(I know, it is "Worse than Failure" now, but I prefer the former name).
I didn't know it had changed.
>There is a philosophical issue here. Assuming he is implementing a
>sparse data structure using hashes (and that's why he is not using
>arrays despite the fact that his keys are integers), a simple name
>change would make all the interpolation completely and utterly
>unnecessary as well as making *ALL* of his code much more readable:
>
>my %test;
>$test{ $_ } = "Hello $_" for '0001' .. '0004';
>
>print "$test{0003}\n";
Indeed!
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Why can"t I access an Array like this?
am 27.08.2007 02:43:03 von Bill H
On Aug 26, 4:04 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> Michele Dondi wrote innews:t3f3d3145p67gs2a0r35ec3ap1pj1ud3m7@4ax.com:
>
> > On Sun, 26 Aug 2007 08:39:07 -0700, Bill H wrote:
>
> >>$in{'TEST0001'} = "Hello 0001";
> >>$in{'TEST0002'} = "Hello 0002";
> >>$in{'TEST0003'} = "Hello 0003";
> >>$in{'TEST0004'} = "Hello 0004";
>
> >>for($i = 1;$i < 5;$i++)
> >>{
> >> $a = substr("0000".$i,-4);
>
> > Why this substr() madness? What's wrong with sprintf()?
>
> Maybe he wants to be able to make it to the front page of the Daily WTF.
>
> (I know, it is "Worse than Failure" now, but I prefer the former name).
>
> >> print "TEST$a = $in{'TEST$a'}\n";
> >>}
>
> >>for($i = 1;$i < 5;$i++)
> >>{
> >> $a = substr("0000".$i,-4);
> >> $b = "TEST$a";
> >> print "$b = $in{$b}\n";
> >>}
>
> > How 'bout
>
> > $in{"$TEST$_"} = "$Hello $_" for '0001'..'0004'; # ?
>
> There is a philosophical issue here. Assuming he is implementing a
> sparse data structure using hashes (and that's why he is not using
> arrays despite the fact that his keys are integers), a simple name
> change would make all the interpolation completely and utterly
> unnecessary as well as making *ALL* of his code much more readable:
>
> my %test;
> $test{ $_ } = "Hello $_" for '0001' .. '0004';
>
> print "$test{0003}\n";
>
> Sinan
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
> clpmisc guidelines:
Sinan
It never occured to me to use sprintf, substr has always worked for
me, but now that you mention it I may just start using it.
As for the reason I wanted to use it the way I posted is that I have a
web form that starts out with
fields:
SNAME0001, SPASS0001, FRONT0001, BACK0001, TITLE0001, COPY0001,
TOC0001, FOR0001, ACK0001, CHAP0001 and the person using the form can
add / remove groups of fields (these 10 fields all ending with the
same numbers). I add and remove the fields dynamically in the perl
script to the web page, so I only know the starting letters of the
field (ie SNAME, SPASS, FRONT etc) and how many groups are on the
page. So I have to work my way through the groups of fields using a
for loop (hence the need for the 0001 - ????) to build the page.
Bill H
Re: Why can"t I access an Array like this?
am 27.08.2007 10:42:52 von Michele Dondi
On Sun, 26 Aug 2007 17:43:03 -0700, Bill H wrote:
>It never occured to me to use sprintf, substr has always worked for
>me, but now that you mention it I may just start using it.
It's just a matter of not reinventing a wheel. (Possibly risking to do
so in a wrong way.) Similarly you could do:
my @odd;
for (my $i=0; $i<=10; $i++) {
push @odd, $i if $i%2;
}
But you may more easily do
my @odd = grep $_%2, 0..10;
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Why can"t I access an Array like this?
am 28.08.2007 10:21:17 von Joe Smith
Bill H wrote:
> for($i = 0;$i < 4;$i++)
> {
> $temp = substr("0000".$i,-4);
> $in{'VALUE$i'} = $i; # This never works
> }
Why are you using substr?
for my $i (0 .. 3) {
my $temp = sprintf "VALUE%04d",$i;
my $foo = $in{$temp};
}
-Joe
Re: Why can"t I access an Array like this?
am 29.08.2007 03:28:11 von 1usa
Bill H wrote in
news:1188175383.166812.89070@19g2000hsx.googlegroups.com:
> SNAME0001, SPASS0001, FRONT0001, BACK0001, TITLE0001, COPY0001,
> TOC0001, FOR0001, ACK0001, CHAP0001 and the person using the form can
> add / remove groups of fields (these 10 fields all ending with the
> same numbers). I add and remove the fields dynamically in the perl
> script to the web page, so I only know the starting letters of the
> field (ie SNAME, SPASS, FRONT etc) and how many groups are on the
> page. So I have to work my way through the groups of fields using a
> for loop (hence the need for the 0001 - ????) to build the page.
No, there is no need for that. The following script is *very* quick and
dirty but it might be useful:
#!perl
use strict;
use warnings;
use CGI;
use Data::Dumper;
use HTML::Entities;
use HTML::Template;
my $tmpl = <
"http://www.w3.org/TR/html4/strict.dtd">
Field Adder Test
Test Form
EO_TMPL
my $cgi = CGI->new;
my $action = $cgi->param('action');
if ( $action eq 'Add Field' ) {
handle_add_field();
}
elsif ( $action eq 'Submit' ) {
handle_dump_form();
}
else {
handle_show_form();
}
sub handle_show_form {
my $template = HTML::Template->new( scalarref => \$tmpl );
$template->param( FIELDS => [ { MYFIELD_VALUE => q{} } ] );
print $cgi->header('text/html'), $template->output;
}
sub handle_dump_form {
print $cgi->header('text/plain'), Dumper( $cgi );
}
sub handle_add_field {
my @fields_loop;
push @fields_loop, map {
{ MYFIELD_VALUE => encode_entities( $_ ) }
} $cgi->param('myfield');
push @fields_loop, { MYFIELD_VALUE => q{} };
my $template = HTML::Template->new( scalarref => \$tmpl );
$template->param( FIELDS => \@fields_loop );
print $cgi->header('text/html'), $template->output;
}
__END__
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: