[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Counting Words



I'm sure you'll get a lot of these, but try this:

#! /usr/bin/perl -w

$a = "this is some sample text";

$returnCount = 0;
#split into array
@words = split( / / , $a );
#loop over array
for ( my $i = 0 ; $i < @words ; $i++ ) {
  $returnCount++;
}
#feedback
print "words: $returnCount\n";

--
you're splitting into an array, and then trying to count the results as
if they were a hash. 


> I am getting an odd number of elements in hash array error.

(i think the error is because there are 5 words in the string. exercise
for the reader: what happens if there are six?)

john.
-- 
----------------------------------------------------------------
John S Jacobs Anderson                      jacobs@azstarnet.com
www.treefort.org/~jacobs/ <--   GeneHack (bioinfo*linux*opinion)
I lost a button hole today.


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org