SyntaxBomb - Indie Coders

Languages & Coding => SmallBASIC => Topic started by: twgonder on July 05, 2021, 17:31:13

Title: Two for one, NULL and setting all array elements to a default
Post by: twgonder on July 05, 2021, 17:31:13
Some BASICs I've used in the past differentiate between NULL and "" (empty string).
for example a = null: print empty(a) [displays 1]
while a = "": print empty(a) [displays 0]
How does small BASIC handle these?

I'm used to error messages that say "Variable not assigned". Does smallBASIC allow for this type of situation rather than just assigning 0 to any variable that isn't explicitly assigned.

Also, some BASICs have a way to set all elements at once, without the need for loops
for example:
Dim a(100,100,100)
Mat a = NULL (or some other actual value)

I can't find such a command or syntax in the online reference manual. If it exists, it should be part of the sample program for  DIM

Not having such a command means I have to write 3 nested for...next loops to set all the array elements to a default.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Aurel on July 05, 2021, 19:24:15
Quote
Also, some BASICs have a way to set all elements at once

what?
which basic can do that ..and even most programming languages cannot set each element to specific value
without using loop.

what you post is ..you declare 3 dimensional array with sizes 100
to set all element of 3 array to null you need only one loop not 3 loops

for i = 0 to 100
    a(i,i,i) = 0
next i

i guess that this work in SB
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Midimaster on July 06, 2021, 00:41:47
....to set all element of 3 array to null you need only one loop not 3 loops

for i = 0 to 100
    a(i,i,i) = 0
next i

This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

Your solution would only fill the 100 elements with "3 times the same order number" like:
[0,0,0] and [1,1,1] and [2,2,2] and...

but not the other 999.899 like [0,0,1] or [0,0,2 ] or ... or [1,0,0] or [37,16,4]...



Is has to be like this:
Code: [Select]
For i=0 to 99
   For j=0 to 99
     For k=0 to 99
         a(i,j,k) = NULL
      Next
   Next
Next

But also interations like this are done below 1msec on modern computers. So no performance problem.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Aurel on July 06, 2021, 08:00:26
Quote
This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
a.1 100
a.2 100
a.3 100

and not multiplay elements ,where you learn programming ?
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Midimaster on July 06, 2021, 09:51:45
...In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
and not multiplay elements ,where you learn programming ?

This is completely different from BlitzMax, where a 3-dim-Array gives dim*dim*dim elements
Code: BlitzMax
  1. Global a:Int[100,100,100]
  2. Print a.length
returns:
Code: [Select]
1000000
Also in C or BlitzBasic the number of elements is always  the product of the dimension sizes:

Example in C
Code: BlitzBasic
  1. Dim a$(3,4,5)
  2.  
gives 3*4*5 = 60 Elements

Could you please re-think your statement and your unfriendly speak.


by the way: I learned BASC on one of the first PET. Then GFA-Basic on Atari, then Visual-Basic on Windows. Later BlitzMax and now often the new Android-Basic B4A . None of them had arrays like you describe them.


Title: Re: Two for one, NULL and setting all array elements to a default
Post by: iWasAdam on July 06, 2021, 10:50:51
yup - midimaster is utterly correct

dim [10] = 10 elements
dim [10,10] = 10x10 = 100 elements
dim [10,10,10] = 10x10x10 = 1000 elements

check out google or any programming book to learn what an array is
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm (https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm)

remember - just because YOU think it's right - doesn't make it so
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: bplus on July 06, 2021, 11:18:30
Quote
This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
a.1 100
a.2 100
a.3 100

and not multiplay elements ,where you learn programming ?

@Aurel they are correct.
Say you want to assign a color for 3D point. You have to go:
for x = 1 to xmax
 for y = 1 to ymax
  for z = 1 to zmax
   myColor(x,y,z) =rgb_formula(x, y, z)
  next
 next
next

if all max's are same for a cube say with side 100 that is 100^3 elements.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: bplus on July 06, 2021, 11:26:32
@twgonder

To my knowledge of sb, you can not do multiple assignments without loop but can depend that everything initialzed at 0 or "" at start, but be careful and warned sb has variable typing so can be confused whether something is number or string like try to input digits you want treated like a string not numbers.

Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Qube on July 06, 2021, 12:59:13
In your awkward brain 3 dim array which is declared with 3 row have only 300 elements..... where you learn programming ?
No need to insult or have digs at others. Even if Midimaster was wrong, which he isn't, then this kind of response is silly. Any further violations will result in a 30 day ban from SyntaxBomb.

Midimaster was correct and you are incorrect.

Dim[ 100, 100, 100 ] has 1,000,000 elements or 1,030,301 elements depending if Dim[ 100 ] goes from 0 to 99 or 0 to 100.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Aurel on July 06, 2021, 14:57:44
wait a moment
in a compiler which i use more than decade it is like i said
number of elements is not multiplied
so again 3 rows where each row or each list contain 100 elements
is 300 elements .
He jump from BlitzMax to BlizPlus then explained it wrong .
so..there is case where is not .
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Aurel on July 06, 2021, 15:01:12
@Bplus
RGB formula is this

Function RGB(byval red as INT,byval green as INT,byval blue as INT) as INT
  INT color
  color = red
  color = color + green*256
  color = color + blue*65536
  Return color
End Function

and as you can see values are multiplied
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: twgonder on July 06, 2021, 15:15:37
yup - midimaster is utterly correct

dim [10] = 10 elements
dim [10,10] = 10x10 = 100 elements
dim [10,10,10] = 10x10x10 = 1000 elements

check out google or any programming book to learn what an array is
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm (https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm)

remember - just because YOU think it's right - doesn't make it so

The original example I made had 100 not 10.
So he was correct.

okay, so I wanted to delete this meaningless post, how does one do that? I can't see it as an option.
Also I don't see a "cancel" button to escape from changing a posting.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Derron on July 06, 2021, 15:26:05
In BlitzMax you have
Multidimensional Arrays
and
Array of Arrays.

Maybe the confusion here is about something similar. So [100,100,100] is in the minds of some of you an array consisting of arrays with each 100 elements.


bye
Ron
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: jsalai on July 06, 2021, 19:11:49
In BlitzMax you have
Multidimensional Arrays
and
Array of Arrays.

Maybe the confusion here is about something similar. So [100,100,100] is in the minds of some of you an array consisting of arrays with each 100 elements.


bye
Ron

For SmallBASIC I kindly propose to forget everything about arrays someone ever learned:

eg:
Code: [Select]
a=seq(1,10,10)
b=seq(2,16,8)
c=seq(5,60,12)

h << a
h << "whatever"
h << b
h << "whatever"
h << c
h << "whatever"
h(0) << b
h(0)(3) << b
insert h(0)(3),3,"whatever"
insert h(0),3,a
insert h(0),5,"whatever"
?h
pause
end

one should read something like:

[1,2,3,[1,2,3,4,5,6,7,8,9,10],[[2,4,6,8,10,12,14,16],whatever],whatever,5,6,7,8,
9,10,[2,4,6,8,10,12,14,16]],whatever,[2,4,6,8,10,12,14,16],whatever,[5,10,15,20,25,30,35,40,45,50,55,60],whatever]

nested, valiable length, numbers, strings, whatever.....
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Midimaster on July 07, 2021, 07:45:26
but this looks more like a list not like an array. Are users able to really build usable 3dimensional arrays with this?

The fundamental property of an array is, that the position of the values is fixed. 
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: jsalai on July 07, 2021, 08:27:06
but this looks more like a list not like an array. Are users able to really build usable 3dimensional arrays with this?

The fundamental property of an array is, that the position of the values is fixed.

Of course!
It depends on programmers discipline...
I gave just a random example of how the dynamic array MAY look.
Every item whether literal or sub-array is precisely accessible by h(x)(y)....(z)
I use nested arrays (you may call them as lists or tables or whatever) in my practice.

Btw, all arrays whether dimensioned or not, are fully dinamic, so can be expanded, shortened, inserted etc.
Usually, just for reference, I write dim h(); as you can see from the example (which is complete!) that is NOT needed.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: Midimaster on July 07, 2021, 08:34:24
ah! now I understand the concept.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: bplus on July 07, 2021, 12:15:35
As jsalai points out, sb array handling truly first class. No restrictions on type in same array including items that are arrays themselves and inserting deleting elements is not a problem either.

For me arrays of arrays of arrays... is mind boggling, might make for some interesting fractals.
Title: Re: Two for one, NULL and setting all array elements to a default
Post by: jsalai on July 07, 2021, 16:52:53
For me arrays of arrays of arrays... is mind boggling, might make for some interesting fractals.

As I stated, depends on programmers discipline.
Eg, when I need some operation on different objects (geometrical shapes of mechanical objects, for example) with different number of parameters (circle, semicircle, rectangle, triangle, etc), with different properties (colors, centroids, areas, etc) in strict predefined order (eg cut out a circle from a triangle which MUST exist), I put the arrays of their properties sequentially in a master array.
Now, to access them, I simply use:

for arr in master_arr
   do something with that "arr" depending on proprerties
next

Nothing mind boggling. Seems logical.

Now why this things in such a tool as SmallBasic when there are AutoCAD, Mapple, Mathcad, Maxima....
Because this works on an Android phone using a few megabytes...
And it is a fun to program it.
SimplePortal 2.3.6 © 2008-2014, SimplePortal