Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Why is my index variable destroyed?

Reply
Thread Tools

Why is my index variable destroyed?

 
 
Louis.
Guest
Posts: n/a
 
      12-18-2004
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.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-18-2004
Louis. wrote:
> 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).

<snip>

> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why can't recognize index in hidden variable john woo Javascript 3 07-03-2005 06:00 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments