why does this not work: foreach (my $word) (@word_array){...}

Discussion in 'Perl Misc' started by TechCrazy, Jun 24, 2005.

  1. TechCrazy

    TechCrazy Guest

    Why do I have to move the declaration of $word outside like this:

    my $word;
    foreach $word (@word_array){
    ...
    }

    If I use inline declaration (to limit scope)

    foreach (my $word) (@word_array){
    ...
    }

    the compiler gets confused.
    TechCrazy, Jun 24, 2005
    #1
    1. Advertising

  2. "TechCrazy" <> wrote in
    news::

    > Why do I have to move the declaration of $word outside like this:
    >
    > my $word;
    > foreach $word (@word_array){
    > ...
    > }
    >
    > If I use inline declaration (to limit scope)
    >
    > foreach (my $word) (@word_array){
    > ...
    > }
    >
    > the compiler gets confused.


    Well, which list do you want to iterate over?

    foreach my $word (@word_array) {
    ...
    }

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jun 24, 2005
    #2
    1. Advertising

  3. TechCrazy

    TechCrazy Guest

    @word_array
    TechCrazy, Jun 24, 2005
    #3
  4. TechCrazy

    LEH Guest

    "TechCrazy" <> wrote in message
    news:...
    >
    > foreach (my $word) (@word_array){
    > ...
    > }
    >
    > the compiler gets confused.


    foreach my $word (@word_array){
    ....
    }
    LEH, Jun 25, 2005
    #4
  5. TechCrazy

    TechCrazy Guest

    Thanks for the helpful exposition, Tim. I didn't think the solution
    would be so simple.

    It would have been nice if scope of the iterator ($word) could be
    limited to the foreach loop.
    TechCrazy, Jun 25, 2005
    #5
  6. TechCrazy

    John Bokma Guest

    "TechCrazy" <> wrote:

    > Thanks for the helpful exposition, Tim. I didn't think the solution
    > would be so simple.
    >
    > It would have been nice if scope of the iterator ($word) could be
    > limited to the foreach loop.


    Read better:

    for my $word ( @words ) {

    }

    does exactly this

    my $word;
    for $word ( @words ) {

    }

    doesn't.

    In the former you can't use $word (it's gone) [1], in the latter it's
    still there.

    [1] the one used in the for that is.

    The last form has it uses if you want to go over the list *and* keep
    $word, e.g.

    my $word;
    for $word ( @words ) {

    last if exists $foo{ $word };
    }

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 25, 2005
    #6
  7. TechCrazy wrote:
    > @word_array


    Yes, what is the matter with @word_array?
    Perl variable types and syntax is explained in "perldoc perldata".

    If you would provide more text then just one single word then maybe we would
    know what your question is.

    jue
    Jürgen Exner, Jun 25, 2005
    #7
  8. TechCrazy

    Guest

    John Bokma wrote:

    > my $word;
    > for $word ( @words ) {
    >
    > }
    >
    > doesn't.
    >
    > In the former you can't use $word (it's gone) [1], in the latter it's
    > still there.
    >
    > [1] the one used in the for that is.


    Actually this is not true.

    The $word inside the loop is still a different lexical variable scoped
    to the block.

    In effect if you say...

    my $word;
    for $word ( @words ) {

    }

    ....then Perl behaves as if you had said...

    my $word;
    for my $word ( @words ) {

    }

    As I have said on a number of occasions in the past I think Perl should
    emit a warning when it does this.
    , Jun 25, 2005
    #8
  9. TechCrazy <> wrote:

    > It would have been nice if scope of the iterator ($word) could be
    > limited to the foreach loop.



    The scope of the iterator *is* limited to the foreach loop in
    the code given to answer your question.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 25, 2005
    #9
  10. TechCrazy

    Guest

    Tim Hammerquist <> wrote:
    > Tim Hammerquist <> wrote:
    > > TechCrazy <> wrote:
    > > > Why do I have to move the declaration of $word outside like this:
    > > >
    > > > my $word;
    > > > foreach $word (@word_array){
    > > > ...
    > > > }
    > > >

    > [ snippage ]
    > >
    > > What you want is:
    > >
    > > foreach my $word (@word_array) {
    > > ...
    > > }

    >
    > NB: there are subtle scoping differences between the OP's example at top
    > and mine. In the top, $word will still be in scope after the foreach
    > loop exits.


    Technically, perhaps, but foreach implicitly localizes (or something
    like that) the iterator. So for most intents and purposes, the iterator
    is a different variable than the one that was declared before the loop.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 25, 2005
    #10
  11. TechCrazy

    Guest

    John Bokma <> wrote:

    >
    > The last form has it uses if you want to go over the list *and* keep
    > $word, e.g.
    >
    > my $word;
    > for $word ( @words ) {
    >
    > last if exists $foo{ $word };
    > }


    Alas, this doesn't keep the value in $word after "last"ing, so it doesn't
    do anything very useful.

    $ perl -le 'my $x=3; foreach $x (1..10){last if $x==5}; print $x'
    3


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 25, 2005
    #11
  12. TechCrazy

    John Bokma Guest

    wrote:

    > John Bokma wrote:


    > The $word inside the loop is still a different lexical variable scoped
    > to the block.


    You are right, and thanks for teaching me something :-D

    > As I have said on a number of occasions in the past I think Perl should
    > emit a warning when it does this.


    I agree. Something like $word is masking early declaration of $word in for
    loop.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 29, 2005
    #12
  13. TechCrazy

    John Bokma Guest

    wrote:

    > John Bokma <> wrote:
    >
    >> my $word;
    >> for $word ( @words ) {
    >>
    >> last if exists $foo{ $word };
    >> }

    >
    > Alas, this doesn't keep the value in $word after "last"ing, so it doesn't
    > do anything very useful.


    So I understand, thanks to nobull and you for pointing this out.


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 29, 2005
    #13
    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. Quick Function

    jstl does not work with foreach

    Quick Function, Jul 14, 2004, in forum: Java
    Replies:
    4
    Views:
    770
    Quick Function
    Jul 14, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    438
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,661
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    1
    Views:
    73
    Oliver Bleckmann
    May 7, 2006
  5. Replies:
    3
    Views:
    67
    Dr.Ruud
    Nov 13, 2007
Loading...

Share This Page