Heres a bit to just get you the idea how it would be done in Hollywood, or at least this is how I would do it.
First I am making a list:
this makes me empty list (or actually it is empty table that can be used as a list, or something else too)
Next, lets add some items in way that you might feel more familiar, a guess based upon your explanation:
Code: Select all
InsertItem(mylist, "one")
InsertItem(mylist, "two")
InsertItem(mylist, "three")
Next I am going to show you three different ways to go through this list:
Code: Select all
for n = 1 to 2
debugprint(mylist[n])
next n
What happens here is that I am using common FOR loop to go through 2 different numbers and then it uses to get specific place from that list, like item number 1 or item number 2, and then it simply debugprints out the content.
This will result in "two" and "three" being printed out. Why? Because actually the first item in that list is at location 0. if you want it to print out "one" you need to access mylist[0], now you are accessing mylist[1] and mylist[2], which are actually "two" and "three".
I am doing the same again, but bit different this time to answer one question of yours:
Code: Select all
for n = 1 to 3
if rawget(mylist, n) then debugprint(mylist[n])
next n
This program now has an error. You might be expecting for "one", "two", "three" to be printed out, but actually only "two" and "three" are being printed out, mylist[3] doesnt exist, and if you would try to print that out, it would result in an error.
However, I added at beginning of that line an IF condition. This Rawget command checks if this item really exists, in this case, when n equals 3, it will in Rawget check "mylist" and see if there is item number 3, since there is not, then IF condition is not met, and THEN is not executed, and hence there is no error exit happening in this program.
So when you mentioned Blitz takes care of that there is something, this is the Hollywood way of taking care of it, which is to check yourself.
We could change this into following as well:
Code: Select all
n=0
Repeat
DebugPrint(mylist[n])
n=n+1
Until RawGet(mylist, n)=Nil
In this case we dont need to know how many items there are on that list, but it will start from beginning and continue until it notices there are no more items there.
This RawGet actually works so that it takes the item from list, and in case the item does not exist, instead of throwing an error, it is instead marking it as NIL, which means not existing.
Therefore as I am increasing n by one at end of loop, I am then at that UNTIL checking that if RawGet results in NIL (= no item) then it will stop the loop and continue with the rest of the program.
Instead of using RAWGET, I would however rather first figure out the size of list, and this is now the second way (all those previous were first way of doing it):
Code: Select all
size = TableItems(mylist)
For n=0 To size-1
DebugPrint(mylist[n])
Next
In this one I first take the amount of items in that list by using "
TableItems()" command, and then I make a for loop out of it, now knowing exactly the number of items in that list.
However, do notice that table items gives human kind of number of items, that if there are three items in list, it will tell there are 3 items.
However, when making that for loop, we need to remember that list starts from 0, so for three items, we are not going from 1 to 3, but from 0 to 2, therefore i used in that for defition size-1.
That when i do tableitems, it results in 3 as three items, but for loop, I will use 0 to 3-1, which results in 0 to 2, as it should be.
Third way to do this is to use ForEach command, this is useful sometimes, but often this tableitems system is better:
In this case, it tells to go through everyitem in "mylist" table, and use debugprint command on them.
This is very good in sense that in this case Hollywood takes care of itself that it goes through all the items, however, as you can see from the result, there is one obvious problem with foreach command, for there is no guarantee on what order it goes through these items. In practice it seems to start from item number 1, then go forward step by step until as last it will go through item number 0. There however is no guarantee this will be the order, as this command have not been defined to go through the list in some certain order, hence while it might work now, it might not work after you do some changes or hollywood reaches next version. Therefore if it matters in which order the list is gone through, then this is not to be used.
Notice also, that if you have mylist[1] and mylist[3] occupied, but mylist[2] not, then this one can give some unexpected behavior as well, since many of those Hollywoods size calculation commands are based upon each spot being occupied. That you might have a list of thousand items, and then when item number 3 is unoccupied, command starts looking at how many items you have, goes step by step forward, and when it notices spot number 3 is NIL, it will decide list ended there and returns the counting result, which is wrong since it missed all the rest of the thousand items.
Anyway, this might give you some idea about how Hollywood handles these, that it is very differfent from Blitz approach, that while (i suppose, dont really know) in blitz you can jump several lines and then use "nextitem", in Hollywood you basically make a loop that centers upon going through that list and all the rest of those lines go inside that loop.
You can of course make functions that would retrieve you the next item from some list, and this way make it behave blitz way:
But usually it is better to try to do it the Hollywoods intended way.