home *** CD-ROM | disk | FTP | other *** search
/ PC Format (Spanish) 3 / PCF3.iso / formula / lists.sx_ / lists.sx
Text File  |  1996-04-06  |  4KB  |  134 lines

  1. ////////////////////////
  2. // Harrow Software 96
  3. // List examples
  4.  
  5. ////////////////////////////////////////////////
  6. // (1) - Adding objects to a list
  7.  
  8. // Several object will be created for this example
  9.  
  10. $my_string = "a string"                    //a string
  11. my_array = new float[100]                //a floating point array
  12. my_structure = new struct                //a structure
  13.  
  14. // A new list will now be created and the objects will
  15. //  be placed on the list
  16.  
  17. my_list = new list
  18.  
  19. my_list add my_string
  20. my_list add my_array
  21. my_list add my_structure
  22.     my_structure.value = 100
  23.     my_structure.flag = TRUE
  24.  
  25. // We can count how many items are on the list using the 'max'
  26. //  extension
  27.  
  28. message "There are ", my_list.max, " objects on the list"
  29.  
  30. // Now that these objects are on the list, they can be passed
  31. //  around using one convenient handle. At some later stage we
  32. //  may need to get them off the list. We can get a handle on
  33. //  them while they are on the list
  34.  
  35. my_string = my_list get 0
  36. my_array = my_list get 1
  37. my_structure = my_list get 2
  38.  
  39. //  or we can pop them off the top of the list and leave the
  40. //  list empty. Note that the last one added to the list will
  41. //  be the first one off
  42.  
  43. my_structure = my_list pop
  44. my_array = my_list pop
  45. my_string = my_list pop
  46.  
  47. // if necessary we can make sure that the list is emptry
  48.  
  49. my_list remove all
  50.  
  51. ////////////////////////////////////////////////
  52. // (2) Lists of lists and object names
  53.  
  54. // lists are objects too and so a list can be added to another
  55. // list. We can say that a parent list has a handle to a
  56. // child list
  57.  
  58. my_other_list = new list
  59. my_list add my_other_list
  60.  
  61. // Objects can be given names. In this case we will assign a
  62. //  name as well as a number to an array object and place
  63. //  it on our child list
  64.  
  65. my_object = new float[100]
  66. $my_object.name = "some name"
  67. my_other_list add my_object
  68.  
  69. // We can then get the object from the list using its name
  70. //  rather than its position. In this case we will search the
  71. //  parent list for the name of the object. Not only will the
  72. //  parent list be searched but also the child list (and its
  73. //  children)
  74.  
  75. my_object = my_list find "some name"
  76.  
  77. ////////////////////////////////////////////////
  78. // (3) List loops
  79.  
  80. // When you need to look at every object on a list the fastest
  81. //  way to do it is with a list loop. First we will create a
  82. //  network of lists containing different types of objects.
  83.  
  84. my_string_list = new list                // a new list of strings
  85. my_string_array = new byte[10][32]        // a new array of strings
  86. $my_string_array[0] = "zero";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"
  87.  
  88. my_string_list add my_string_array        // add the array to the list
  89. for n = 0 to my_string_array(0)-1
  90.     $my_string = $my_string_array[n]    // add every string to the list
  91.     my_string_list add my_string
  92.  
  93. my_struct_list = new list                // a new list of structures
  94. for n = 0 to 9
  95.     my_structure = new struct            // a new structure
  96.     $my_structure.name = "struct",n
  97.     my_structure.value = n
  98.     my_structure.flag = TRUE
  99.     my_struct_list add my_structure        // add the structure to the list
  100.  
  101. my_everything_list = new list            // make a complete list
  102. my_everything_list add my_string_list    // add the other lists
  103. my_everything_list add my_struct_list
  104.  
  105. // Now we can get these items from the list. First we will
  106. //  display the types of items in the base list.
  107.  
  108. message "Parent list"
  109. the_item = my_everything_list loop n
  110.     message $the_item.type
  111. message ""
  112.  
  113. // Then we can display type of every item in every list
  114.  
  115. message "All lists"
  116. the_item = my_everything_list loop n mode LIST_TREE
  117.     message $the_item.type," - ",n
  118. message ""
  119.  
  120. // Finally we can get a handle to every item of a certain type
  121. // on the list. In this case we will get the structures.
  122.  
  123. message "Structures up"
  124. the_item = my_everything_list loop n type "Structure" mode LIST_TREE
  125.     message $the_item.name," ",the_item.value," ",the_item.flag
  126. message ""
  127.  
  128. message "Structures down"
  129. the_item = my_everything_list loop n type "Structure" mode LIST_TREE | LIST_DOWN
  130.     message $the_item.name," ",the_item.value," ",the_item.flag
  131. message ""
  132.  
  133.  
  134.