Home > Archive > PERL CGI Beginners > September 2004 > Re: How to enumerate array elements?
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 |
Re: How to enumerate array elements?
|
|
| Gunnar Hjalmarsson 2004-09-04, 8:55 am |
| Siegfried Heintze wrote:
> Consider the following program:
>
> my $me;
> $me->{verd_result}->[0][2]=5;
> $me->{verd_result}->[0][5]=34;
> print "len = ", $#{@{$me->{verd_result}->[0]}};
> for (@{$me->{verd_result}->[0]}){
> print "\n v="; print;
> }
How is that related to CGI?
> It produces this output:
>
> len = 5
> v=
> v=
> v=5
> v=
> v=
> v=34
No, it doesn't. It produces a fatal error.
- Copy and paste code that you post, do not retype it!
- Enable warnings in your program!
> How do I modify the program so it only produces this output:
>
> len = 5
> v[2]=5
> v[5]=34
That output wouldn't be correct, since the array contains six
elements, not five.
This code:
my $ref = $me->{verd_result}->[0];
print "len = " . @$ref, "\n";
for ( 0 .. $#$ref ) {
defined $ref->[$_] and print " v[$_]=$ref->[$_]\n";
}
outputs:
len = 6
v[2]=5
v[5]=34
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Charles K. Clarkson 2004-09-04, 8:55 am |
| Gunnar Hjalmarsson <mailto:noreply@gunnar.cc> wrote:
: Siegfried Heintze wrote:
: : It produces this output:
: :
: : len = 5
: : v=
: : v=
: : v=5
: : v=
: : v=
: : v=34
:
: No, it doesn't. It produces a fatal error.
I got four warnings:
Use of uninitialized value in print at aa.pl line 20.
Use of uninitialized value in print at aa.pl line 20.
Use of uninitialized value in print at aa.pl line 20.
Use of uninitialized value in print at aa.pl line 20.
len = 5
v=
v=
v=5
v=
v=
v=34
: - Copy and paste code that you post, do not retype it!
:
: - Enable warnings in your program!
:
: : How do I modify the program so it only produces this
: : output:
: :
: : len = 5
: : v[2]=5
: : v[5]=34
:
: That output wouldn't be correct, since the array contains
: six elements, not five.
:
: This code:
:
: my $ref = $me->{verd_result}->[0];
: print "len = " . @$ref, "\n";
: for ( 0 .. $#$ref ) {
: defined $ref->[$_] and print " v[$_]=$ref->[$_]\n";
: }
:
: outputs:
: len = 6
: v[2]=5
: v[5]=34
You are not answering the question:
print
q~len = 5
v[2]=5
v[5]=34~;
Sorry, I know its not helpful. I just couldn't
resist. :)
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Gunnar Hjalmarsson 2004-09-04, 8:55 am |
| Charles K. Clarkson wrote:
> Gunnar Hjalmarsson wrote:
>
> I got four warnings:
>
> Use of uninitialized value in print at aa.pl line 20.
> Use of uninitialized value in print at aa.pl line 20.
> Use of uninitialized value in print at aa.pl line 20.
> Use of uninitialized value in print at aa.pl line 20.
> len = 5
> v=
> v=
> v=5
> v=
> v=
> v=34
Hmm.. I checked again, and found the explanation: I'm running this
kind of code snippets via CGI, having tainted mode enabled, and then
the above code gives me:
Bizarre copy of ARRAY in leave at ...
When removing the -T switch, I get the same as you.
The error message refers to the line:
print "len = ", $#{@{$me->{verd_result}->[0]}};
and that line *is* bizarre. To get the last index in the array you
should reasonably just do:
$#{ $me->{verd_result}->[0] }
Actually, it's kind of strange that Perl does not object when the -T
switch is not enabled. The explanation according to "perldoc perldiag"
is that "Perl detected an attempt to copy an internal value that is
not copyable", and it classifies it as an internal error you should
never see...
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|