why doesn't /^$_$/ behave?

Discussion in 'Perl Misc' started by Sara, Nov 18, 2003.

  1. Sara

    Sara Guest

    OK, $_ is "1", and my aray contains "13", but not "1".

    This matches:

    grep /^$_$/, @a;

    But this doesn't match:

    my $xi = $_;
    grep /^$xi$/, @a;

    p 5.6.1 on RH.
     
    Sara, Nov 18, 2003
    #1
    1. Advertising

  2. [posted & mailed]

    On 18 Nov 2003, Sara wrote:

    >OK, $_ is "1", and my aray contains "13", but not "1".
    >
    >This matches:
    >
    > grep /^$_$/, @a;


    Because in grep() and map(), $_ is locally set to the value in the list
    you're looking at. That's why you write

    grep /foo/, @a;

    because /foo/ is really $_ =~ /foo/.

    $_ = 10;
    grep print("_ = $_\n"), 1, 2, 3;

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Nov 18, 2003
    #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. Joe Burnett
    Replies:
    0
    Views:
    2,050
    Joe Burnett
    Sep 7, 2003
  2. Stephan Aspridis

    Loop doesn't behave the way it's supposed to.

    Stephan Aspridis, Jan 3, 2004, in forum: C Programming
    Replies:
    19
    Views:
    528
    Stephan Aspridis
    Jan 11, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,075
    Smokey Grindel
    Dec 2, 2006
  4. Alexey Verkhovsky
    Replies:
    8
    Views:
    167
    Hal Fulton
    Jul 18, 2004
  5. Asfand Yar Qazi
    Replies:
    2
    Views:
    110
    Asfand Yar Qazi
    Jan 19, 2006
Loading...

Share This Page