Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Using grep in a scalar context

by AnomalousMonk (Abbot)
on Feb 06, 2013 at 13:16 UTC ( #1017420=note: print w/ replies, xml ) Need Help??


in reply to Using grep in a scalar context

You say the approach you are using is "not working", leaving us to guess what data you are using and what result you expect.

My guesses are that either you want to count the number of strings in a dataset (i.e., an array) in which a pattern occurs at least once, or you want to count the total occurrences of a pattern in all strings in a dataset.

The code you posted seems to serve for the first purpose. A variation using map seems to take care of the other. (Note that the pattern occurs twice in 'zAAzAAz'.) In neither case is the original dataset changed.

>perl -wMstrict -le "my @FMat; my @dipept = qw(xAAx yAAy xyzzy zAAzAAz zzzz); ;; $FMat[0][0] = grep(/AA/, @dipept); print qq{grepped: $FMat[0][0]}; ;; $FMat[0][0] = map /AA/g, @dipept; print qq{mapped: $FMat[0][0]}; ;; print qq{@dipept}; " grepped: 3 mapped: 4 xAAx yAAy xyzzy zAAzAAz zzzz

Update: Do we need to consider the question of overlapping versus non-overlapping pattern matches? I.e., How many matches are there in 'xAAAx'?


Comment on Re: Using grep in a scalar context
Download Code
Re^2: Using grep in a scalar context
by newbie1991 (Acolyte) on Feb 06, 2013 at 13:39 UTC
    I'm counting total occurrences of a pattern in the dataset. Input in array format is M H D L N with each element being one letter. Output should count how many times MH, HD, DL, etc. appear. The input is MUCH longer (it's a amino acid sequence). And yes, overlaps are considered. AAA has 2 matches.
      I am still not sure what your input is, but I hope you might find one of the following two solutions helpful:
      use Data::Dumper; my $string = 'MHDLKNDHASDRWT'; my %count_string; $count_string{$_}++ for $string =~ /(?=(..))/g; # Uses look-ahead to o +nly progress by one character. my @array = split //, $string; my %count_array; $count_array{ join q(), @array[$_, $_ + 1] }++ for 0 .. $#array - 1; print Dumper \%count_string, \%count_array;
      Note: /AA/ matches the capital letter A followed by the capital letter A. It does not stand for "anything" in regular expressions.

      Updated: Added the hashes.

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-10-20 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls