in reply to shorter 1 liner way of doing this

use warnings; use strict; use List::MoreUtils qw(indexes); my @array = qw("this" "xxx" "is" not "xxx" and "xxx" "whatever"); my @indexes = indexes { /xxx/ } @array; print @indexes;

Replies are listed 'Best First'.
Re^2: shorter 1 liner way of doing this
by theleftright (Novice) on Dec 27, 2015 at 21:27 UTC
    hmm I was thinking more of a perl way, without including external libs. Like for example:
    map($_=~/xxx/g ? push(@m,$i) : $i++) for(@array); print @m;
    clearly not working :D
      > clearly not working :D

      combining map and for gives two nested loops.

      maybe you meant this?

      DB<102> my $i=0; @m= map { $_=~/xxx/ ? $i++ : () } @a => (0, 1, 2)

      UPDATE
      DB<104> my $i=-1; @m= map { $i++; $_=~/xxx/ ? $i :() } @a => (1, 4, 6)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      Why are you against using a module? It's just as "perlish" as the grep version and it's shorter and more self documenting.