Re: Counting characters
On Wed, Jan 27, 2010 at 08:40:48AM -0800, Larry Wall wrote: On Wed, Jan 27, 2010 at 01:24:48PM +, Matthew Walton wrote: : On Wed, Jan 27, 2010 at 1:19 PM, Carl Mäsak cma...@gmail.com wrote: : Mark (), Carl (): : S05 describes tr/// in terms of the .trans function, a handsome but : very different beast. Specifically, it doesn't seem to have a scalar : context, with which one could count things. : : What does trans return in numeric (+) context? : : As spec'd, it returns the numification of the string resulting from : the substitution, I guess. : : // Carl : : : $str.comb(/C|G/).join('').chars might do it. It's maybe not quite as elegant... Hmm, what might be more elegant? Maybe something like... [+] $str.comb.BagC G; Probably does too much work building the Bag though, unless it can be lazy somehow. But the point is that Bags are really just histograms with a cute name. That or the optimiser is capable of recognising the entire construction, and compiling it down to something efficient. For example, how @a = sort @a; foreach (reverse @a) { ... } ... reverse sort { ... } ... if (%hash) { ... } now work in Perl 5, by being optimised to a more efficient execution sequence. Nicholas Clark
Re: Counting characters
Would you want to use something else for that, maybe .comb? From the spec: 'The comb function looks through a string for the interesting bits, ignoring the parts that don't match. In other words, it's a version of split where you specify what you want, not what you don't want.' chris On Jan 27, 2010, at 7:08 AM, Mark J. Reed wrote: What does trans return in numeric (+) context? On Wednesday, January 27, 2010, Carl Mäsak cma...@gmail.com wrote: How is character counting done in Perl 6? In Perl 5, it is `scalar tr/CG//` if I want to count the number of Cs plus the number of Gs in a string. S05 describes tr/// in terms of the .trans function, a handsome but very different beast. Specifically, it doesn't seem to have a scalar context, with which one could count things. -- Mark J. Reed markjr...@gmail.com
Re: Counting characters
What does trans return in numeric (+) context? On Wednesday, January 27, 2010, Carl Mäsak cma...@gmail.com wrote: How is character counting done in Perl 6? In Perl 5, it is `scalar tr/CG//` if I want to count the number of Cs plus the number of Gs in a string. S05 describes tr/// in terms of the .trans function, a handsome but very different beast. Specifically, it doesn't seem to have a scalar context, with which one could count things. -- Mark J. Reed markjr...@gmail.com
Re: Counting characters
Mark (), Carl (): S05 describes tr/// in terms of the .trans function, a handsome but very different beast. Specifically, it doesn't seem to have a scalar context, with which one could count things. What does trans return in numeric (+) context? As spec'd, it returns the numification of the string resulting from the substitution, I guess. // Carl
Re: Counting characters
On Wed, Jan 27, 2010 at 1:19 PM, Carl Mäsak cma...@gmail.com wrote: Mark (), Carl (): S05 describes tr/// in terms of the .trans function, a handsome but very different beast. Specifically, it doesn't seem to have a scalar context, with which one could count things. What does trans return in numeric (+) context? As spec'd, it returns the numification of the string resulting from the substitution, I guess. // Carl $str.comb(/C|G/).join('').chars might do it. It's maybe not quite as elegant... Matt
Re: Counting characters
On Wed, Jan 27, 2010 at 01:24:48PM +, Matthew Walton wrote: : On Wed, Jan 27, 2010 at 1:19 PM, Carl Mäsak cma...@gmail.com wrote: : Mark (), Carl (): : S05 describes tr/// in terms of the .trans function, a handsome but : very different beast. Specifically, it doesn't seem to have a scalar : context, with which one could count things. : : What does trans return in numeric (+) context? : : As spec'd, it returns the numification of the string resulting from : the substitution, I guess. : : // Carl : : : $str.comb(/C|G/).join('').chars might do it. It's maybe not quite as elegant... Hmm, what might be more elegant? Maybe something like... [+] $str.comb.BagC G; Probably does too much work building the Bag though, unless it can be lazy somehow. But the point is that Bags are really just histograms with a cute name. Larry