Showing entries with tag "Array".

Found 2 entries

Perl: find the index of an array item

I needed to find the index of an item in an array so I wrote a simple Perl function.

sub array_id {
   my ($needle,@haystack) = @_;
   my $count = 0;

   foreach my $item (@haystack) {
      if ($item eq $needle) {
         return $count;
      }

      $count++;
   }

   return -1;
}
Leave A Reply - 1 Reply

Perl: doing a regexp replace on an array

I have an array of items that I want to do a quick regexp replace on each element. Here is a very elegant solution:

@names = ("John", "Paul", "george", "Ringo");
s/^g/G/g for @names;
print join(", ",@names);
Leave A Reply