Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: using grep to find a word

by Anonymous Monk
on Jun 24, 2013 at 08:43 UTC ( [id://1040401]=note: print w/replies, xml ) Need Help??


in reply to Re: using grep to find a word
in thread using grep to find a word

<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

Replies are listed 'Best First'.
Re^3: using grep to find a word
by hdb (Monsignor) on Jun 24, 2013 at 08:51 UTC
    @words_which_start_with_double_quotes = grep {/^"/} @array_of_words;
Re^3: using grep to find a word
by Anonymous Monk on Jun 24, 2013 at 08:55 UTC
Re^3: using grep to find a word
by Eily (Monsignor) on Jun 24, 2013 at 12:22 UTC

    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.

Re^3: using grep to find a word
by Laurent_R (Canon) on Jun 24, 2013 at 11:55 UTC
    @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);
Re^3: using grep to find a word
by kcott (Archbishop) on Jun 24, 2013 at 23:39 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1040401]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found