in reply to Re: How do I cut single characters out of a string
in thread How do I cut single characters out of a string
Way back in the early days of Perl 5 substr didn't have the facility to supply a fourth argument so assigning to it as you describe was the only way to go. The advantage of using the 4-arg form to replace text is that what has been replaced is returned. Thus you can chain substrs together to move text around.
knoppix@Microknoppix:~$ perl -E ' > $str = q{abcdefghijk}; > say $str; > substr $str, 5, 2, substr $str, 3, 2, substr $str, 5, 2; > say $str;' abcdefghijk abcfgdehijk knoppix@Microknoppix:~$ perl -E '
Of course, that particular example can be done more simply with a regex substitution but it can be a useful technique sometimes.
knoppix@Microknoppix:~$ perl -E ' > $str = q{abcdefghijk}; > say $str; > $str =~ s{^...\K(..)(..)}{$2$1}; > say $str;' abcdefghijk abcfgdehijk knoppix@Microknoppix:~$
I hope this is of interest.
Cheers,
JohnGG
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: How do I cut single characters out of a string
by tobyink (Canon) on Jan 15, 2012 at 12:53 UTC |