Quite smart solution.
but it costs 0.335752 seconds for 100000 loops. In contrast, Perl costs only 0.015 seconds.