Op 9/08/2024 om 13:00 schreef Jan Bredenbeek via Ql-Users:
On 09-08-2024 07:52, Wolfgang Lenerz via Ql-Users wrote:
without having looked at the string slicing code in SMSQE, I believe
that the general contract is a$(x to y) where x and y are positive
integers and y is bigger than, or equal to, x. If x is omitted, it is
implied that it should be 1. If y is omitted, it is implied that it
should be the size of the string (this explains j$ and k$).
So the examples where x is bigger than y will either return nothing
(when x=y-1) or an error - as these are non-sensical in regard of the
above contract, that doesn't bother me. This is why the line i$=a$(L
to L-2) fails: x is bigger than y.
This is true even for the JS ROM, except that omitting x causes an
error because JS assumes it's zero by default...
By exception, a$(0) returns the length of a$, so in the line
d$=a$(0), d$ isn't 1, but 10. This is also why your line e$=a$(0 to
1) fails.
Both Minerva and SMSQ/E return the length of a string(0), JS returns
'out of range'.
Notes:
1 - If y is bigger than the size of the string, no error is
generated, and it will be limited to the length of the string: Print
a$(1 to 100) will just print the entire string.
Even true for JS.
But what if x is greater than LEN(a$)?
If LEN(a$)=10, then
- Both JS and SMSQ/E return 'array index out of range' with a$(11),
but SMSQ/E returns empty string with a$(11 to 12)!
(I believe older versions of SMSQ/E accepted a$(11) but Marcel changed
that some years ago).
- Minerva returns empty string with a$(11) (but does not allow
a$(12)). This is documented in the Minerva manual.
2 - Fun can be had with negative numbers - Norman Dunbar drew my
attention to that.
One useful thing would be to allow negative numbers for x, meaning the
'x'th element from the *end* of the string. E.g. "abcdefgh"(-3 TO)
would return "fgh". This avoids awkward constructs like a$(LEN(a$)-x+1
TO), you would just write a$(-x TO).
Of course this would mean that y (if present) must be negative too,
with a default of -1 rather than LEN(a$)...
Best Regards,
Thanks for your additional info.
François Van Emelen
_______________________________________________
QL-Users Mailing List