Why is my index variable destroyed?

Discussion in 'Perl' started by Louis., Dec 18, 2004.

  1. Louis.

    Louis. Guest

    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.
     
    Louis., Dec 18, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Dec 18, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    907
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,987
    Smokey Grindel
    Dec 2, 2006
  3. Steven T. Hatton
    Replies:
    3
    Views:
    366
    Victor Bazarov
    Mar 7, 2006
  4. loudking

    Automatic variable is not destroyed?

    loudking, Nov 24, 2008, in forum: C Programming
    Replies:
    10
    Views:
    852
    Keith Thompson
    Nov 24, 2008
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    305
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page