Lists¶
In this chapter we are going to learn how to deal with lists.
Create Lists¶
We can create new lists by defining the list items inside square brackets.
Example:
aList = [1,2,3,4,5]
Also we can create new lists using the : operator
Example:
aList = 1:5
aList2 = "a":"z"
Example:
aList = 5:1
aList2 = "z":"a"
Also we can create lists using the list() function
Syntax:
list = list(size)
To create 2D list
list = list(nRows,nCols)
Example (1)
aList = list(10) # aList contains 10 items
Example (2)
aList = list(5,4) # Create 2D List contains 5 rows and 4 columns
Note
the list index start from 1
Add Items¶
To add new items to the list, we can use the Add() function.
Syntax:
Add(List,Item)
Example:
aList = ["one","two"]
add(aList,"three")
showln aList
Also we can do that using the + operator.
Syntax:
List + item
Example:
aList = 1:10 # create list contains numbers from 1 to 10
aList + 11 # add number 11 to the list
showln aList # print the list
Get List Size¶
We can get the list size using the len() function
Syntax:
Len(List)
Example:
aList = 1:20 showln len(aList) # print 20
Delete Item From List¶
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
showln aList # print one two three
Get List Item¶
To get an item from the list, we uses the next syntax
List[Index]
Example:
aList = ["Jakarta","Delhi"]
show "Indonesia : " + aList[1] + newline +
"India : " + aList[2] + newline
Set List Item¶
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"
showln aList
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"]
showln find(aList,"three") # print 3
Example:
mylist = [["one",1],
["two",2],
["three",3]]
showln find(mylist,"two",1) # print 2
showln find(mylist,2,2) # print 2
Also we can use the binarysearch() function to search in sorted list.
Syntax:
BinarySearch(List,ItemValue) ---> Item Index
BinarySearch(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index
Example:
aList = ["one","two","three","four","five"]
aList = sort(aList)
showln binarysearch(aList,"three")
Output:
five
four
one
three
two
4
Sort¶
We can sort the list using the sort() function.
Syntax:
Sort(List) ---> Sorted List
Sort(List,nColumn) ---> Sorted List based on nColumn
Sort(List,nColumn,cAttribute) ---> Sorted List based on Object Attribute
Example:
aList = [10,12,3,5,31,15]
aList = sort(aList) showln aList # print 3 5 10 12 15 31
We can sort list of strings
Example:
mylist = ["Banana","Apple","Dog","Cat","Zebra"]
showln mylist # print list before sorting
mylist = sort(mylist) # sort list
showln "list after sort"
showln mylist # print Apple Banana Cat Dog Zebra
We can sort a list based on a specific column.
Example:
aList = [ ["Cat",15000] ,
["Apple", 14000 ] ,
["Zebra", 16000 ] ,
["Dog", 12000 ] ,
["Banana",11000 ] ]
aList2 = sort(aList,1)
showln aList2
Output:
Apple
14000
Banana
11000
Cat
15000
Dog
12000
Zebra
16000
Reverse¶
We can reverse a list using the reverse() function.
Syntax:
Reverse(List) ---> Reversed List
Example:
aList = [10,20,30,40,50]
aList = reverse(aList)
showln aList # print 50 40 30 20 10
Insert Items¶
To insert an item in the list we can use the insert() function.
Syntax:
Insert(List,Index,Item)
The inserted item will be AFTER the Index
Example:
aList = ["A","B","D","E"]
insert(aList,2,"C") # Inserts AFTER Index 2, "C" into Position 3
showln aList # print A B C D E
Nested Lists¶
The list may contain other lists
Example:
aList = [ 1 , [10,20,30] , 5 , [100,1000,5000] ]
aList2 = [
"one","two",
[3,4],
[20,30], ["three",
"four",
"five",[100,200,300]
]
]
showln aList[2] # print 10 20 30
showln aList[4][3] # print 5000
showln aList2[5][2] # print four
showln aList2[5][4][3] # print 300
Copy Lists¶
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]
]
]
aList2 = aList # Copy aList to aList2
aList2[5] = "other" # modify item number five
showln aList2[5] # print other
showln aList[5] # print three four five 100 200 300
First-class lists¶
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] )
showln aList[10] # print 5
showln mylist() # print 10 20 30 40 50
func duplicate(list){
nMax = len(list)
for x = 1 to nMax
list + list[x]
end
return list
}
func mylist() return [10,20,30,40,50] end
Using Lists during definition¶
We can use the list and the list items while we are defining the list for the first time.
Example:
aList = [ [1,2,3,4,5] , aList[1] , aList[1] ]
showln aList # print 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Example:
x = [ 1, 2, x ]
showln x # print 1 2 1 2
showln len(x) # print 3
showln x[1] # print 1
showln x[2] # print 2
showln x[3] # print 1 2
Output:
1
2
1
2
3
1
2
1
2
Passing Lists to Functions¶
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
myfunc(aList) # call function, pass list by reference
showln aList # print 1 2 3 4 5 6 7 8 9 10
end
func myfunc(list)
list + [6,7,8,9,10]
end
Access List Items by String Index¶
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] ]
show aList["one"] + newline +
aList["two"] + newline +
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 ]
show aList["one"] + newline +
aList["two"] + newline +
aList["three"] + newline # print 1 2 3
showln aList[1] # print one 1
Tip
using : before identifier (one word) means literal
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["Indonesia"] = "Jakarta"
aList["India"] = "Delhi"
show aList["Indonesia"] + newline + # print Jakarta
aList["India"] + newline # print Delhi
Passing Parameters or Arguments Using List¶
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:
myconnect ( [ :server = "myserver.com" , :port = 80 ,
:username = "glaurung" , :password = "password" ] )
func myconnect(mypara)
# print connection details
show "User Name : " + mypara[:username] + newline +
"Password : " + mypara[:password] + newline +
"Server : " + mypara[:server] + newline +
"Port : " + mypara[:port]
end
Passing Parameters or Arguments Using List Array¶
Passing Arguments or Parameters to a Function in an array format
Example:
myList = [5,7,3,9] ### list with args or parms in an array
result = sum(myList)
showln "Sum result: "+ result
func sum(aList)
acc = 0
sizeList = len(aList)
for i = 1 to sizeList
showln aList[i]
acc = acc + aList[i]
end
return acc
end
Return Parameters as List or Hash Table¶
Return Parameters from a Function in an Array or Hash Format
Example:
sudoku = [ [2,9,0],
[0,0,1],
[0,0,0] ]
aOutput = myFunctionArray(sudoku)
showln "Return Array: T/F: "+ aOutput[1] +" Row: "+ aOutput[2] +" Col: "+ aOutput[3]
aOutput = myFunctionHash(sudoku)
showln "Return Hash.: T/F: "+ aOutput[:lValue] +" Row: "+ aOutput[:nRow] +" Col: "+ aOutput[:nCol]
###----------------------------------
### isSolvedSoduku - Return ARRAY
func myFunctionArray(sudoku)
for Row = 1 to 9
for Col = 1 to 9
if sudoku[Row][Col] = 0
//----------------------------
// Return Array with 3 fields
return [false, Row, Col]
end
end
end
return [true, Row, Col]
end
###----------------------------------
### isSolvedSoduku - Return HASH
func myFunctionHash(sudoku)
for Row = 1 to 3
for Col = 1 to 3
if sudoku[Row][Col] == 0
//---------------------------------
// Return Hash Table with 3 fields
return [ :lValue = false,
:nRow = Row,
:nCol = Col
]
end
end
end
return [ :lValue = False, :nRow = Row, :nCol = Col ]
end
###-----------------------------
Creating a Multi-Dimensional Array using List¶
A Multi-Dimensional Array of any size can be built using recursion in a Function
Example:
###---------------------------------------------------------
### Create Array -- Dimensions Any Size: 3D, 4D, 5D etc
dimList = [4,3,4]
bList = createDimList(dimList)
###---------------------------------------------------------
### Populate the arrays using a counter 1 , 4x4x4 = 256 , 2x3x4x5x6 = 720
Counter = 1
for Col=1 to dimList[1]
for Row=1 to dimList[2]
for Dep=1 to dimList[3]
blist[Col][Row][Dep] = Counter
Counter++
end
end
end
###-----------------------------------------------
### Print the array elements in block format
for Col=1 to dimList[1]
for Row=1 to dimList[2]
for Dep=1 to dimList[3]
show bList[Col][Row][Dep] show " "
end
show newline
end
show newline
end
###===========================
### FUNCTIONS
###-----------------------------------------------------------------------
### Recursive Create a Dimension Array
### Call by passing an array of dimensions: dimList = [2,3,4,5]
### Drop the first entry every iteration call, making newParms
###
### Example:
### dimList = [4,2,3,2] <<< Number and size of dimensions in array format
### bList = createDimList(dimList) <<< Call using the array as input
func createDimList(dimArray)
sizeList = len(dimArray)
newParms = []
for i = 2 to sizeList
Add(newParms, dimArray[i])
end
alist = list(dimArray[1])
if sizeList == 1
return aList
end
for t : alist
t = createDimList(newParms)
end
return alist
end
Swap Items¶
We can swap the list items using the Swap() function.
Example:
aList = [:one,:two,:four,:three]
showln aList
showln copy("*",50)
swap(aList,3,4)
showln aList
Output
one
two
four
three
**************************************************
one
two
three
four