<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GeeksPlanet.net &#187; array</title>
	<atom:link href="http://geeksplanet.net/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://geeksplanet.net</link>
	<description>C, C++, JAVA, PERL programming for Dummies</description>
	<lastBuildDate>Wed, 24 Feb 2010 11:38:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A simple perl program to count the occurences of a string in a given text.</title>
		<link>http://geeksplanet.net/2009/07/perl/a-simple-perl-program-to-count-the-occurences-of-a-string-in-a-given-text/</link>
		<comments>http://geeksplanet.net/2009/07/perl/a-simple-perl-program-to-count-the-occurences-of-a-string-in-a-given-text/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 06:23:12 +0000</pubDate>
		<dc:creator>Satish Gandham</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[chop]]></category>
		<category><![CDATA[split]]></category>

		<guid isPermaLink="false">http://geeksplanet.net/?p=114</guid>
		<description><![CDATA[In this program we are going to use the split function in perl to count the occurrences of a particular string in a given text. Analysis In line 5, if we don&#8217;t add the blank space before and after the text, we will not be able to count the occurrence of the string at the [...]]]></description>
			<content:encoded><![CDATA[<p>In this program we are going to use the split function in perl to count the occurrences of a particular string in a given text.</p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/bin/perl
#A perl program to count the occrrences of a particular string in a given text
print(&quot;Enter some text\n&quot;);
$text=&quot; &quot;.&lt;stdin&gt;.&quot; &quot;;
print(&quot;Enter the string whose occurrences you want to count:&quot;);
$string=&lt;stdin&gt;;
chop($string);
@array= split(/$string/,$text);
$count=@array;
print(&quot;\nThe number of occurrences of the string \&quot;$string\&quot; is &quot;. ($count-1).&quot;\n&quot;);
</pre>
<p><strong>Analysis</strong></p>
<p><strong>In line 5,</strong> if we don&#8217;t add the blank space before and after the text, we will not be able to count the occurrence of the string at the beginning and end.<br />
<strong>In line 7 </strong>we are <strong>chopping</strong> the string to get rid of the new line character at the end, if we don&#8217;t do this we wont get the count we are expecting.<br />
<strong>In line 8</strong> we are using the split function. <strong>Split</strong> is a library function, it splits the string when ever it see the sub string between //.<br />
for example @array=split(/the/, &#8220;this is a test string to, this string to split when ever there is an occurrence of the word this&#8221;);<br />
now the array will have the following elements<br />
<strong>$array[0]</strong>=&#8221; is a test string to, &#8221;<br />
<strong>$array[1]</strong>=&#8221;string to split when ever there is an occurrence of the word &#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://geeksplanet.net/2009/07/perl/a-simple-perl-program-to-count-the-occurences-of-a-string-in-a-given-text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

