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

Re: [MacPerl] Counting Words



Try;

local @words = split(" ", $a) ;

return $#words + 1 ;

$#arrayName returns the last index number of the array, adding 1 to it
gives you the number of elements in the array (assuming 0 based indexing).

Dick Munroe

jacobs@azstarnet.com wrote:

> 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
begin:vcard 
adr;dom:;;267 Cox St.;Hudson;Ma;01749;
n:Munroe;Dick
x-mozilla-html:FALSE
org:Acorn Software, Inc.
version:2.1
email;internet:munroe@acornsw.com
title:Chief Technology Officer
tel;fax:978 562 1133
tel;work:888 226 7679
x-mozilla-cpt:;0
fn:Munroe, Dick
end:vcard