in reply to Re^3: check if an element exists in array
in thread check if an element exists in array

Thank you all, I seem to be close...
But if I go with grep as you suggest, I DO find the names, but how do I print the numbers as well? I tried with $_->1 but didn't work...
  • Comment on Re^4: check if an element exists in array

Replies are listed 'Best First'.
Re^5: check if an element exists in array
by FunkyMonk (Bishop) on Apr 19, 2008 at 09:58 UTC
    Assuming you mean $_->[1] (wrapping code in <code>...</code> tags preserves things like []).

    It works for me! Perhaps you should show exactly what you tried and how exactly it "didn't work".


    Unless I state otherwise, my code all runs with strict and warnings
      I am using your piece of code:
      if ( grep { $_->[0] eq $name } @names ) { }
      It matches 'Jim' for example, but how do I print Jim's number?
       print $_->[1]; ?
        I think you haven't thought about this enough. Jim doesn't have a number, he has two. The print above will work in the for loop example I gave in my first reply:
        for ( grep { $_->[0] eq $name } @names ) { print $_->[1]; }


        Unless I state otherwise, my code all runs with strict and warnings