in reply to substr question
You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue. If you assign something shorter than LENGTH, the string will shrink, and if you assign something longer than LENGTH, the string will grow to accommodate it.
Essentially when using substr as an lvalue it remembers the offset and length into which the rvalue will be substituted. As you surmised, the length is updated to the length of the substitution in each case. This is natural and means that each successive substitution will fully replace the previous one.
And this is what we see in each of the cases you give in your example.:
$x = '1234'; for (substr($x,1,2)) { # lvalue offset and length start at (1 +,2) $_ = 'a'; print $x,"\n"; # becomes (1,1) -- prints 1a4, $_ = 'xyz'; print $x,"\n"; # becomes (1,3) -- prints 1xyz4 $x = '56789'; # no change (1,3) $_ = 'pq'; print $x,"\n"; # becomes (1,2) after pq is subbed int +o (1,3) -- prints 5pq9 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: substr question
by johngg (Canon) on Aug 04, 2013 at 13:05 UTC | |
by lightoverhead (Pilgrim) on Aug 05, 2013 at 23:07 UTC | |
|
Re^2: substr question
by lightoverhead (Pilgrim) on Aug 05, 2013 at 23:05 UTC |