cleaned up version of erik quanstrom's suggestion. allows $x(1-5) $x(3-) as subscript notation. this is fantastic, because it means i can start writing x=$x(2-) and retire my old idiom *=($x); shift; x=($*) ☺ Reference: /n/sources/patch/applied/rc-subseq Date: Sun Jul 20 10:14:03 CES 2008 Signed-off-by: rsc@swtch.com --- /sys/man/1/rc Sun Jul 20 10:13:10 2008 +++ /sys/man/1/rc Sun Jul 20 10:13:09 2008 @@ -213,6 +213,11 @@ is followed by a parenthesized list of subscripts, the value substituted is a list composed of the requested elements (origin 1). The parenthesis must follow the variable name with no spaces. +Subscripts can also take the form +.IB m - n +or +.IB m - +to indicate a sequence of elements. Assignments to variables are described below. .HP .BI $# argument --- /sys/src/cmd/rc/exec.c Sun Jul 20 10:13:11 2008 +++ /sys/src/cmd/rc/exec.c Sun Jul 20 10:13:10 2008 @@ -679,21 +679,51 @@ } word* +copynwords(word *a, word *tail, int n) +{ + word *v, **end; + + v = 0; + end = &v; + while(n-- > 0){ + *end = newword(a->word, 0); + end = &(*end)->next; + a = a->next; + } + *end = tail; + return v; +} + +word* subwords(word *val, int len, word *sub, word *a) { - int n; + int n, m; char *s; if(!sub) return a; a = subwords(val, len, sub->next, a); s = sub->word; deglob(s); + m = 0; n = 0; - while('0'<=*s && *s<='9') n = n*10+ *s++ -'0'; - if(n<1 || lenlen || m<0) return a; - for(;n!=1;--n) val = val->next; - return newword(val->word, a); + if(n+m>len) + m = len-n; + while(--n > 0) + val = val->next; + return copynwords(val, a, m+1); } void