Message from discussion
Count number of times matched
Newsgroups: perl.beginners
Path: g2news1.google.com!news3.google.com!newsfeed.stanford.edu!nntp.perl.org
Return-Path: <f...@hashref.com>
Mailing-List: contact beginners-h...@perl.org; run by ezmlm
Delivered-To: mailing list beginn...@perl.org
Received: (qmail 31630 invoked from network); 1 Dec 2005 14:40:05 -0000
Received: from x1a.develooper.com (HELO x1.develooper.com) (216.52.237.111)
by lists.develooper.com with SMTP; 1 Dec 2005 14:40:05 -0000
Received: (qmail 21382 invoked by uid 225); 1 Dec 2005 14:40:04 -0000
Delivered-To: beginn...@perl.org
Received: (qmail 21375 invoked by alias); 1 Dec 2005 14:40:03 -0000
X-Spam-Status: No, hits=-2.5 required=8.0
tests=BAYES_00,FORGED_RCVD_HELO
X-Spam-Check-By: la.mx.develooper.com
Received-SPF: neutral (x1.develooper.com: local policy)
Received: from terremoto.isoco.net (HELO smtp.isoco.com) (62.81.148.13)
by la.mx.develooper.com (qpsmtpd/0.28) with ESMTP; Thu, 01 Dec 2005 06:39:55 -0800
Received: from localhost (localhost.localdomain [127.0.0.1])
by smtp.isoco.com (Postfix-Nx) with ESMTP id 06CBE27671
for <beginn...@perl.org>; Thu, 1 Dec 2005 15:39:46 +0100 (CET)
Received: from smtp.isoco.com ([127.0.0.1])
by localhost (terremoto.isoco.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 02087-10 for <beginn...@perl.org>;
Thu, 1 Dec 2005 15:39:42 +0100 (CET)
Received: from intranet.isoco.net (vinson.isoco.net [172.16.0.45])
by smtp.isoco.com (Postfix-Nx) with ESMTP id 296752766F
for <beginn...@perl.org>; Thu, 1 Dec 2005 15:39:42 +0100 (CET)
Received: from [172.16.1.38] (fxnw.bcn.isoco.net [172.16.1.38])
(using TLSv1 with cipher RC4-SHA (128/128 bits))
(No client certificate requested)
by intranet.isoco.net (Postfix) with ESMTP id E66B94DD8C
for <beginn...@perl.org>; Thu, 1 Dec 2005 15:39:43 +0100 (CET)
Mime-Version: 1.0 (Apple Message framework v746.2)
In-Reply-To: <20051201141326.clcgtexi0w88kk8w@www.sms.ed.ac.uk>
References: <20051201141326.clcgtexi0w88kk8w@www.sms.ed.ac.uk>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
Message-ID: <669A822B-0978-4EC2-80B5-EDE4C09BE...@hashref.com>
Content-Transfer-Encoding: 7bit
Subject: Re: Count number of times matched
Date: Thu, 1 Dec 2005 15:39:41 +0100
To: beginners perl <beginn...@perl.org>
X-Mailer: Apple Mail (2.746.2)
X-Virus-Scanned: by amavisd-new at isoco.com
Approved: n...@nntp.perl.org
From: f...@hashref.com (Xavier Noria)
On Dec 1, 2005, at 15:13, SG Edwards wrote:
> Hi folks,
>
> If I want to count the number of times that a match occurs in a
> line is there a way of doing this, if there is I have not found it!
>
> e.g.
> $line="This is a sentence about matching sentences.\n";
>
> $line=~/sentence/ig;
If you just want to count occurrences you can use a counter:
my $c = 0;
++$c while $line =~ /sentence/g;
There's a shorter idiom that takes advantage of the way list
assignments work in scalar context that involves something known as
the "goatse operator" (that's jargon it's not a real operator):
my $c =()= $line =~ /sentence/g;
but you have to choose which one is more easy to understand for you.
> Is there a way to automatically capture each individual match (e.g.
> using a special array character?)
I don't know whether this is what you ask for, but m//g in list
context returns the captures in one shot:
my @tags = $html =~ /<(\w+)/g;
That is documented in perlop.
-- fxn