On Wed, Mar 12, 2003 at 03:09:36PM -0800, R. Joseph Newton wrote: > Mark Anderson wrote: > > > > It sounds like you need a count hash. You might try something like: > > > > > > my @tokens = split /:/; > > > foreach (@tokens) { > > > if ($tokenCount{$_}) { > > > $tokenCount{$_}++; > > > } else { > > > $tokenCount{$_} = 1; > > > } > > > } > > > > The following works the same and may or may not be easier to > > understand/maintain. > > $tokenCount{$_} is automatically created with a value of 0 when first > > called, and > > then is incremented to 1, so you don't have to test or create it yourself. > > > > my @tokens = split /:/; > > for (@tokens) { > > $tokenCount{$_}++ > > } # for > > Wow! It works. I'm not sure that's such a good thing, though. How > does one ditinguish, then, between 0 as a meaningful value, and a > still-undefined variable?
And I'd code that as: $tokenCount{$_}++ for split /:/; Well, actually, as: $token_count{$_}++ for split /:/; Check for definedness with C<defined $token_count{$token}> or existence with C<exists $token_count{$token}> > I think, for the sake of healthy discipline, at least, that I would > stick with explicitly assigning a numerical value to my variables > before executing any other operations on them. Implicit casts from > undef? Boolean--aye, any other type, Nay! This is actually a common Perl idiom, and very useful in situations such as this. Of course, anyone is free to program in any way they please, but for me, the longer version is slower to write, slower to read and understand and slower to run. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]