perl6: count chinese characters in string


Hi,

This is my first post here and it will be a short one.

I was asked to count chinese characters in a string. Most of us know the annoying task of writing an essay with $n words. For chinese it will be $n characters.

Would be a shame if we take this too serious and write too much.

so:

my Str $text = "你好! 我喜欢!";

$text ~~ s:g/ \W | \d //;

say $text.chars;


And that's it.

So why even blog about it? What I think is amazing is that all worked with the first attempt.
I often just try stuff in perl6 without knowing if it will work the way I think it will work.

However most of the time, it just does... and this is why I love this language!

Keep up the great work!!

Ben

2 Comments

你好,我爱你编的这个程: )
Thank you for your help!

Cai

I would appreciate it if you could let us know the unicode code points for the Chinese characters.

Thanks.

-Tom

Leave a comment

About spebern

user-pic I blog about Perl.