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

Re: [MacPerl] Counting Words



On Thu, May 20, 1999 at 11:34:11PM -0500, Michael D. Kirkpatrick wrote:
> I am trying to get a word count from a string.
> 
> $a="This is some sample text."
> 
> I have tried this and it does not work:
> $returncount=0;
>  @words=split(/\ /,$a);
>  foreach $trash(sort keys(@words)){
>   $returncount++;
>  }
> 
> I am getting an odd number of elements in hash array error.
> 
> Any help would be greatly appreciated.  Thanks in advance.
> 

@words is not a hash.  What are you trying to do?

If you want to know how many words in a string:

@words = split ' ', $a;
$returncount = @words;

An array in a scalar context is the number of elements.


You need to go read the perldata ('Data structures') documentation.


Ronald

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