Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Count how many times find and replaced happened

Reply
Thread Tools

Count how many times find and replaced happened

 
 
blnukem
Guest
Posts: n/a
 
      04-29-2004
Hi All

Is there a way to count how many times $Line =~ s///; has happened?

foreach my $Line (@ArrayToRead) {
$Line =~ s/TextToFind/TextToReplace/i;
}

Thank You
Blnukem


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-29-2004
blnukem <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Hi All
>
> Is there a way to count how many times $Line =~ s///; has happened?
>
> foreach my $Line (@ArrayToRead) {
> $Line =~ s/TextToFind/TextToReplace/i;
> }


Use grep in scalar context:

my $count = grep s/TextToFind/TextToReplace/i, @ArrayToRead;

Anno
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-29-2004
blnukem <(E-Mail Removed)> wrote:

> Is there a way to count how many times $Line =~ s///; has happened?



my $how_many_times = $Line =~ s///;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      04-29-2004
Anno Siegel <(E-Mail Removed)-berlin.de> wrote:

> blnukem <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>> Is there a way to count how many times $Line =~ s///; has
>> happened?
>>
>> foreach my $Line (@ArrayToRead) {
>> $Line =~ s/TextToFind/TextToReplace/i;
>> }

>
> Use grep in scalar context:
>
> my $count = grep s/TextToFind/TextToReplace/i, @ArrayToRead;


That's an interesting use of grep and s///.

My first thought was that he meant the number of substitutions, which
can be very different. That is,

my $count;
$count += s/TextToFind/TextToReplace/g for @array;

(note 'g' option)

/me shrugs...
 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      04-29-2004

"blnukem" <(E-Mail Removed)> wrote in message
news:S%6kc.42117$(E-Mail Removed) et...
> Hi All
>
> Is there a way to count how many times $Line =~ s///; has happened?
>
> foreach my $Line (@ArrayToRead) {
> $Line =~ s/TextToFind/TextToReplace/i;
> }
>


my $count = 0;
foreach my $Line (@ArrayToRead) {
$Line =~ s/TextToFind/TextToReplace/i and $count += 1;
}
-Robin



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-29-2004
Robin <webmaster@infusedlight> wrote:

> From: "Robin" <webmaster @ infusedlight . net>



Please choose one posting address and stick to it.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      04-29-2004

"Tad McClellan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Robin <webmaster@infusedlight> wrote:
>
> > From: "Robin" <webmaster @ infusedlight . net>

>
>
> Please choose one posting address and stick to it.

yeah I did...sorry.
-Robin



 
Reply With Quote
 
Alien Resident
Guest
Posts: n/a
 
      04-29-2004
Tad McClellan wrote:

> Robin <webmaster@infusedlight> wrote:
>
>
>>From: "Robin" <webmaster @ infusedlight . net>

>
>
>
> Please choose one posting address and stick to it.
>
>

oh, yea... a couple got by me. Let's make this simple... contains: infusedlight or Robin
or potential for teeth grinding... OK let's hope that does it.

-AR
 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      04-29-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-04-29, Robin <webmaster@infusedlight> wrote:
>
> "Tad McClellan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> Please choose one posting address and stick to it.


> yeah I did...sorry.


You did? This is the third address you've used in the span of a few
weeks. Webmaster, indeed--I hope your webmaster skillz are markedly
better than your skillz "creating dynamic websites with CGI".

- --keith

- --
http://www.velocityreviews.com/forums/(E-Mail Removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAkYcThVcNCxZ5ID8RAoSsAJ9q/DYvsSl2k3mo33pyIUsU4uzSgQCfQQME
O2p6VLy5IL5gJB6me2iXH9w=
=XJEX
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      04-30-2004

"Keith Keller" <(E-Mail Removed)-francisco.ca.us> wrote in message
news:(E-Mail Removed)-francisco.ca.us...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 2004-04-29, Robin <webmaster@infusedlight> wrote:
> >
> > "Tad McClellan" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >>
> >> Please choose one posting address and stick to it.

>
> > yeah I did...sorry.

>
> You did? This is the third address you've used in the span of a few
> weeks. Webmaster, indeed--I hope your webmaster skillz are markedly
> better than your skillz "creating dynamic websites with CGI".


well, two of those email addresses are prefixed with "webmaster", but it
doesn't improve my webmaster skills.
-Robin


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to find number of times certain conditions happened in an array? lightaiyee@gmail.com C++ 4 11-26-2007 11:39 AM
How many times is too many times? Jules W MCSA 3 08-30-2005 07:33 PM
replaced vs non replaced Shawn Modersohn HTML 4 07-20-2004 06:58 AM
How to count how many times a submit button is hit Mogga HTML 3 11-18-2003 09:43 PM



Advertisments