Useful funtion to remove the last character of a string.

Started by Andrew on 27-Apr-2017/20:10:07-7:00
I couldn't figure out how to remove the last character of a string, so my father wrote this cool function. trim-last: func ['Removes last value' thing [string! block!] {Block or String to remove last value}][ thing: tail thing thing: back thing remove thing thing: head thing ]
Take/last series//Does this for you.
Another way which doesn't need a function, and a variable to hold the intermediate result: >> series: copy [ 1 2 3 4 ] == [1 2 3 4] >> head remove back tail series == [1 2 3]
If you do it this way, you could choose any index position to remove ('length string' allows you to choose the last character): string: "asdfghjkl" remove at string length? string probe string
This is such a great example of my historical difficulty with REBOL, which I finally seem to be outgrowing. Andrew's function and Graham's function do the same thing with the same functions in the same order. Andrew's way is the way I would tend to write it, locked as I am into my third-generation way of thinking. But Graham's version uses the REBOL feature of passing data from one function to another which allows code to be a bit more dense and is not the way my instincts direct me. I have thought about writing a little monograph, for personal use, with a title along the lines of "Good Code or Bad?" where I collect examples of one way of coding something and contrast it with the "REBOL way." I suspect that if I would go back through all I have written I would find quite a few examples.
I prefer the concise form, but I get the sense that I might be in the minority.
Steven—I had a pass at explaining this as my first entry in SO Documentation: https://stackoverflow.com/documentation/rebol/9176/series
If you want economy and beauty in your script you need to factor ruthlessly. Remove the chaff to expose the bare essentials of what you want to achieve.

Reply

Replies are rate limited to reduce abuse.