Re: basic question help with strings
am 28.01.2006 15:06:33 von someone
Darren & Linda Ingall wrote:
> new to perl not a clue how cant find example
>
> $a = ("ab ab ab ab ab"
> $b = ("ab")
> $times = # count how many times $b is in
> $a
>
> a simple 1 liner for sombody who knows ?
perldoc -q "How can I count the number of occurrences of a substring within a
string"
John
--
use Perl;
program
fulfillment
Re: basic question help with strings
am 28.01.2006 17:01:13 von darren_linda
thanks i found that last night # still not got a clue
#!user/bin/perl -w
$count =0;
$me = "-";
$string = "-954548-2ab4abababab23 -76 4 1-4 -44";
while ($string =~ /$me\d+/g) { $count++ }
print $count
all the time $me = '-' it works but if i want $me to = "ab" no go
can sombody please explain
=~ /$me\d+/g
Re: basic question help with strings
am 29.01.2006 01:02:45 von Joe Smith
Darren & Linda Ingall wrote:
> thanks i found that last night # still not got a clue
> #!user/bin/perl -w
> $count =0;
> $me = "-";
> $string = "-954548-2ab4abababab23 -76 4 1-4 -44";
> while ($string =~ /$me\d+/g) { $count++ }
> print $count
>
> all the time $me = '-' it works but if i want $me to = "ab" no go
Works exactly as expected for me. Seven instances of hyphen
followed by one or more digits. Two instances of "ab" followed
by one or more digits. (Also, it's "/usr" not "user".)
linux% cat test.pl
#!/usr/bin/perl -w
$string = "-954548-2ab4abababab23 -76 4 1-4 -44";
print "Checking string '$string'\n";
for $me ('-', 'ab') {
$count =0;
while ($string =~ /$me\d+/g) { $count++ }
print "For '$me', count = $count\n";
}
linux% ./test.pl
Checking string '-954548-2ab4abababab23 -76 4 1-4 -44'
For '-', count = 5
For 'ab', count = 2
> can sombody please explain
>
> =~ /$me\d+/g
=~ is the binding operator - used when searching/matching strings
in a variable other than $_.
//g finds all matches at once in list context, all matches
one at a time in scalar context (such as 'while()').
\d+ is something you should know already.
This is all explained on any decent tutorial on perl's regular
expressions operators. Which tutorial are you using?
-Joe
P.S. There is more traffic in comp.lang.perl.misc, although you
might find it overwhelming.
Re: basic question help with strings
am 05.02.2006 18:47:11 von Rich
Darren & Linda Ingall wrote:
> new to perl not a clue how cant find example
>
> $a = ("ab ab ab ab ab"
> $b = ("ab")
> $times = # count how many times $b is in
> $a
>
> a simple 1 liner for sombody who knows ?
>
perl -le '$x="ab ab ab";$me="ab";$x=~s/$me/$c++/eg;print $c'
3
you can use the /e to run a command instead of replacing
$x=~s/$me/$c++/eg
Rich
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---