Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Grep reutrn the occurance of a pattern

by Anonymous Monk
on Mar 13, 2008 at 13:41 UTC ( [id://673962]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

I know how to grep the index of an array for a pattern, but can I have a grep that can give me the index of the pattern, as well as the number of pattern occurance (in any form, not limited to array)?

(I think I see something on regex counting the number of occurance of a pattern, but I am not so clear for its syntax...may be somebody can give me some hint?)

Replies are listed 'Best First'.
Re: Grep reutrn the occurance of a pattern
by Fletch (Bishop) on Mar 13, 2008 at 13:54 UTC

    See How (Not) To Ask A Question, but (completely off the wall guess here) perhaps you want to grep over the indexen rather than the array itself?

    my @array = qw( foo bar baz quux ); my @idx_of_eles_with_an_a = grep $array[ $_ ] =~ /a/, 0..$#array;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Grep reutrn the occurance of a pattern
by moklevat (Priest) on Mar 13, 2008 at 13:56 UTC
    To get the number of matches you evaluate grep in scalar context. Here:

    #!/usr/bin/perl use strict; use warnings; my @items = qw/yes no no no yes yes no no/; my $number_of_matches = grep(/yes/,@items); print "$number_of_matches\n";

    Prints 3.

    Update: Noticed that I left out @items, and I see I am not alone in my confusion about exactly what it was the OP wanted.

Re: Grep reutrn the occurance of a pattern
by johngg (Canon) on Mar 13, 2008 at 13:59 UTC
    You can count the occurances and retain the captures like this. I'm not sure what you mean by "the index of an array for a pattern" though. Perhaps you could elaborate. This

    $ perl -le ' > $str = q{adasdGTsaJsdfsCFwee}; > $capsCt = @caps = $str =~ m{([A-Z]+)}; > print qq{Found: $capsCt}; > print for @caps;'

    produces

    Found: 3 GT J CF

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Grep reutrn the occurance of a pattern
by Narveson (Chaplain) on Mar 13, 2008 at 13:52 UTC
    may be somebody can give me some hint?

    Please give an example:

    • Some data that you want to search;
    • a pattern you might search for;
    • the output you desire.
Re: Grep reutrn the occurance of a pattern
by Pancho (Pilgrim) on Mar 13, 2008 at 14:01 UTC
Re: Grep reutrn the occurance of a pattern
by ikegami (Patriarch) on Mar 13, 2008 at 15:56 UTC
    See @- and @+
    while (/.../g) { print("Matched ", substr($var, $-[0], $+[0] - $-[0]), " at pos $-[0 +]\n"); }
Re: Grep reutrn the occurance of a pattern
by Anonymous Monk on Mar 14, 2008 at 01:19 UTC
    Hi all,

    I am the one who ask the question in rush, Thanks all of you answer this poorly organized question. After certain period of time and read the reply, I think I prefer something like this....

    my @match = grep { $_ =~ /($pattern)/ ; $hash{$_}{$pattern} = no_of_o +ccurance_of_regex; } @inputs;

    So I wil get a hash with the thing I want. Does it possible in grep?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found