2 single: Lists; Introduction
8 In this chapter we are going to learn how to deal with lists.
11 pair: Lists; Create Lists
16 We can create new lists by defining the list items inside square bracts.
24 Also we can create new lists using the : operator
41 Also we can create lists using the list() function
53 aList = list(10) # aList contains 10 items
55 .. note:: the list index start from 1
58 pair: Lists; Add Items
63 To add new items to the list, we can use the Add() function.
80 Also we can do that using the + operator.
92 aList = 1:10 # create list contains numbers from 1 to 10
93 aList + 11 # add number 11 to the list
94 see aList # print the list
97 pair: Lists; Get List Size
102 We can get the list size using the len() function
114 aList = 1:20 see len(aList) # print 20
117 pair: Lists; Delete Item From List
119 Delete Item From List
120 =====================
122 To delete an item from the list, we can use the del() function
134 aList = ["one","two","other","three"]
135 Del(aList,3) # delete item number three
136 see aList # print one two three
140 pair: Lists; Get List Item
145 To get an item from the list, we uses the next syntax
155 aList = ["Cairo","Riyadh"]
156 see "Egypt : " + aList[1] + nl +
157 "KSA : " + aList[2] + nl
160 pair: Lists; Set List Item
165 To set the value of an item inside the list, we can use the next syntax
169 List[Index] = Expression
175 aList = list(3) # create list contains three items
176 aList[1] = "one" aList[2] = "two" aList[3] = "three"
185 To find an item inside the list we can use the find() function
191 Find(List,ItemValue) ---> Item Index
192 Find(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index
193 Find(List,ItemValue,nColumn,cAttribute) ---> Item Index
199 aList = ["one","two","three","four","five"]
200 see find(aList,"three") # print 3
210 see find(mylist,"two",1) + nl # print 2
211 see find(mylist,2,2) + nl # print 2
213 Also we can use the binarysearch() function to search in sorted list.
219 BinarySearch(List,ItemValue) ---> Item Index
220 BinarySearch(List,ItemValue,nColumn) ---> Search in nColumn, returns the Item Index
226 aList = ["one","two","three","four","five"]
228 see binarysearch(aList,"three")
247 We can sort the list using the sort() function.
253 Sort(List) ---> Sorted List
254 Sort(List,nColumn) ---> Sorted List based on nColumn
255 Sort(List,nColumn,cAttribute) ---> Sorted List based on Object Attribute
261 aList = [10,12,3,5,31,15]
262 aList = sort(aList) see aList # print 3 5 10 12 15 31
264 We can sort list of strings
270 mylist = ["mahmoud","samir","ahmed","ibrahim","mohammed"]
271 see mylist # print list before sorting
272 mylist = sort(mylist) # sort list
273 see "list after sort"+nl
274 see mylist # print ahmed ibrahim mahmoud mohammed samir
276 We can sort a list based on a specific column.
282 aList = [ ["mahmoud",15000] ,
285 ["mohammed", 12000 ] ,
288 aList2 = sort(aList,1)
308 pair: Lists; Reverse()
313 We can reverse a list using the reverse() function.
319 Reverse(List) ---> Reversed List
325 aList = [10,20,30,40,50]
326 aList = reverse(aList)
327 see aList # print 50 40 30 20 10
330 pair: Lists; Insert()
335 To insert an item in the list we can use the insert() function.
341 Insert(List,Index,Item)
343 The inserted item will be AFTER the Index
349 aList = ["A","B","D","E"]
350 insert(aList,2,"C") # Inserts AFTER Index 2, "C" into Position 3
351 see aList # print A B C D E
355 pair: Lists; Nested Lists
360 The list may contain other lists
366 aList = [ 1 , [10,20,30] , 5 , [100,1000,5000] ]
376 see aList[2] # print 10 20 30
377 see aList[4][3] + nl # print 5000
378 see aList2[5][2] + nl # print four
379 see aList2[5][4][3] # print 300
382 pair: Lists; Copy Lists
387 We can copy lists (including nested lists) using the Assignment operator.
402 aList2 = aList # Copy aList to aList2
403 aList2[5] = "other" # modify item number five
404 see aList2[5] + nl # print other
405 see aList[5] # print three four five 100 200 300
409 pair: Lists; First-Class Lists
414 Lists are `first-class citizens <http://en.wikipedia.org/wiki/First-class_citizen>`_ where we can store
415 lists in variables, pass lists to functions, and return lists from functions.
421 aList = duplicate( [1,2,3,4,5] )
422 see aList[10] + nl # print 5
424 see mylist() # print 10 20 30 40 50
433 func mylist return [10,20,30,40,50]
436 pair: Lists; Using Lists during definition
438 Using Lists during definition
439 =============================
441 We can use the list items while we are defining the list for the first time.
447 aList = [ [1,2,3,4,5] , aList[1] , aList[1] ]
448 see aList # print 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
452 pair: Lists; Passing Lists to Functions
454 Passing Lists to Functions
455 ==========================
457 Lists are passed to functions by reference, This means that the called
458 function will work on the same list and can modify it.
465 aList = [1,2,3,4,5] # create list, local in function main
466 myfunc(aList) # call function, pass list by reference
467 see aList # print 1 2 3 4 5 6 7 8 9 10
474 pair: Lists; Access List Items by String Index
476 Access List Items by String Index
477 =================================
479 Instead of using numbers to determine the item index when we get item value or set item value,
480 We can access items using string index if the item is a list contains two items and
481 the first item is a string.
487 aList = [ ["one",1] , ["two",2] , ["three",3] ]
488 see aList["one"] + nl +
490 aList["three"] # print 1 2 3
492 This type of lists can be defined in a better syntax using the : and = operators.
498 aList = [ :one = 1 , :two = 2 , :three = 3 ]
499 see aList["one"] + nl +
501 aList["three"] + nl # print 1 2 3
502 see aList[1] # print one 1
504 .. tip:: using : before identifier (one word) means literal
506 .. note:: using = inside list definition create a list of two items where
507 the first item is the left side and the second item is the right side.
509 We can add new items to the list using the string index
516 aList["Egypt"] = "Cairo"
517 aList["KSA"] = "Riyadh"
518 see aList["Egypt"] + nl + # print Cairo
519 aList["KSA"] + nl # print Riyadh
522 pair: Lists; Passing Parameters or Arguments Using List
524 Passing Parameters or Argumnents Using List
525 ===========================================
527 This type of lists is very good for passing parameters to functions
528 Where the order of parameters will not be important (we can change the order).
530 Also some parameters maybe optional.
536 myconnect ( [ :server = "myserver.com" , :port = 80 ,
537 :username = "mahmoud" , :password = "password" ] )
539 func myconnect mypara
541 # print connection details
542 see "User Name : " + mypara[:username] + nl +
543 "Password : " + mypara[:password] + nl +
544 "Server : " + mypara[:server] + nl +
545 "Port : " + mypara[:port]
547 pair: Lists; Passing Parameters Arguments Using List Array
549 Passing Parameters or Argumnents Using List Array
550 =================================================
552 Passing Arguments or Parmameters to a Function in an array format
558 myList = [5,7,3,9] ### list with args or parms in an array
560 See "Sum result: "+ result +n
564 sizeList = len(aList)
566 for i = 1 to sizeList
574 pair: Lists; Return as List or Hash Table
576 Return Parameters as List or Hash Table
577 =======================================
579 Return Parameters from a Function in an Array or Hash Format
589 aOutput = myFunctionArray(sudoku)
590 See "Return Array: T/F: "+ aOutput[1] +" Row: "+ aOutput[2] +" Col: "+ aOutput[3] +nl
592 aOutput = myFunctionHash(sudoku)
593 See "Return Hash.: T/F: "+ aOutput[:lValue] +" Row: "+ aOutput[:nRow] +" Col: "+ aOutput[:nCol] +nl
595 ###----------------------------------
596 ### isSolvedSoduku - Return ARRAY
598 Func myFunctionArray(sudoku)
601 if sudoku[Row][Col] = 0
603 //----------------------------
604 // Return Array with 3 fields
605 return [False, Row, Col]
609 return [True, Row, Col]
611 ###----------------------------------
612 ### isSolvedSoduku - Return HASH
614 Func myFunctionHash(sudoku)
617 if sudoku[Row][Col] = 0
619 //---------------------------------
620 // Return Hash Table with 3 fields
621 return [ :lValue = False,
629 return [ :lValue = False, :nRow = Row, :nCol = Col ]
631 ###-----------------------------
635 pair: Lists; Create Multi Dimension Array Using List and Recursion
637 Creating a Multi-Dimensional Array using List
638 ==============================================
640 A Multi-Dimensional Array of any size can be built using recursion in a Function
646 ###---------------------------------------------------------
647 ### Create Array -- Dimensions Any Size: 3D, 4D, 5D etc
650 bList = createDimList(dimList)
652 ###---------------------------------------------------------
653 ### Populate the arrays using a counter 1 , 4x4x4 = 256 , 2x3x4x5x6 = 720
657 for Col=1 to dimList[1]
658 for Row=1 to dimList[2]
659 for Dep=1 to dimList[3]
660 blist[Col][Row][Dep] = Counter
666 ###-----------------------------------------------
667 ### Print the array elements in block format
669 for Col=1 to dimList[1]
670 for Row=1 to dimList[2]
671 for Dep=1 to dimList[3]
672 See bList[Col][Row][Dep] See " "
679 ###===========================
682 ###-----------------------------------------------------------------------
683 ### Recursive Create a Dimension Array
684 ### Call by passing an array of dimesions: dimList = [2,3,4,5]
685 ### Drop the first entry every iteration call, making newParms
688 ### dimList = [4,2,3,2] <<< Number and size of dimensions in array format
689 ### bList = createDimList(dimList) <<< Call using the array as input
691 func createDimList(dimArray)
693 sizeList = len(dimArray)
696 for i = 2 to sizeList
697 Add(newParms, dimArray[i])
700 alist = list(dimArray[1])
707 t = createDimList(newParms)
714 pair: Lists; Swap Items
719 We can swap the list items using the Swap() function.
725 aList = [:one,:two,:four,:three]
727 see copy("*",50) + nl
739 **************************************************