Difference between Section() and SectionXT() in SoftanzaLib

38 views
Skip to first unread message

Mansour Ayouni

unread,
Feb 2, 2024, 10:42:33 AMFeb 2
to The Ring Programming Language
Hello All,

In Softanza, you can get a part of a list (or string) using Section() function, also called Slice() :

o1 = new stzString("123456789")

? o1.Section(3, 5)
#--> "345"

When you inverse the params so the first is greater then the second, nothing happens to the result ( the Section() function is not aware of the direction of parsing ) :

? o1.Section(5, 3)
#--> "345"

You may argue that it would be useful, in this case, to embrace the Python-way of returning an inverted string (or list)...

Softanza does not reject that, and finds it very useful too! But, it just requires that you use the extended form of the function, SectionXT() :

? o1.SectionXT(5,3)
#--> "543"

As you see, the section has been reversed. But you can do more, and use negative numbers to order Softanza to start parsing from the end:

? o1.SectionXT(-4, -2)
#--> 678

? o1.SectionXT(-2, -4)
#--> 876

Rember : if you try these fancy things with the more conservative Section() methond (without ...XT() extension), and for Softanza to stay simple and consistent for the most common use cases, you will get an error:

? o1.Section(-2, -4)
#--> Error message: n1 and n2 must be inside the list.

Before you leave : In all Softanza, all what works for stzString, should work the same for stzList. For our case, just change the first line of the code to use stzList instead of stzString, like this :

o1 = new stzList("1":"9")

Now you can run the code successfully without any modification, and get the same results (although in a different type, a list of chars instead of a substring.)

A snapshot of this narration running on Notepad :

image.png

All the best,
Mansour

Mahmoud Fayed

unread,
Feb 3, 2024, 7:47:41 AMFeb 3
to The Ring Programming Language
Hello Mansour

So, unlike SubStr() function which uses characters count, The Section() method will get a string from position to position.
This could be useful when the parameter (second position) is the output of another function call the get a position.

Suggestion:
It's better to demonstrate such features in the future using strings that contains (words) instead of numbers.

Greetings,
Mahmoud

Mansour Ayouni

unread,
Feb 3, 2024, 12:49:56 PMFeb 3
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud,

Thanks for your message.

So, unlike SubStr() function which uses characters count

In fact, Softanza has the Range function just for this. Example :
? Q("Ringorialand").Range(1, 4) #--> "Ring"

Which is also possible using the eXTended version of Section() like this:
? Q(" Ringorialand  ").SectionXT(1, :UpToNChars = 4) #--> "Ring"

You can see the use of those two functions in the bottom of the snapshot hereafter.

This could be useful when the parameter (second position) is the output of another function

You are right! Take a look at these examples:
image.png

It's better to demonstrate such features in the future using strings that contains (words) instead of numbers

Yes, of course. There are plenty of examples showing it with strings. I just wanted the reader to see the positions themselves on the result.

All the best,
Mansour


--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/8408f85d-084e-4bf6-ae1c-84d383015ed8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages