theleftright has asked for the wisdom of the Perl Monks concerning the following question:

could i get this any shorter and i don't mean by simply joining the lines together inside the curly brackets. I mean something like somehow wrapping it inside a map or so.. using the special variables? ..
@array = qw("this" "xxx" "is" not "xxx" and "xxx" "whatever"); #exampl +e array @m; $i=0; for(@array){ push(@m,$i)if $_=~/xxx/g; $i++; } print @m;
output should be 146

Replies are listed 'Best First'.
Re: shorter 1 liner way of doing this
by fishmonger (Chaplain) on Dec 27, 2015 at 21:03 UTC
    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;
      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.

Re: shorter 1 liner way of doing this
by Anonymous Monk on Dec 27, 2015 at 21:31 UTC
    grep { $array[$_] =~ /xxx/ } 0 .. $#array;
    why are you so obsessed with one liners
      smaller code and it's one of perls many strong points and for whatever reason people are obsessed with perl golf and quines and 1 liners and perl poems and ..ect..

        You have to program to play Perl golf, etc., but Perl golf, etc., is not programming. It doesn't really matter what you do, just don't do it in the machine room and frighten the hardware.

        Value clarity over concision.


        Give a man a fish:  <%-{-{-{-<

        Obsessed? Nay, sir. I despise Perl “poetry” and as Clemens observed golf is a good walk spoiled and quines are for Aussie professors!

Re: shorter 1 liner way of doing this
by Athanasius (Archbishop) on Dec 28, 2015 at 04:07 UTC

    Hello theleftright,

    Other monks have addressed your question. I just want to point out that the qw operator, which the Camel Book calls the “quote words” construct,1 creates a list of words and stringifies (quotes) each word for you. So this:

    use strict; use warnings; use Data::Dump; my @array = qw("this" "xxx" "is" not "xxx" and "xxx" "whatever"); dd \@array;

    produces this:

    13:57 >perl 1497_SoPW.pl [ "\"this\"", "\"xxx\"", "\"is\"", "not", "\"xxx\"", "and", "\"xxx\"", "\"whatever\"", ] 13:58 >

    which shows that the quote characters in your original list have been retained as part of the data. Probably not what you intended?

    This makes no difference to the outcome of the code in this particular example, but is something you should be aware of in your general programming.

    1Programming Perl, 4th Edition, p. 72.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: shorter 1 liner way of doing this
by Discipulus (Canon) on Dec 27, 2015 at 23:08 UTC
    map and grep are high order function able to make one line of Perl very, very expressive. The positive part of your question lead me to address you to practice theese function. Also the book High Order Perl can be interesting for you, if you can afford it, given your actual Perl skill (mine is stil to low to end the book..)
    That said, probably compiler is happier with plain code
    Nothing new after Anonym's answer but 'say list of indexes that match xxx' is:
    perl -E "say grep {$ARGV[$_]=~/xxx/} 0..$#ARGV" aaa xxx bb cc xxx dd x +xx eee 146


    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      ... the book High Order Perl can be interesting for you, if you can afford it ...

      Very interesting indeed, and quite affordable in electronic form: free HOP download.


      Give a man a fish:  <%-{-{-{-<