I am not sure what your first template is trying to do. It appears to be trying to extract the first character of the series, then the first 2 characters, then the third and fourth characters, then other stuff.
If the above is correct then this general progrm mode template does the job.
Code:
program:
if $series then
template('series/{series:substr(0,1)}/{series:substr(0,2)}/{series:substr(2,4)}/{series_index:0>3s} - {title}/{author_sort:sublist(0,1,&)} - {series} - {series_index:0>3s} - {title}')
else
template('single books/{author_sort[0]}/{author_sort:sublist(0,1,&)}/{title}/{author_sort} - {title}')
fi