("0" . submatch(1))[-2:]
Hmmm...a nifty new feature in vim7 that is here on my work machine, but
unavailable on my hosting service (still running 6.3). Looks like a
much-needed pilfering from Python's handy slicing syntax. :)
well, then,
strpart("0" . submatch(1), strlen(submatch(1)) - 2)
Um...using "s" for the submatch in the example:
let s='19'
echo s.'->'.strpart('0'.s, strlen(s)-2)
The "echo" yields
19->019
I think you meant the following:
echo s.'->'.strpart('0'.s, strlen('0'.s)-2)
which yields the correct results for 1 and 2-digit values of s:
19->19
3->03
but still chokes on crazy 3-character values of s:
123->123
instead of
123->23
Who would have thought the implementation of a right() would be
such a complex problem :)
Using subsitute(), I've shortened it by a couple characters:
echo s.'->'.substitute('0'.s, '.*\ze..$', '', '')
Thus, making a right() function look something like
function! Right(s, length)
let l:length = a:length
let l:pad = ''
while l:length
let l:pad = l:pad.'0'
let l:length -= 1
endwhile
return substitute(l:pad.a:s, '^.*\ze.\{'.a:length.'}$', '', '')
endfunc
for pre-vim7 compatibility, all of which collapses nicely into
vim7-ese as
function! Right(s, length)
return (repeat('0', a:length).a:s)[(-a:length):]
endf
I guess a true Right() function wouldn't zero-pad. But I guess
then I should be referring to the desired effect in question as
LeftPadWithZerosAndForceToAFixedWidth but that's a bit cumbersome. ;)
-tim