Why is my index variable destroyed?

L

Louis.

Hi,

I am finding that outside the foreach loop my $i index does not exist
anymore, eventhough I defined it beforehand. This does not seem
logical. Is there a reason behind this?

Thank you in advance,
Louis.



my $i;

$url{1} = apple;
$url{2} = pear;
$url{3} = cherry;

$desc{1} = rose;
$desc{2} = tulip;
$desc{3} = sunflower;

foreach $i (keys %url) {
print "$i: $url{$i}\n";
if ( $url{$i} eq "pear" ) { $j = $i; print "found\n"; last; }
}

print "$i: $desc{$i}\n"; # nothing.
print "$j: $desc{$j}\n"; # works.
 
G

Gunnar Hjalmarsson

Louis. said:
I am finding that outside the foreach loop my $i index does not exist
anymore, eventhough I defined it beforehand.

It's explained in the docs. Read about foreach loops in "perldoc
perlsyn" (first para).

my $i;

$url{1} = apple;
$url{2} = pear;
$url{3} = cherry;

Please use strictures and warnings!

If you have further questions, post to comp.lang.perl.misc. This
newsgroup is defunct.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top