How to find the position of a string in a array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vibhakhushi
    New Member
    • Mar 2010
    • 33

    How to find the position of a string in a array?

    I have an array
    Code:
    @myarray = ("father","mother","sister","brother");
    
    How to find the position of "sister" in @myarray
    I didn't get any built in functions to do this. I'm a newbie in Perl. Please suggest some good tutorial.
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    To find the index of an element in an array, use List::MoreUtils

    Code:
    use strict;
    use warnings;
    use List::MoreUtils qw(first_index);
    
    my @myarray = ("father","mother","sister","brother");
    printf "item with index %i in list is sister", first_index { $_ eq 'sister' } @myarray;
    
    __END__
    
    item with index 2 in list is sister

    Comment

    • vibhakhushi
      New Member
      • Mar 2010
      • 33

      #3
      Thank you toolic. It worked like charm to me :)

      Comment

      Working...