slice negative counter

Started by hs, July 25, 2007, 04:17:48 AM

Previous topic - Next topic

hs

Hi all!



it's probably been discussed before but i haven't searched enough (i got the result on negative offset for slice)



is there any easy way to get negative counter act as position?

inclusive start and end

like:

(set 'a '(1 2 3 4 5 6 7))



(2 -2 a) ;want (3 4 5 6), newlisp gives (3 4 5 6 7)



in code:

(define (my-slice l s c)

   (if (>= c 0)

      (slice l s c)

      (slice l s (+ (length l) c -1))))



a; (1 2 3 4 5 6 7)

(my-slice a 2 -2) ;gives (3 4 5 6)

(my-slice a 2 0); gives ()

(my-slice a 2 2); gives (3 4)



it would be nice to have it in implicit slicing too like (2 -2 a)

thx!

cormullion

#1
looks like you can now do this!


Quote9.1.11 release notes

    negative second length in slicing now interpreted as offset

    from the right: (2 3 "abcdefg") => "cde", (2 -3 "abcdefg") => "cd"

    before negative length would be interpreted as going to the end

hs

#2
Quote from: "cormullion"looks like you can now do this!


Quote9.1.11 release notes

    negative second length in slicing now interpreted as offset

    from the right: (2 3 "abcdefg") => "cde", (2 -3 "abcdefg") => "cd"

    before negative length would be interpreted as going to the end


it's inclusive in left endpoint and non-inclusive at the right endpoint, similar to python



>>> a="abcdefg"

>>> a[2:-3]

'cd'

>>> a[2:]

'cdefg'

>>> a[2:0]

''



thanks Lutz!

Lutz

#3
nobody confuse (a 2 0) -> "c" and (2 0 a) -> "", in newLISP the indices are before the string/list when slicing.



Lutz

frontera000

#4
> (slice '(a b c d e f) 2 -1)

(c d e)

> (slice '(a b c d e f) 2 )

(c d e f)



Second one seems to be correct.



Both should give same results, no?

Lutz

#5
Since v 9.1.11, both are correct now. Now a negative length spec in slice is taken as offset from the end. The manual in 9.1.11 hasn't been updated yet and still shows the old way of treating a negative parameter.



- glad to see you on this board again ;-)



Lutz

frontera000

#6
Thanks. I was trapped in Java hell for a year. :)