Paul Johnson wrote: > 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.
Hi Mark, Are you a beginner at Perl, as is the original poster? Somehow I doubt it. If you can show me some benchmarks on the running times that show any serious loss in performance, I would gladly take that into consideration. I would certainly grant also, that a seasoned Perl programmer might understand the cryptic syntaces proposed at a glance. I find that highly unlikely in the case of anyone new to the arcane Perl argot. In retrospect, I could have been more clear by using if (defined ($tokenCount{$_}) { since this would more clearly communicate that keys already encountered have their count incremented, whie any previously unseen key is being created and initialized to 1 in one fell swoop. Since the incrementation think it would be counter-productive to further obscure the process by trying to crunch it down to some obscure, context-deprived [in spite of the ready availability of the working context] one-liner. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]