REBOL for COBOL programmers |
OCCURS
Date written: February 6, 2015 This page explains how REBOL makes arrays, like the OCCURS in COBOL. COBOL OCCURSIn COBOL, if you want to store one thing after another in memory, you have to set up the memory area at compile time with the OCCURS statement, something like this: level-number-1 identifier-1 OCCURS integer-1 TIMES INDEXED BY index-name-1This is a simple example. There is a variation that allows for a variable number of occurrences, but that is beyond our scope here. Also, the OCCURS can be nested to allow for multi-dimensional arrays. You refer to an occurrence by referring to the identifier-1 followed by the index-name-1 in parentheses, after making sure the value of index-name-1 is some number that is between zero and the size of the array (as indicated by integer-1). The REBOL equivalentIn REBOL, you usually would make an array by making a block. You may, but don't have to, specify any size. Usually a person just sets a word to refer to an empty block, like this: >> blk1: make block! 20 == [] >> length? blk1 == 0 >> blk2: [] == [] >> length? blk2 == 0 >>Alternatively, if you are setting up some sort of table of values, you can hard-code it into memory, like this: >> blk3: ["a1" "a2" "a3"] == ["a1" "a2" "a3"] >> length? blk3 == 3 >>You can make a two-dimensional array by nesting blocks, like this: >> blk4: [ ["a1" "a2" "a3"] ["b1" "b2" "b3"] ["c1" "c2" "c3"] ] == [["a1" "a2" "a3"] ["b1" "b2" "b3"] ["c1" "c2" "c3"]] >> length? blk4 == 3 >> probe first blk4 ["a1" "a2" "a3"] == ["a1" "a2" "a3"] >> And finally, because of REBOL's "code is data" feature, you don't have to create an array by hard-coding values in your program. You can put the values into a text file, with the brackets you would use if you were hard-coding in the program and then "load" the text file with the "load" function to create that block in memory. Now, how to refer to items in a block. You can use the "path" notation: >> probe blk4/2/2 "b2" == "b2" >>You can use words with integer values as subscripts: >> ix1: 2 == 2 >> ix2: 2 == 2 >> probe blk4/:ix1/:ix2 "b2" >> probe blk4/(ix1)/(ix2) "b2"Notice how you need the colon in front of ix1 and ix2. This is referred to as the "get-word" notation because it refers to the values of ix1 and ix2. It "gets" their values. You also can use parentheses around ix1 and ix2 to cause them to be evaluated. You also can use the "pick" function: >> probe pick pick blk4 2 2 "b2" == "b2" >> probe pick pick blk4 ix1 ix2 "b2" == "b2" >>Notice that here you don't use a colon in front of ix1 and ix1. Those words are evaluated and replaced with their values. You DO need the colons if you use the path notation because if you did not use the colons (or parentheses) that notation would have a different meaning. The reason for that is that a block in REBOL is not like an array in COBOL. In COBOL, and array is one thing after another, but they are the same kinds of things. In REBOL, a block can contain one thing after another, but they can be many different things, including other words with values. Therefore, to access those other things inside a block, you need the path notation. Since this is not a REBOL reference manual, there is a vast amount unmentioned here. You absolutely must read the REBOL/Core user User Manual on the REBOL site. In REBOL, a block is a type of data known more generally as a series. Other types of data are of the series type also, which generally refers to things that occur one after the other. Besides blocks, you also then have strings. By making many things of the series data types, REBOL can use common series functions on data of that type. Mastering the series is a core REBOL skill. This page is not presented to give mastery; it is presented so that you do in REBOL something like you would do in COBOL, to get you going on REBOL programming. |