Click to See Complete Forum and Search --> : What does this Statement Does?
kimskams80
02-05-2009, 07:50 PM
Hi
What does this statement does
my @temp = ($string = ~/($word)/gi);
I'm using this to count the number of occurences of a substring $word in main string $string.. What does it return in @temp??
Thanks
Sixtease
02-06-2009, 01:50 AM
Temp will contain the substrings of $string that match $word. For example, if $word is "a.", and $string is "As I am", then @temp will contain ("As", "am").
kimskams80
02-06-2009, 03:16 AM
Thanks for reply.
In your example, $word contains "a." or simply "a" ??
And also how can I modify this statement to match the total word in a main string ... like if I have $string="I am kidding with a kid";
and I want to count occurrences of $word="kid" .. bz I think using same statement it will return in @temp="kidding" "kid" but I just want @tem="kid"
thanks in adv
Sixtease
02-06-2009, 03:23 AM
I had "a.". The dot matches any character. You could surround the $word with "\b" (\b means word boundary).my $word = 'kid';
my $string = 'I am kidding with a kid';
my @temp = ($string =~ /\b($word)\b/gi);
Now @temp only contains one occurrence of "kid".
kimskams80
03-09-2009, 11:26 AM
Hi 60s
What does \b or word boundary means here?
I mean how it would recognize the word boundary? Does it use the "Space" around the word or something else? Bz in my case its not necessary that each word's boundary is marked by "spaces" around it. Sometimes there is no space.
Like if I want to find the word "I" (the subject) in a text
"Yes they r gone.I am going too"
then you can see there is no space before "I" but its preceded by a "." (full stop) or I give you an example of a text like
"Yes they r gone.I'm going too"
Here "I" is followed by symbol '
So can you clarify the boundary here
Thanks in Adv
Sixtease
03-09-2009, 06:12 PM
I don't want to be rude but
why didn't you try if \b matches what you need and
why didn't you consult the documentation (http://perldoc.perl.org/perlreref.html#ANCHORS)?
\b means a \w followed by a \W or vice versa -- a word character on one side and a non-word character on the other.
So yes, in both "gone.I" and "I'm" the word "I" would be recognized.
kimskams80
03-09-2009, 06:48 PM
I am very very sorry sir for disturbing you. I did consult the documentation .. but I was not understanding ... So ..
Sorry however.. I would try my best to understand all and then I will contact here.
Thankssss for All :)