Negative arrays

Find quick help here to get you started with Hollywood
Post Reply
nmakk
Posts: 17
Joined: Wed Apr 22, 2020 11:40 pm

Negative arrays

Post by nmakk »

For some reason I need to have an array that is two dimensional and starts from -2 and finishes at 10, like:

a[-2][-2] up to a[10][10]

I read in the Hollywood documentation that "In Hollywood, indices cannot only be positive integers, but also negative integers, ... . For example, you can also initialize negative elements of the table" (Chapter 8 Data Types / Tables)

For the above array I defined the part from a[0][0] to a[10][10] with the DIM statement, but for the negative indicies I used the direct mehod (as DIM does not accept negative numbers -- in case of a DIM a[-10][-10] it runs into a 'value needed error'):

a={}
a[-1]={}
a[-1][-1]=0
... etc until I initialize all variables as needed.

As this threw some errors when using the variable with negative indicies, I defined then the whole array like this:

min = -2
max = 10

a = {}
For i = min To max
a[i] = {}
For j = min To max
a[i][j] = 0
Next
Next

You can compile a file with these in there, but the only issue is that this does not work... in case I refer to any negative indicies this runs to an error:
- table -1 was not initialized...

Any idea how can you initialize a negative index array?
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Negative arrays

Post by SamuraiCrow »

Not enough information to solve it. Can you use the IPairs iterator to list all the keys and values of the first dimension of arrays?
I'm on registered MorphOS using FlowStudio.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Negative arrays

Post by Bugala »

Code: Select all

Min = -2
Max = 10

a = {}
For i = Min To Max
a[i] = {}
For j = Min To Max
a[i][j] = 0
Next
Next

DebugPrint("a[1][1]="..a[1][1])
DebugPrint("a[-1][-1]="..a[-1][-1])
It seems to work fine for me.

But one possibility is that the "0" gives a problem since that could be same as nothing.

So try change number 1 instead for example.
nmakk
Posts: 17
Joined: Wed Apr 22, 2020 11:40 pm

Re: Negative arrays

Post by nmakk »

Thank you, tried with another value, the same occurs.

BUT:

I tried the Pairs() as well and interesting outcome:
in the main block this works fine and lists up all the indicies of the table, inccluding the negative ones:

Code: Select all

For key, value in Pairs(a[-2]) Do NPrint(key, value)
However, when I put this into a function that is called later on, then the same Pairs comes with the output of the positive indices only (so no value for -1 and -2)...

Why is it different in a function and in the main block? This is defined as a global variable (table) at the beginning of the main code, I also used the Global statement before that to be sure...

thank you!
nmakk
Posts: 17
Joined: Wed Apr 22, 2020 11:40 pm

Re: Negative arrays

Post by nmakk »

So, just for the record:

in the main block after the initial cycle of assigning values to the 'array' from -2 to 10 I had other value assignments in a form:

Code: Select all

a[0]={1,2,3,4,5,6,7,8,9,10}
and that deleted (sent to NIL:) all values and keys before 0... Anything after this that referred to anything below 0 in the index gave an error of reaching to -1 etc.

So: I went through my whole script and eliminated the negative arrays, using only positives now. Now the script works, though it remains a mystery for me why the negative indexes are not handled properly (easily can be my igorance / low knowledge...) -- for me it seems that the default is an array from 0 (like if you define it with DIM -- and you cannot define negative indexes with DIM nor you can start the value assignement from below 0 in the above form of a[0]={}) and in specific circumstances Hollywood falls back to this default.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Negative arrays

Post by airsoftsoftwair »

Not sure what is the problem here because....
nmakk wrote: Tue May 05, 2020 9:01 pm a={}
a[-1]={}
a[-1][-1]=0
... etc until I initialize all variables as needed.
As this threw some errors when using the variable with negative indicies
...this code works fine here. Try:

Code: Select all

a={}
a[-1]={}
a[-1][-1]=0
DebugPrint(a[-1][-1])
Prints "0" here so it works but you said that the code threw some errors? What exactly is throwing errors? The code above works here...

Also, your second code snippet works fine here. Try:

Code: Select all

min = -2
max = 10
a = {}
For i = min To max
a[i] = {}
For j = min To max
a[i][j] = 0
Next
Next
DebugPrint(a[-1][-1])
Prints 0 as it is supposed to be. So what is the problem please? :)
nmakk
Posts: 17
Joined: Wed Apr 22, 2020 11:40 pm

Re: Negative arrays

Post by nmakk »

Hi Andreas,

I think it was an issue in my code, that the array was overwritten by a later definition.

So to decribe in more detail: after I successfully created the negative array with

Code: Select all

min = -2
max = 10
a = {}
For i = min To max
a[i] = {}
For j = min To max
a[i][j] = 0
Next
Next
I issued a command like this:

Code: Select all

a[0]={1,2,3,4,5,6,7,8,9,10}
after this the negative index, like: a[0][-1] was not reachable anymore (threw an error - "table -1 was not initialized...")
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Negative arrays

Post by airsoftsoftwair »

Yes, this is a mistake in your code then because that code obviously resets a[0] to just a single table.
Post Reply