in reply to Can you write a faster code to perform this task?

Not the fastest but nice: scalar map { /(M+)/g } ($str);

Compared to the anon monk's benchmark below:

count 3 map 3 regex 3 split 3 Rate split regex map count split 207677/s -- -7% -28% -47% regex 223050/s 7% -- -23% -43% map 288533/s 39% 29% -- -27% count 394162/s 90% 77% 37% --

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»