List and strings

Find quick help here to get you started with Hollywood
amyren
Posts: 47
Joined: Thu May 02, 2019 11:53 am

List and strings

Post by amyren » Sat May 11, 2019 11:12 pm

Could anyone tell me the correct syntax for this piece of code?
Now it will complain about that textout is expecting a string.

Code: Select all

test$={}
InsertItem(test$,"mytext")
TextOut(20, 100, test$)	

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: List and strings

Post by msu » Sat May 11, 2019 11:30 pm

Code: Select all

test$={}
InsertItem(test$,"mytext")
TextOut(20, 100, test$[0])
WaitLeftMouse	
End

amyren
Posts: 47
Joined: Thu May 02, 2019 11:53 am

Re: List and strings

Post by amyren » Sun May 12, 2019 12:46 pm

Thanks.
You made that look embarrasingly easy :D I tried numerous combinations pretty close to the correct one before getting frustrated enaugh to ask.

But this leads me to my next question(s).

I do have a program I made in blitzbasic that I want to transform into HollyWood code. At first I just like to get it to work, with possibly as few changes as possible.
In blitz, for lists like this, blitz will keep track of the current item, which will be the last item accessed.
So that when the variable is called with an empty bracket like test$() the current list item wil be returned.
Then one can use commands like FirstItem, NextItem, PrevItem and LastItem to navigate through the list, and will set the current item accordingly.
AddItem or Addlast will also change the current item while adding an item.
Usually these commands is used in conjunction with IF, so it also will perform a check. Then one will know if the list is full or empty, or you already have reached the first or the last item. I find that useful also for error checking, since if the program tries to use an non-existing item in a list, the IF expression will return a False statement which can be used in the program by the Else command.

How are these things handled in HollyWood?
I did look up the docs for NextItem, but this seem to relate to start or Nil, so I guess there is no such term in Hollywood as current item.
In this case, it may look like I must find my own routine for keeping track of the current item for each list I may have.

Not sure if this makes sense. But i found there was very few of the Examples that could enlighten these questions for me.

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: List and strings

Post by msu » Sun May 12, 2019 5:02 pm

I think a 1: 1 implementation will not work.
I recommend you to use the "Traditional" Method.
In addition, the table library is very slow. :oops:

amyren
Posts: 47
Joined: Thu May 02, 2019 11:53 am

Re: List and strings

Post by amyren » Sun May 12, 2019 8:13 pm

msu wrote:
Sun May 12, 2019 5:02 pm
I think a 1: 1 implementation will not work.
I recommend you to use the "Traditional" Method.
In addition, the table library is very slow. :oops:
The program is not using any advanced blitz commands, only system friendly stuff.
I will have to reconstruct the GUI, and the part where a few images are shown. The rest is mostly common commands.
And, obviously I will need to figure a different way to handle my lists.
The speed is not an issue for me at the moment.

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: List and strings

Post by msu » Sun May 12, 2019 9:28 pm

Just post a simple BlitzBasic example.
Maybe then I or someone else can help.

Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

Re: List and strings

Post by Bugala » Mon May 13, 2019 9:13 am

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:

Code: Select all

mylist = {}
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:

Code: Select all

ForEach(mylist, DebugPrint)
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:

Code: Select all

currentitem = getnextitem(mylist)
But usually it is better to try to do it the Hollywoods intended way.

amyren
Posts: 47
Joined: Thu May 02, 2019 11:53 am

Re: List and strings

Post by amyren » Mon May 13, 2019 11:25 am

msu wrote:
Sun May 12, 2019 9:28 pm
Just post a simple BlitzBasic example.
Maybe then I or someone else can help.
Thanks, I'll keep that in mind.
I will do some more attempts myself to resolve this first . I found that way I learn more.

amyren
Posts: 47
Joined: Thu May 02, 2019 11:53 am

Re: List and strings

Post by amyren » Sat May 18, 2019 10:59 am

Why doesnt this first example work

Code: Select all

	If Exists("save/mydata.data") 
		OpenFile(1, "save/mydata.data", #MODE_READ)
		While Not Eof(1)
			loadedline$ = ReadLine(1)
			If not loadedline$ = "unwanted" ;dont load this entry
				InsertItem(myvar$, loadedline$)
			EndIf
		Wend	
		CloseFile(1)
	EndIf
While this does?

Code: Select all

	If Exists("save/mydata.data") 
		OpenFile(1, "save/mydata.data", #MODE_READ)
		While Not Eof(1)
			loadedline$ = ReadLine(1)
			If loadedline$ = "unwanted" ;dont load this entry
			Else
				InsertItem(myvar$, loadedline$)
			EndIf
		Wend	
		CloseFile(1)
	EndIf
The first example will return an error about comparing number with string
I cant see what makes the loadedline$ variable a number in my first example. Could anyone explain this?

User avatar
msu
Posts: 44
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: List and strings

Post by msu » Sat May 18, 2019 9:31 pm

"Not" can not be used with string variables.
"Not" can only result in True (1) or False (0).

Post Reply