Example:
aList = ["one","two"]
add(aList,"three")
see aList
Example:
aList = 1:10 # create list contains numbers from 1 to 10
aList + 11 # add number 11 to the list
see aList # print the list
Example:
aList = 1:20 see len(aList) # print 20
To delete an item from the list, we can use the del() function
Syntax:
del(list,index)
Example:
aList = ["one","two","other","three"]
Del(aList,3) # delete item number three
see aList # print one two three
Example:
aList = ["Cairo","Riyadh"]
see "Egypt : " + aList[1] + nl +
"KSA : " + aList[2] + nl
To set the value of an item inside the list, we can use the next syntax
List[Index] = Expression
Example:
aList = list(3) # create list contains three items
aList[1] = "one" aList[2] = "two" aList[3] = "three"
see aList
21.7 Search
To find an item inside the list we can use the find() function
Syntax:
Find(List,ItemValue) ---> Item Index
Find(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index
Find(List,ItemValue,nColumn,cAttribute) ---> Item Index
Example:
aList = ["one","two","three","four","five"]
see find(aList,"three") # print 3
Example:
mylist = [["one",1],
["two",2],
["three",3]]
Example:
aList = ["one","two","three","four","five"]
aList = sort(aList)
see binarysearch(aList,"three")
Output:
five
four
one
three
two
4
21.8 Sort
Example:
aList = [10,12,3,5,31,15]
aList = sort(aList) see aList # print 3 5 10 12 15 31
aList2 = sort(aList,1)
see aList2
Output:
ahmed
14000
ibrahim
11000
mahmoud
15000
mohammed
12000
samir
16000
21.9 Reverse
21.8. Sort 92
Ring Documentation, Release 1.2
Example:
aList = [10,20,30,40,50]
aList = reverse(aList)
see aList # print 50 40 30 20 10
We can copy lists (including nested lists) using the Assignment operator.
Example:
aList = [
"one","two",
[3,4],
[20,30], ["three",
"four",
"five",[100,200,300]
]
]
Lists are first-class citizens where we can store lists in variables, pass lists to functions, and return lists from functions.
Example:
aList = duplicate( [1,2,3,4,5] )
see aList[10] + nl # print 5
We can use the list items while we are defining the list for the first time.
Example:
aList = [ [1,2,3,4,5] , aList[1] , aList[1] ]
see aList # print 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Lists are passed to functions by reference, This means that the called function will work on the same list and can
modify it.
Example:
func main
aList = [1,2,3,4,5] # create list, local in function main
Instead of using numbers to determine the item index when we get item value or set item value, We can access items
using string index if the item is a list contains two items and the first item is a string.
Example:
aList = [ ["one",1] , ["two",2] , ["three",3] ]
see aList["one"] + nl +
aList["two"] + nl +
aList["three"] # print 1 2 3
This type of lists can be defined in a better syntax using the : and = operators.
Example:
aList = [ :one = 1 , :two = 2 , :three = 3 ]
see aList["one"] + nl +
aList["two"] + nl +
aList["three"] + nl # print 1 2 3
see aList[1] # print one 1
Note: using = inside list definition create a list of two items where the first item is the left side and the second item is
the right side.
We can add new items to the list using the string index
Example:
aList = []
aList["Egypt"] = "Cairo"
aList["KSA"] = "Riyadh"
see aList["Egypt"] + nl + # print Cairo
aList["KSA"] + nl # print Riyadh
This type of lists is very good for passing parameters to functions Where the order of parameters will not be important
(we can change the order).
Also some parameters maybe optional.
Example:
TWENTYTWO
STRINGS
In this chapter we are going to learn about strings creation and manipulation.
Syntax:
cStr = "This is a string"
cStr2 = 'Another string'
cStr3 = :JustAnotherString
cStr4 = `Yet "another" 'string' ! `
We can get the string length (letters count inside a string) using the len() function
Syntax:
len(string) ---> string length
Example:
cStr = "How are you?"
see cStr + nl
see "String size : " + len(cStr) + nl
Syntax:
lower(string) ---> convert string letters to lower case
upper(string) ---> convert string letters to UPPER case
Example:
cStr = "Welcome To The Ring Programming Language"
see cStr + nl + upper(cStr) + nl + lower(cStr)
97
Ring Documentation, Release 1.2
Example:
# print user name letter by letter (each letter in new line)
We can get a specified number of characters from a string using the Left() function.
The starting position is 1.
Syntax:
Left(string,count)
Example:
see left("Hello World!",5) # print Hello
We can get a specified number of characters from a string using the Right() function.
Example:
see Right("Hello World!",6) # print World!
We can remove all leading and trailing spaces from a string using the Trim() function.
Syntax:
trim(string)
Example:
cMsg = " Welcome "
see trim(cMsg) # print Welcome
We can duplicate a string more than one time using the copy() function.
Syntax:
copy(string,nCount) ---> string replicated nCount times
Example
see copy("***hello***",3) # print ***hello******hello******hello***
We can count the number of lines inside a string using the Lines() function.
Syntax:
lines(string) ---> Number of lines inside the string
Example:
cStr = "Hello
How are you?
are you fine?"
see lines(cStr) # print 3