If you need to play with long strings char-by-char, spliting and joining them can consume large amounts of memory, and making every reference to each char of the form substr( $s, $p, 1 ) = 'X' if substr( $s, $p, 1 ) ne ' '; can be a PITA.
You can avoid both using lvalue substr refs.
my $s= 'the quick brown fox jumps over the lazy dog'; $$_ ne ' ' and $$_ = 'X' for map{ \substr( $s,$_, 1 ) } 0 .. length $s; print $s; XXX XXXXX XXXXX XXX XXXXX XXXX XXX XXXX XXXX
I'm still hoping that LW will see fit to include $s[ $p ] syntax for accessing the chars of a string in P6 once that syntax is no longer valid for elements of an array -- or at least that it will be possible to create a module that gives this syntax efficiently.
In reply to Re: foreach (each character in string..)?
by BrowserUk
in thread foreach (each character in string..)?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |