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

Hey

I have to find a word ending with % in an array , I think we can do it with grep . can we do it

Replies are listed 'Best First'.
Re: using grep to find a word
by hdb (Monsignor) on Jun 24, 2013 at 07:56 UTC

    Yes, you can do it with grep.

Re: using grep to find a word
by kcott (Archbishop) on Jun 24, 2013 at 08:06 UTC

    G'day shushant,

    Welcome to the monastery.

    "I have to find a word ending with % in an array , I think we can do it with grep . can we do it"

    Yes, you can do it with grep. However, depending on the size of the array and whether you care which word ("ending with %") you find, the first() function provided by List::Util might be a better choice.

    -- Ken

      <hey >

      I am also using grep , but having problem .

      i am trying to find words starting with "" (double quotes)in array and I am doing this

      @foo_d = grep(/""/, @arr);

      but it is not working , @foo_d always holds 1 if anyone can please help out

        @words_which_start_with_double_quotes = grep {/^"/} @array_of_words;

        You should open your own thread if the question is different (here you just have a common topic). Maybe it's a bit late far that this time, but for your next question maybe :).

        So, if you want a string that starts with two double quotes, that would be with /^""/. And then, if you have a number as a result instead of strings, you probably called grep in a scalar context (so you get the number of matches instead of the matches). Maybe you wrote $foo_d instead of @foo_d, or you forgot a ; in the previous line. Or maybe you called @foo_d in a scalar context when printing it, you'd still get a number of elements then instead of content.

        @foo_d = grep(/""/, @arr);

        Not working because you are looking for two double quotes instead of one double quote. If you want a double quote at the start of your string:

        @foo_d = grep(/^"/, @arr);

        You really need to show your input and output as well as the process you're using to get from one to the other. Guidelines for doing this can be found in "How do I post a question effectively?".

        "but it is not working , @foo_d always holds 1 if anyone can please help out"

        How are you determining what "@foo_d always holds"? Here's a number of different ways that you might be doing this:

        $ perl -Mstrict -Mwarnings -le ' my @all_words = qw{abc "def ghi "jkl mno}; print "*** Using grep BLOCK LIST ***"; my @qq_words_block = grep { /^"/ } @all_words; print "qq_words_block = ", @qq_words_block; print "qq_words_block = " . @qq_words_block; print "qq_words_block = ", "@qq_words_block"; print "qq_words_block = " . "@qq_words_block"; print "*** Using grep EXPR, LIST ***"; my @qq_words_expr = grep(/^"/, @all_words); print "qq_words_expr = ", @qq_words_expr; print "qq_words_expr = " . @qq_words_expr; print "qq_words_expr = ", "@qq_words_expr"; print "qq_words_expr = " . "@qq_words_expr"; ' *** Using grep BLOCK LIST *** qq_words_block = "def"jkl qq_words_block = 2 qq_words_block = "def "jkl qq_words_block = "def "jkl *** Using grep EXPR, LIST *** qq_words_expr = "def"jkl qq_words_expr = 2 qq_words_expr = "def "jkl qq_words_expr = "def "jkl

        In list context, @array expands to its elements; in scalar context, you get the number of elements.

        Search for $LIST_SEPARATOR in perlvar for a discussion of why "@array" separates the elements with spaces.

        -- Ken

Re: using grep to find a word
by space_monk (Chaplain) on Jun 24, 2013 at 09:31 UTC
    Don't have a Perl install on here to check it, but this is close...
    #!/usr/bin/perl use strict; use warnings; my @words = qw( first second third% fourth% fifth); my @results = grep (/%$/, @words); print "Found:".join(",", @results)."\n" if (@results);
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: using grep to find a word
by Anonymous Monk on Jun 24, 2013 at 08:00 UTC