Is $_ special somehow?

Discussion in 'Perl Misc' started by Steve, May 28, 2010.

  1. Steve

    Steve Guest

    I'm going through the O'reilly book and I learned about the next
    operator. So this works, as it should:

    -----------------------------------------
    my @words = qw#hello kayak prong#;
    for (@words)
    {
    next if /kayak/;
    print "$_\n";
    }
    -----------------------------------------

    However, if I write it as this, it doesn't work:
    -----------------------------------------
    my @words = qw#hello kayak prong#;
    for my $word (@words)
    {
    next if /kayak/;
    print "$word\n";
    }
    -----------------------------------------

    Why is this? I thought in this case, $_ and $word would contain the
    same thing?
     
    Steve, May 28, 2010
    #1
    1. Advertising

  2. Steve

    Uri Guttman Guest

    >>>>> "S" == Steve <> writes:

    S> I'm going through the O'reilly book and I learned about the next
    S> operator. So this works, as it should:

    there are many o'reilly books on perl. which one are you reading?

    S> -----------------------------------------
    S> my @words = qw#hello kayak prong#;

    # is a poor choice for a string delimiter. the best practice choices are
    usually paired chars like () or {}. some styles like // especially when
    passing args to a use command.

    S> for (@words)
    S> {
    S> next if /kayak/;
    S> print "$_\n";
    S> }
    S> -----------------------------------------

    S> However, if I write it as this, it doesn't work:
    S> -----------------------------------------
    S> my @words = qw#hello kayak prong#;
    S> for my $word (@words)
    S> {
    S> next if /kayak/;
    S> print "$word\n";
    S> }
    S> -----------------------------------------

    S> Why is this? I thought in this case, $_ and $word would contain the
    S> same thing?

    you don't say what is different or show output. this is a classic newbie
    issue saying something doesn't work but not saying what you expected vs
    what you see.

    now i can see the issue and it is very simple. for will only alias its
    list elements to $_ if you don't supply a loop variable. in the second
    case you do so /kayak/ will match against $_ which isn't set (or has
    some older value left over). this means you likely weren't using
    warnings which would have told you that $_ was undef (assuming that for
    the moment). that would have clued you in to noticing $_ was never being
    set in the second loop. you could have also tested that by printing $_
    in the second loop. that is a classic debugging technique. you think $_
    would be set or the /kayak/ would match against it but that wasn't the
    case. some simple prints of the right things would have told you
    this. always check your assumptions with debugging prints when you are
    not sure about something.

    so this lesson was more than on your bug but how to debug your own code.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 29, 2010
    #2
    1. Advertising

  3. Steve

    John Bokma Guest

    "Uri Guttman" <> writes:

    > you don't say what is different or show output. this is a classic newbie
    > issue saying something doesn't work but not saying what you expected vs
    > what you see.


    He did: he expect the second snippet to work like the first. It doesn't
    matter what the output is IMO, since it's clear what the mistake is in
    the second example.

    Please don't make beginners look stupid, thank you.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, May 29, 2010
    #3
  4. Steve

    Uri Guttman Guest

    >>>>> "JB" == John Bokma <> writes:

    JB> "Uri Guttman" <> writes:
    >> you don't say what is different or show output. this is a classic newbie
    >> issue saying something doesn't work but not saying what you expected vs
    >> what you see.


    JB> He did: he expect the second snippet to work like the first. It doesn't
    JB> matter what the output is IMO, since it's clear what the mistake is in
    JB> the second example.

    JB> Please don't make beginners look stupid, thank you.

    i beg to differ. claiming the two outputs aren't the same isn't the same
    as saying what is different about them. that info would always make it
    easier to find the bug. and it is a good lesson on how to describe
    bugs. the classic 'it doesn't work' is something you need to teach to
    avoid.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, May 29, 2010
    #4
  5. Steve

    Willem Guest

    John Bokma wrote:
    ) "Uri Guttman" <> writes:
    )
    )> you don't say what is different or show output. this is a classic newbie
    )> issue saying something doesn't work but not saying what you expected vs
    )> what you see.
    )
    ) He did: he expect the second snippet to work like the first. It doesn't
    ) matter what the output is IMO, since it's clear what the mistake is in
    ) the second example.

    It is irrelevant that the mistake is clear.

    The OP cannot know that it is clear, otherwise they would have known what
    the mistake is in the first place. Therefore, they should have posted
    all information that would be helpful to solving the problem, such as
    exactly what the expected and actual outputs are, etcetera.

    Being a beginner, they have just as much to learn about how to ask
    questions effectively as about programming itself.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, May 29, 2010
    #5
  6. Steve

    Guest

    On Sat, 29 May 2010 08:05:50 +0000 (UTC), Willem <> wrote:

    >John Bokma wrote:
    >) "Uri Guttman" <> writes:
    >)
    >)> you don't say what is different or show output. this is a classic newbie
    >)> issue saying something doesn't work but not saying what you expected vs
    >)> what you see.
    >)
    >) He did: he expect the second snippet to work like the first. It doesn't
    >) matter what the output is IMO, since it's clear what the mistake is in
    >) the second example.
    >
    >It is irrelevant that the mistake is clear.
    >
    >The OP cannot know that it is clear, otherwise they would have known what
    >the mistake is in the first place.


    He said he was learning about the next operator. How is he learning about
    that and doesen't know the for operator?
    Furthermore, how does he know that $_ would contain anything, or what it
    is in this context?

    [snip]
    >Being a beginner, they have just as much to learn about how to ask
    >questions effectively as about programming itself.


    But the questions is why did he put a variable between for and the list?
    (he is just learning about "next")
    And how does he know what this means:
    "next if /kayak/;"

    I think the question is fake.
    Usually beginners would start with something traditional and laborous:

    for (my $i = 0; $i < @words; $i++)
    {
    if ($words[$i] =~ /kayak/)
    {
    next;
    }
    # do something else
    }

    -sln
     
    , May 29, 2010
    #6
  7. Steve

    John Bokma Guest

    "Uri Guttman" <> writes:

    >>>>>> "JB" == John Bokma <> writes:

    >
    > JB> "Uri Guttman" <> writes:
    > >> you don't say what is different or show output. this is a classic newbie
    > >> issue saying something doesn't work but not saying what you expected vs
    > >> what you see.

    >
    > JB> He did: he expect the second snippet to work like the first. It doesn't
    > JB> matter what the output is IMO, since it's clear what the mistake is in
    > JB> the second example.
    >
    > JB> Please don't make beginners look stupid, thank you.
    >
    > i beg to differ. claiming the two outputs aren't the same isn't the same
    > as saying what is different about them. that info would always make it
    > easier to find the bug. and it is a good lesson on how to describe
    > bugs. the classic 'it doesn't work' is something you need to teach to
    > avoid.


    On the latter I agree wholeheartedly with you. But to me the OP posted
    sufficient information to provide a solution IMO. I think most people
    here saw the solution before even reading what was wrong with it :).

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, May 29, 2010
    #7
  8. Steve

    John Bokma Guest

    Willem <> writes:

    > John Bokma wrote:
    > ) "Uri Guttman" <> writes:
    > )
    > )> you don't say what is different or show output. this is a classic newbie
    > )> issue saying something doesn't work but not saying what you expected vs
    > )> what you see.
    > )
    > ) He did: he expect the second snippet to work like the first. It doesn't
    > ) matter what the output is IMO, since it's clear what the mistake is in
    > ) the second example.
    >
    > It is irrelevant that the mistake is clear.


    To quote Uri: "I beg to differ". ;-)

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, May 29, 2010
    #8
  9. Steve

    Willem Guest

    John Bokma wrote:
    ) "Uri Guttman" <> writes:
    )> i beg to differ. claiming the two outputs aren't the same isn't the same
    )> as saying what is different about them. that info would always make it
    )> easier to find the bug. and it is a good lesson on how to describe
    )> bugs. the classic 'it doesn't work' is something you need to teach to
    )> avoid.
    )
    ) On the latter I agree wholeheartedly with you. But to me the OP posted
    ) sufficient information to provide a solution IMO. I think most people
    ) here saw the solution before even reading what was wrong with it :).

    Your 'but' is irrelevant. You don't seem to get the point.

    For simple problems, a half-assed description is enough to spot the
    problem. However, somebody who posts simple problems is also learning
    *how to ask questions*. And giving half-assed descriptions is not a
    good way to ask questions. Therefore, you should look beyond the simple
    problem and see the opportunity to educate somebody on the efficiency
    of question-asking.

    I notice that crossthread, you posted 'i beg to differ' to my post where
    I made roughly the same point. You also snipped my argumentation, and
    gave no arguments of your own. That is not a good way to argue.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, May 29, 2010
    #9
  10. Steve

    John Bokma Guest

    Willem <> writes:

    > John Bokma wrote:
    > ) "Uri Guttman" <> writes:
    > )> i beg to differ. claiming the two outputs aren't the same isn't the same
    > )> as saying what is different about them. that info would always make it
    > )> easier to find the bug. and it is a good lesson on how to describe
    > )> bugs. the classic 'it doesn't work' is something you need to teach to
    > )> avoid.
    > )
    > ) On the latter I agree wholeheartedly with you. But to me the OP posted
    > ) sufficient information to provide a solution IMO. I think most people
    > ) here saw the solution before even reading what was wrong with it :).
    >
    > Your 'but' is irrelevant. You don't seem to get the point.


    I do get "the point", we just disagree.

    > gave no arguments of your own. That is not a good way to argue.


    Argueing only makes sense if at least one party is willing to learn,
    hence pointless here.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, May 29, 2010
    #10
  11. Steve

    Justin C Guest

    On 2010-05-28, Steve <> wrote:
    > I'm going through the O'reilly book and I learned about the next
    > operator. So this works, as it should:
    >
    > -----------------------------------------
    > my @words = qw#hello kayak prong#;
    > for (@words)
    > {
    > next if /kayak/;
    > print "$_\n";
    > }
    > -----------------------------------------
    >
    > However, if I write it as this, it doesn't work:
    > -----------------------------------------
    > my @words = qw#hello kayak prong#;
    > for my $word (@words)
    > {
    > next if /kayak/;
    > print "$word\n";
    > }
    > -----------------------------------------
    >
    > Why is this? I thought in this case, $_ and $word would contain the
    > same thing?


    next if ($word =~ /kayak/);

    see perldoc perlvar for details on $_

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Jun 3, 2010
    #11
    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. =?Utf-8?B?RFdZ?=
    Replies:
    1
    Views:
    356
    =?Utf-8?B?SmltIEhlYXZleQ==?=
    Feb 1, 2006
  2. Ike

    extend String (somehow!)

    Ike, Sep 14, 2004, in forum: Java
    Replies:
    12
    Views:
    4,022
  3. Can Burak Cilingir

    disabling the line buffer somehow

    Can Burak Cilingir, Mar 5, 2006, in forum: Python
    Replies:
    0
    Views:
    273
    Can Burak Cilingir
    Mar 5, 2006
  4. Arthur J. O'Dwyer

    Re: Pointers in C: Is ptr MOD val possible somehow in c?

    Arthur J. O'Dwyer, Feb 21, 2006, in forum: C Programming
    Replies:
    4
    Views:
    895
    Jordan Abel
    Feb 24, 2006
  5. Ben
    Replies:
    10
    Views:
    572
    Christopher Benson-Manica
    Oct 28, 2006
Loading...

Share This Page