<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Word Count</title>
	<atom:link href="http://programmingpraxis.com/2009/12/08/word-count/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/12/08/word-count/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: slabounty</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-1911</link>
		<dc:creator><![CDATA[slabounty]]></dc:creator>
		<pubDate>Mon, 15 Nov 2010 00:05:55 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-1911</guid>
		<description><![CDATA[A ruby version that a) doesn&#039;t look all that rubyish and b) doesn&#039;t format the output nicely but does work more or less like the unix version 

[sourcecode lang=&quot;ruby&quot;]
require &#039;getoptlong&#039;

opts = GetoptLong.new(
    [&quot;--words&quot;, &quot;-w&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--chars&quot;, &quot;-c&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--lines&quot;, &quot;-l&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--verbose&quot;, &quot;-v&quot;, GetoptLong::NO_ARGUMENT]
    )

words = false
lines = false
chars = false
$verbose = false

begin
    opts.each do &#124; opt, arg&#124;
        case opt
        when &quot;--words&quot;
            words = true
        when &quot;--chars&quot;
            chars = true
        when &quot;--lines&quot;
            lines = true
        when &quot;--verbose&quot;
            $verbose = true
        end
    end
rescue
    puts &quot;Illegal command line option.&quot;
    exit
end

accumulate = false
if ARGV.length &gt; 1
    accumulate = true
    wcl_totals = Hash.new(0)
end

puts &quot;accumulate = #{accumulate}&quot;

ARGV.each do &#124;file_name&#124;
    File.open(file_name) do &#124; file &#124;

        wcl = Hash.new(0)

        while line = file.gets
            wcl[:words] += line.split.length
            wcl[:chars] += line.length
            wcl[:lines] += 1
        end

        if accumulate
            wcl_totals[:words] += wcl[:words]
            wcl_totals[:chars] += wcl[:chars]
            wcl_totals[:lines] += wcl[:lines]
        end

        puts &quot;#{wcl[:words] if words} #{wcl[:lines] if lines} #{wcl[:chars] if chars} #{file_name}&quot;
    end
end

puts &quot;#{wcl_totals[:words] if words} #{wcl_totals[:lines] if lines} #{wcl_totals[:chars] if chars} Total&quot; if accumulate
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>A ruby version that a) doesn&#8217;t look all that rubyish and b) doesn&#8217;t format the output nicely but does work more or less like the unix version </p>
<pre class="brush: ruby;">
require 'getoptlong'

opts = GetoptLong.new(
    [&quot;--words&quot;, &quot;-w&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--chars&quot;, &quot;-c&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--lines&quot;, &quot;-l&quot;, GetoptLong::NO_ARGUMENT],
    [&quot;--verbose&quot;, &quot;-v&quot;, GetoptLong::NO_ARGUMENT]
    )

words = false
lines = false
chars = false
$verbose = false

begin
    opts.each do | opt, arg|
        case opt
        when &quot;--words&quot;
            words = true
        when &quot;--chars&quot;
            chars = true
        when &quot;--lines&quot;
            lines = true
        when &quot;--verbose&quot;
            $verbose = true
        end
    end
rescue
    puts &quot;Illegal command line option.&quot;
    exit
end

accumulate = false
if ARGV.length &gt; 1
    accumulate = true
    wcl_totals = Hash.new(0)
end

puts &quot;accumulate = #{accumulate}&quot;

ARGV.each do |file_name|
    File.open(file_name) do | file |

        wcl = Hash.new(0)

        while line = file.gets
            wcl[:words] += line.split.length
            wcl[:chars] += line.length
            wcl[:lines] += 1
        end

        if accumulate
            wcl_totals[:words] += wcl[:words]
            wcl_totals[:chars] += wcl[:chars]
            wcl_totals[:lines] += wcl[:lines]
        end

        puts &quot;#{wcl[:words] if words} #{wcl[:lines] if lines} #{wcl[:chars] if chars} #{file_name}&quot;
    end
end

puts &quot;#{wcl_totals[:words] if words} #{wcl_totals[:lines] if lines} #{wcl_totals[:chars] if chars} Total&quot; if accumulate
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: wc: Word Count &#124; Andrew Ferguson</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-831</link>
		<dc:creator><![CDATA[wc: Word Count &#124; Andrew Ferguson]]></dc:creator>
		<pubDate>Sun, 13 Dec 2009 00:41:06 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-831</guid>
		<description><![CDATA[[...] to the RSS feed or email list for updates on this topic.The goal for this Programming Praxis was to implement the Unix wc function. This one took me a couple days (I haven&#8217;t had a lot of time recently) to complete, but I [...]]]></description>
		<content:encoded><![CDATA[<p>[...] to the RSS feed or email list for updates on this topic.The goal for this Programming Praxis was to implement the Unix wc function. This one took me a couple days (I haven&#8217;t had a lot of time recently) to complete, but I [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank Gleason</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-828</link>
		<dc:creator><![CDATA[Frank Gleason]]></dc:creator>
		<pubDate>Thu, 10 Dec 2009 23:59:46 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-828</guid>
		<description><![CDATA[I too posted before reading the instructions. Sorry about that.

[sourcecode lang=&quot;cpp&quot;]
#include &lt;stdio.h&gt;
#include &lt;sys/fcntl.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

int lflag = 0;
int wflag = 0;
int cflag = 0;
char *fname = &#039;&#092;&#048;&#039;;
char buf[BUFSIZ];
extern int errno;

main(argc, argv)
int argc;
char *argv[];
  { 
    int i, fd;

    while (argc &gt; 1 &amp;&amp; argv[1][0] == &#039;-&#039;) {
      switch (argv[1][1]) {
        case &#039;l&#039; : lflag = 1;
                   break;
	case &#039;w&#039; : wflag = 1;
		   break;
	case &#039;c&#039; : cflag = 1;
		   break;
	default : printf(&quot;usage: wc [-lwc] [name...]\n&quot;);
		  exit(1);
        }
      argc--;
      argv++;
      }
    if (lflag == 0 &amp;&amp; wflag == 0 &amp;&amp; cflag == 0)
      lflag = wflag = cflag = 1;
    if (argc == 1)
      wc(STDIN_FILENO);
    else
      for (i = 1; i &lt; argc; i++)
        if ((fd = open(argv[i], O_RDONLY)) == -1) {
	  printf(&quot;%s: can not open %s, errno=%d\n&quot;, argv[0], argv[i],errno);
	  exit(1);
          }
        else
          {
            fname = argv[i];
            wc(fd);
            close(fd);
          }
    exit(0);
  }

wc(fd)
int fd;
  { 
    int n, l = 0, w = 0, t = 0, ws = 1;
    char *cp, c;

    while (n = read(fd, buf, BUFSIZ)) {
        t += n;
        for (cp = buf; cp != (buf + n); cp++) {
            c = *cp;
            if (c == &#039;\n&#039;) {
                l++;
                ws = 1;
              }
            else  
            if (c != &#039; &#039; &amp;&amp; c != &#039;\t&#039;) {
                if (ws) {
	            ws = 0;
	            w++;
                  }
              } 
	    else 
              ws = 1;
          }
      }
    if (lflag)
      printf (&quot;%d &quot;, l);
    if (wflag)
      printf(&quot;%d &quot;, w);
    if (cflag)
      printf(&quot;%d &quot;, t);
    if (fname)
      printf(&quot;%s&quot;, fname);
    printf(&quot;\n&quot;);
  }
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I too posted before reading the instructions. Sorry about that.</p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;sys/fcntl.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

int lflag = 0;
int wflag = 0;
int cflag = 0;
char *fname = '&#92;&#48;';
char buf[BUFSIZ];
extern int errno;

main(argc, argv)
int argc;
char *argv[];
  { 
    int i, fd;

    while (argc &gt; 1 &amp;&amp; argv[1][0] == '-') {
      switch (argv[1][1]) {
        case 'l' : lflag = 1;
                   break;
	case 'w' : wflag = 1;
		   break;
	case 'c' : cflag = 1;
		   break;
	default : printf(&quot;usage: wc [-lwc] [name...]\n&quot;);
		  exit(1);
        }
      argc--;
      argv++;
      }
    if (lflag == 0 &amp;&amp; wflag == 0 &amp;&amp; cflag == 0)
      lflag = wflag = cflag = 1;
    if (argc == 1)
      wc(STDIN_FILENO);
    else
      for (i = 1; i &lt; argc; i++)
        if ((fd = open(argv[i], O_RDONLY)) == -1) {
	  printf(&quot;%s: can not open %s, errno=%d\n&quot;, argv[0], argv[i],errno);
	  exit(1);
          }
        else
          {
            fname = argv[i];
            wc(fd);
            close(fd);
          }
    exit(0);
  }

wc(fd)
int fd;
  { 
    int n, l = 0, w = 0, t = 0, ws = 1;
    char *cp, c;

    while (n = read(fd, buf, BUFSIZ)) {
        t += n;
        for (cp = buf; cp != (buf + n); cp++) {
            c = *cp;
            if (c == '\n') {
                l++;
                ws = 1;
              }
            else  
            if (c != ' ' &amp;&amp; c != '\t') {
                if (ws) {
	            ws = 0;
	            w++;
                  }
              } 
	    else 
              ws = 1;
          }
      }
    if (lflag)
      printf (&quot;%d &quot;, l);
    if (wflag)
      printf(&quot;%d &quot;, w);
    if (cflag)
      printf(&quot;%d &quot;, t);
    if (fname)
      printf(&quot;%s&quot;, fname);
    printf(&quot;\n&quot;);
  }
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank Gleason</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-827</link>
		<dc:creator><![CDATA[Frank Gleason]]></dc:creator>
		<pubDate>Thu, 10 Dec 2009 19:11:23 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-827</guid>
		<description><![CDATA[#include 
#include 
#include 
#include 

int lflag = 0;
int wflag = 0;
int cflag = 0;
char *fname = &#039;&#039;;
char buf[BUFSIZ];
extern int errno;

main(argc, argv)
int argc;
char *argv[];
  { 
    int i, fd;

    while (argc &gt; 1 &amp;&amp; argv[1][0] == &#039;-&#039;) {
      switch (argv[1][1]) {
        case &#039;l&#039; : lflag = 1;
                   break;
        case &#039;w&#039; : wflag = 1;
                   break;
        case &#039;c&#039; : cflag = 1;
                   break;
        default : printf(&quot;usage: \n&quot;);
                  exit(1);
        }
      argc--;
      argv++;
      }
    if (lflag == 0 &amp;&amp; wflag == 0 &amp;&amp; cflag == 0)
      lflag = wflag = cflag = 1;
    if (argc == 1)
      wc(STDIN_FILENO);
    else
      for (i = 1; i &lt; argc; i++)
        if ((fd = open(argv[i], O_RDONLY)) == -1) {
          printf(&quot;%s: can not open %s, errno=%d\n&quot;, argv[0], argv[i],errno);
          exit(1);
          }
        else
          {
            fname = argv[i];
            wc(fd);
            close(fd);
          }
    exit(0);
  }

wc(fd)
int fd;
  { 
    int n, l = 0, w = 0, t = 0, ws = 1;
    char *cp, c;

    while (n = read(fd, buf, BUFSIZ)) {
        t += n;
        for (cp = buf; cp != (buf + n); cp++) {
            c = *cp;
            if (c == &#039;\n&#039;) {
                l++;
                ws = 1;
              }
            else  
            if (c != &#039; &#039; &amp;&amp; c != &#039;\t&#039;) {
                if (ws) {
                    ws = 0;
                    w++;
                  }
              } 
            else 
              ws = 1;
          }
      }
    if (lflag)
      printf (&quot;%d &quot;, l);
    if (wflag)
      printf(&quot;%d &quot;, w);
    if (cflag)
      printf(&quot;%d &quot;, t);
    if (fname)
      printf(&quot;%s&quot;, fname);
    printf(&quot;\n&quot;);
  }]]></description>
		<content:encoded><![CDATA[<p>#include<br />
#include<br />
#include<br />
#include </p>
<p>int lflag = 0;<br />
int wflag = 0;<br />
int cflag = 0;<br />
char *fname = &#8221;;<br />
char buf[BUFSIZ];<br />
extern int errno;</p>
<p>main(argc, argv)<br />
int argc;<br />
char *argv[];<br />
  {<br />
    int i, fd;</p>
<p>    while (argc &gt; 1 &amp;&amp; argv[1][0] == &#8216;-&#8217;) {<br />
      switch (argv[1][1]) {<br />
        case &#8216;l&#8217; : lflag = 1;<br />
                   break;<br />
        case &#8216;w&#8217; : wflag = 1;<br />
                   break;<br />
        case &#8216;c&#8217; : cflag = 1;<br />
                   break;<br />
        default : printf(&#8220;usage: \n&#8221;);<br />
                  exit(1);<br />
        }<br />
      argc&#8211;;<br />
      argv++;<br />
      }<br />
    if (lflag == 0 &amp;&amp; wflag == 0 &amp;&amp; cflag == 0)<br />
      lflag = wflag = cflag = 1;<br />
    if (argc == 1)<br />
      wc(STDIN_FILENO);<br />
    else<br />
      for (i = 1; i &lt; argc; i++)<br />
        if ((fd = open(argv[i], O_RDONLY)) == -1) {<br />
          printf(&quot;%s: can not open %s, errno=%d\n&quot;, argv[0], argv[i],errno);<br />
          exit(1);<br />
          }<br />
        else<br />
          {<br />
            fname = argv[i];<br />
            wc(fd);<br />
            close(fd);<br />
          }<br />
    exit(0);<br />
  }</p>
<p>wc(fd)<br />
int fd;<br />
  {<br />
    int n, l = 0, w = 0, t = 0, ws = 1;<br />
    char *cp, c;</p>
<p>    while (n = read(fd, buf, BUFSIZ)) {<br />
        t += n;<br />
        for (cp = buf; cp != (buf + n); cp++) {<br />
            c = *cp;<br />
            if (c == &#039;\n&#039;) {<br />
                l++;<br />
                ws = 1;<br />
              }<br />
            else<br />
            if (c != &#039; &#039; &amp;&amp; c != &#039;\t&#039;) {<br />
                if (ws) {<br />
                    ws = 0;<br />
                    w++;<br />
                  }<br />
              }<br />
            else<br />
              ws = 1;<br />
          }<br />
      }<br />
    if (lflag)<br />
      printf (&quot;%d &quot;, l);<br />
    if (wflag)<br />
      printf(&quot;%d &quot;, w);<br />
    if (cflag)<br />
      printf(&quot;%d &quot;, t);<br />
    if (fname)<br />
      printf(&quot;%s&quot;, fname);<br />
    printf(&quot;\n&quot;);<br />
  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miguel Valadas</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-826</link>
		<dc:creator><![CDATA[Miguel Valadas]]></dc:creator>
		<pubDate>Wed, 09 Dec 2009 16:35:04 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-826</guid>
		<description><![CDATA[Sorry about the previous post:
[sourcecode lang=&quot;Ruby&quot;]
# Init Some Variables
count_lines = true
count_words = true
count_chars = true

line_count = 0
word_count = 0
char_count = 0

filename = ARGV[0]
  
  
#Check Arguments
if(ARGV[0] =~ /^-.+$/)
  count_lines = (ARGV[0].index(&#039;l&#039;) != nil)
  count_words = (ARGV[0].index(&#039;w&#039;) != nil)
  count_chars = (ARGV[0].index(&#039;c&#039;) != nil)
  filename = ARGV[1]
end

#Count with Regexp

file = File.open(filename,&#039;r+&#039;)
while (line = file.gets) do
  line_count += 1;
  word_count += line.scan(/[^ \n\t]+/).size
  char_count += line.size
end

puts &quot;Line Count = #{line_count}&quot; unless !count_lines
puts &quot;Word Count = #{word_count}&quot; unless !count_words
puts &quot;Character Count = #{char_count}&quot; unless !count_chars
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Sorry about the previous post:</p>
<pre class="brush: ruby;">
# Init Some Variables
count_lines = true
count_words = true
count_chars = true

line_count = 0
word_count = 0
char_count = 0

filename = ARGV[0]
  
  
#Check Arguments
if(ARGV[0] =~ /^-.+$/)
  count_lines = (ARGV[0].index('l') != nil)
  count_words = (ARGV[0].index('w') != nil)
  count_chars = (ARGV[0].index('c') != nil)
  filename = ARGV[1]
end

#Count with Regexp

file = File.open(filename,'r+')
while (line = file.gets) do
  line_count += 1;
  word_count += line.scan(/[^ \n\t]+/).size
  char_count += line.size
end

puts &quot;Line Count = #{line_count}&quot; unless !count_lines
puts &quot;Word Count = #{word_count}&quot; unless !count_words
puts &quot;Character Count = #{char_count}&quot; unless !count_chars
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Miguel Valadas</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-825</link>
		<dc:creator><![CDATA[Miguel Valadas]]></dc:creator>
		<pubDate>Wed, 09 Dec 2009 16:29:13 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-825</guid>
		<description><![CDATA[# Init Some Variables
count_lines = true
count_words = true
count_chars = true

line_count = 0
word_count = 0
char_count = 0

filename = ARGV[0]
  
  
#Check Arguments
if(ARGV[0] =~ /^-.+$/)
  count_lines = (ARGV[0].index(&#039;l&#039;) != nil)
  count_words = (ARGV[0].index(&#039;w&#039;) != nil)
  count_chars = (ARGV[0].index(&#039;c&#039;) != nil)
  filename = ARGV[1]
end

#Count with Regexp

file = File.open(filename,&#039;r+&#039;)
while (line = file.gets) do
  line_count += 1;
  word_count += line.scan(/[^ \n\t]+/).size
  char_count += line.size
end

puts &quot;Line Count = #{line_count}&quot; unless !count_lines
puts &quot;Word Count = #{word_count}&quot; unless !count_words
puts &quot;Character Count = #{char_count}&quot; unless !count_chars]]></description>
		<content:encoded><![CDATA[<p># Init Some Variables<br />
count_lines = true<br />
count_words = true<br />
count_chars = true</p>
<p>line_count = 0<br />
word_count = 0<br />
char_count = 0</p>
<p>filename = ARGV[0]</p>
<p>#Check Arguments<br />
if(ARGV[0] =~ /^-.+$/)<br />
  count_lines = (ARGV[0].index(&#8216;l&#8217;) != nil)<br />
  count_words = (ARGV[0].index(&#8216;w&#8217;) != nil)<br />
  count_chars = (ARGV[0].index(&#8216;c&#8217;) != nil)<br />
  filename = ARGV[1]<br />
end</p>
<p>#Count with Regexp</p>
<p>file = File.open(filename,&#8217;r+&#8217;)<br />
while (line = file.gets) do<br />
  line_count += 1;<br />
  word_count += line.scan(/[^ \n\t]+/).size<br />
  char_count += line.size<br />
end</p>
<p>puts &#8220;Line Count = #{line_count}&#8221; unless !count_lines<br />
puts &#8220;Word Count = #{word_count}&#8221; unless !count_words<br />
puts &#8220;Character Count = #{char_count}&#8221; unless !count_chars</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Cowan</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-824</link>
		<dc:creator><![CDATA[John Cowan]]></dc:creator>
		<pubDate>Wed, 09 Dec 2009 16:05:19 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-824</guid>
		<description><![CDATA[GNU wc has three major improvements on the naive algorithm:

1) It does its own block buffering rather than using stdio buffering, which means that characters are counted a block at a time.

2) It has multiple inner loops, deciding on the basis of the options which one to run (thus the inword logic is not executed at all if -w is not specified).

3) If -c is the only option, then it attempts to fstat() files rather than reading them, being careful to make sure the file is a regular file (not a device file) and taking into account the possibility that stdin may be a file that isn&#039;t positioned at its beginning.  This allows O(1) behavior in favorable circumstances.

It also extends classic wc semantics by being able to count bytes with -b and possibly-multibyte characters with -c, though if characters are known to be single-byte in the current encoding it will treat -c and -b the same (optimization #3 above really applies to -b).  It also provides -L which returns the length of the longest line and -W to return the count of words, which are obvious and useful extensions.]]></description>
		<content:encoded><![CDATA[<p>GNU wc has three major improvements on the naive algorithm:</p>
<p>1) It does its own block buffering rather than using stdio buffering, which means that characters are counted a block at a time.</p>
<p>2) It has multiple inner loops, deciding on the basis of the options which one to run (thus the inword logic is not executed at all if -w is not specified).</p>
<p>3) If -c is the only option, then it attempts to fstat() files rather than reading them, being careful to make sure the file is a regular file (not a device file) and taking into account the possibility that stdin may be a file that isn&#8217;t positioned at its beginning.  This allows O(1) behavior in favorable circumstances.</p>
<p>It also extends classic wc semantics by being able to count bytes with -b and possibly-multibyte characters with -c, though if characters are known to be single-byte in the current encoding it will treat -c and -b the same (optimization #3 above really applies to -b).  It also provides -L which returns the length of the longest line and -W to return the count of words, which are obvious and useful extensions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jean Lazarou</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-823</link>
		<dc:creator><![CDATA[Jean Lazarou]]></dc:creator>
		<pubDate>Tue, 08 Dec 2009 19:23:43 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-823</guid>
		<description><![CDATA[[sourcecode lang=&quot;ruby&quot;]
#
# returns, for the given file, an array with the number of lines, 
# words and character
#
def word_count file

  lines = 0
  chars = 0
  words = 0
 
  open(file) do &#124;f&#124;
   
    f.each do &#124;line&#124;
      lines += 1
      chars += line.length
      words += line.split.length
    end
   
  end
 
  [lines, words, chars]
 
end

# retrieve command line options
options = [&#039;w&#039;, &#039;l&#039;, &#039;c&#039;]

if ARGV[0] =~ /-([lwc])([lwc])?([lwc])?/
  options = [$1, $2, $3]
  ARGV.shift
end

unless ARGV[0]
  abort(&quot;Usage: #{$0} file1 file2 ...&quot;)
end
 
cumulate = ARGV.length &gt; 1

# process each file and output the count
total_lines = 0
total_chars = 0
total_words = 0

ARGV.each do &#124;file&#124;
 
  unless File.exist?(file)
    $stderr.puts &quot;File not found: #{file}&quot;
    next
  end
 
  lines, words, chars = word_count(file)
 
  print &quot;#{&#039;%7d&#039; % lines}\t&quot; if options.include?(&#039;l&#039;)
  print &quot;#{&#039;%7d&#039; % words}\t&quot; if options.include?(&#039;w&#039;)
  print &quot;#{&#039;%7d&#039; % chars}\t&quot; if options.include?(&#039;c&#039;)
  print &quot;\t#{file}&quot; if cumulate
  puts
         
  total_lines += lines
  total_chars += chars
  total_words += words

end

if cumulate 
  print &quot;#{&#039;%7d&#039; % total_lines}\t&quot; if options.include?(&#039;l&#039;)
  print &quot;#{&#039;%7d&#039; % total_words}\t&quot; if options.include?(&#039;w&#039;)
  print &quot;#{&#039;%7d&#039; % total_chars}\t&quot; if options.include?(&#039;c&#039;)
  puts &quot;total&quot;
end
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<pre class="brush: ruby;">
#
# returns, for the given file, an array with the number of lines, 
# words and character
#
def word_count file

  lines = 0
  chars = 0
  words = 0
 
  open(file) do |f|
   
    f.each do |line|
      lines += 1
      chars += line.length
      words += line.split.length
    end
   
  end
 
  [lines, words, chars]
 
end

# retrieve command line options
options = ['w', 'l', 'c']

if ARGV[0] =~ /-([lwc])([lwc])?([lwc])?/
  options = [$1, $2, $3]
  ARGV.shift
end

unless ARGV[0]
  abort(&quot;Usage: #{$0} file1 file2 ...&quot;)
end
 
cumulate = ARGV.length &gt; 1

# process each file and output the count
total_lines = 0
total_chars = 0
total_words = 0

ARGV.each do |file|
 
  unless File.exist?(file)
    $stderr.puts &quot;File not found: #{file}&quot;
    next
  end
 
  lines, words, chars = word_count(file)
 
  print &quot;#{'%7d' % lines}\t&quot; if options.include?('l')
  print &quot;#{'%7d' % words}\t&quot; if options.include?('w')
  print &quot;#{'%7d' % chars}\t&quot; if options.include?('c')
  print &quot;\t#{file}&quot; if cumulate
  puts
         
  total_lines += lines
  total_chars += chars
  total_words += words

end

if cumulate 
  print &quot;#{'%7d' % total_lines}\t&quot; if options.include?('l')
  print &quot;#{'%7d' % total_words}\t&quot; if options.include?('w')
  print &quot;#{'%7d' % total_chars}\t&quot; if options.include?('c')
  puts &quot;total&quot;
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-822</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 08 Dec 2009 13:39:38 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-822</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/12/08/programming-praxis-word-count/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import System.Environment
import Text.Printf

parseOpts :: [String] -&gt; ([Bool], [String])
parseOpts ((&#039;-&#039;:ps):args) = (map (`elem` ps) &quot;lwc&quot;, args)
parseOpts args            = (replicate 3 True, args)

count :: [Bool] -&gt; [(String, String)] -&gt; [String]
count opts = map (\(name, text) -&gt; concat
    [printf &quot;%8s&quot; $ if opt then show . length $ f text else &quot;-&quot;
    &#124; (f, opt) &lt;- zip [lines, words, map return] opts] ++ &quot; &quot; ++ name)

main :: IO ()
main = do args &lt;- getArgs
          let (opts, files) = parseOpts args
          mapM_ putStrLn . count opts =&lt;&lt; if null files
              then fmap (\x -&gt; [(&quot;&quot;, x)]) getContents
              else fmap (zip files) $ mapM readFile files
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/12/08/programming-praxis-word-count/" rel="nofollow">http://bonsaicode.wordpress.com/2009/12/08/programming-praxis-word-count/</a> for a version with comments):</p>
<pre class="brush: css;">
import System.Environment
import Text.Printf

parseOpts :: [String] -&gt; ([Bool], [String])
parseOpts (('-':ps):args) = (map (`elem` ps) &quot;lwc&quot;, args)
parseOpts args            = (replicate 3 True, args)

count :: [Bool] -&gt; [(String, String)] -&gt; [String]
count opts = map (\(name, text) -&gt; concat
    [printf &quot;%8s&quot; $ if opt then show . length $ f text else &quot;-&quot;
    | (f, opt) &lt;- zip [lines, words, map return] opts] ++ &quot; &quot; ++ name)

main :: IO ()
main = do args &lt;- getArgs
          let (opts, files) = parseOpts args
          mapM_ putStrLn . count opts =&lt;&lt; if null files
              then fmap (\x -&gt; [(&quot;&quot;, x)]) getContents
              else fmap (zip files) $ mapM readFile files
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Word Count &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/12/08/word-count/#comment-821</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Word Count &#171; Bonsai Code]]></dc:creator>
		<pubDate>Tue, 08 Dec 2009 13:39:27 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1758#comment-821</guid>
		<description><![CDATA[[...] Praxis &#8211; Word&#160;Count By Remco Niemeijer  In today&#8217;s Programming Praxis exercise, we have to implement the Unix wc command line utility. Let&#8217;s get [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Word&nbsp;Count By Remco Niemeijer  In today&#8217;s Programming Praxis exercise, we have to implement the Unix wc command line utility. Let&#8217;s get [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

