For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2004 > Count how many times find and replaced happened









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Count how many times find and replaced happened
blnukem

2004-04-29, 11:50 am

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


Anno Siegel

2004-04-29, 11:50 am

blnukem <bill@hotmail.com> 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
David K. Wall

2004-04-29, 11:50 am

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:

> blnukem <bill@hotmail.com> wrote in comp.lang.perl.misc:
>
> 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...
Tad McClellan

2004-04-29, 11:50 am

blnukem <bill@hotmail.com> wrote:

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



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


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Robin

2004-04-29, 7:23 pm


"blnukem" <bill@hotmail.com> wrote in message
news:S%6kc.42117$Gd3.9618519@news4.srv.hcvlny.cv.net...
> 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



Tad McClellan

2004-04-30, 3:41 am

Robin <webmaster@infusedlight> wrote:

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



Please choose one posting address and stick to it.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Robin

2004-04-30, 3:42 am


"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnc92qpb.3qo.tadmc@magna.augustmail.com...
> Robin <webmaster@infusedlight> wrote:
>
>
>
> Please choose one posting address and stick to it.

yeah I did...sorry.
-Robin



Alien Resident

2004-04-30, 3:42 am

Tad McClellan wrote:

> Robin <webmaster@infusedlight> wrote:
>
>
>
>
>
> 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
Keith Keller

2004-04-30, 3:42 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-04-29, Robin <webmaster@infusedlight> wrote:
>
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnc92qpb.3qo.tadmc@magna.augustmail.com...
[color=darkred]
> yeah I did...sorry.


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

- --keith

- --
kkeller-usenet@wombat.san-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-----
Robin

2004-04-30, 3:44 am


"Keith Keller" <kkeller-usenet@wombat.san-francisco.ca.us> wrote in message
news:ku0s6c.mq.ln@goaway.wombat.san-francisco.ca.us...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 2004-04-29, Robin <webmaster@infusedlight> wrote:
>
>
> You did? This is the third address you've used in the span of a few
> ws. 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com