Page 1 of 1

multiline and array

Posted: Mon Dec 12, 2011 10:06 pm
by xorc1zt
hi,

here are my two request

1. would be great if you can add a multi line statement for using the functions like this

Code: Select all

myFunction( 123,
            456,
            "abc",
            #PUREBASIC,
            variable)
2. a better method for putting value into a array. maybe like With/Endwith

Code: Select all

Dim vertices.i(20)

With vertices()
    0,1,2,3,
    4,5,6,7,
    8,9,0,9,
    8,7,6,5,
    4,3,2,1
Endwith
IMO this is a lot more easy and readable than the current method (especially with multidimensional array)

Code: Select all

Dim vertices.i(20)

vertices(0) = 0
vertices(1) = 1
vertices(2) = 2
vertices(3) = 3
vertices(4) = 4
vertices(5) = 5
vertices(6) = 6
vertices(7) = 7
vertices(8) = 8
vertices(9) = 9
[...]
Thank you for reading this,
bye.

Re: multiline and array

Posted: Mon Dec 12, 2011 10:39 pm
by Env
I can honestly see that causing problems with how I imagine PB parses source code.. Perhaps nesting the data in braces instead?

Re: multiline and array

Posted: Mon Dec 12, 2011 11:08 pm
by xorc1zt
every languages i learned allow these two things, even python is good with this. anything between brackets should be considered as a single line.

Re: multiline and array

Posted: Wed Dec 14, 2011 10:56 pm
by chris319
Does PB need another keyword? We can already do this:

Code: Select all

vertex.i = 12
How about this?

Code: Select all

Dim vertices.i(6) = 1, 2, 3, 4, 5, 6
The "=" (sort of) takes the place of your "with" keyword and an EOL or colon takes the place of your "endwith" keyword.

There is also this for simple cases:

Code: Select all

Dim vertices.i(20)
For count = 1 to 20
vertices(count) = count
Next

Re: multiline and array

Posted: Wed Dec 14, 2011 11:28 pm
by Env

Code: Select all

With/EndWith
already exist in PureBasic.. They basically allow you to short-hand access a variables functions... but either way, it would conflict with the original use of the keywords...

How about;

Code: Select all

Dim vertices.i(6) = (1, 2, 3, 4, 5, 6)
Surely that would be easier to integrate?

Re: multiline and array

Posted: Wed Dec 14, 2011 11:59 pm
by chris319
Env wrote: How about;

Code: Select all

Dim vertices.i(6) = (1, 2, 3, 4, 5, 6)
Surely that would be easier to integrate?
Why do you need the parentheses? The initialization values would be delimited by the "=" and a CR or LF character or a ":", viz.:

Code: Select all

Dim vertices.i(6) = 1, 2, 3, 4, 5, 6: Dim endpoints.i(6)
Just as we now have this with no parentheses:

Code: Select all

vertex.i = 6

Re: multiline and array

Posted: Thu Dec 15, 2011 12:29 am
by HeX0R
xorc1zt wrote: 2. a better method for putting value into a array. maybe like With/Endwith

Code: Select all

Dim vertices.i(20)

With vertices()
    0,1,2,3,
    4,5,6,7,
    8,9,0,9,
    8,7,6,5,
    4,3,2,1
Endwith
IMO this is a lot more easy and readable than the current method (especially with multidimensional array)
I would do it like this:

Code: Select all

Dim vertices.i(20)
CopyMemory(?_VERTICES, @vertices(), ?_VERTICES_END - ?_VERTICES)

DataSection
	_VERTICES:
	Data.i 0, 1, 2, 3
	Data.i 4, 5, 6, 7
	Data.i 8, 9, 0, 9
	Data.i 8, 7, 6, 5
	Data.i 4, 3, 2, 1
	_VERTICES_END:
EndDataSection

Re: multiline and array

Posted: Thu Dec 15, 2011 1:19 am
by xorc1zt
seriously, purebasic is the only one of the modern basic languages which is not allowing these two things

freebasic

Code: Select all

Dim myArray(1 To 5) As Integer => {1, 2, 3, 4, 5}
realbasic and gambas

Code: Select all

Dim i() as Integer
i = Array(1, 2, 3, 4, 5)
blitzmax

Code: Select all

Local int_array[]=[1,2,3,4,5]
powerbasic

Code: Select all

ARRAY ASSIGN x&() = 1,2,3,4,5,6,7,8,9,10
edit:
HeX0R wrote: I would do it like this:

Code: Select all

Dim vertices.i(20)
CopyMemory(?_VERTICES, @vertices(), ?_VERTICES_END - ?_VERTICES)

DataSection
	_VERTICES:
	Data.i 0, 1, 2, 3
	Data.i 4, 5, 6, 7
	Data.i 8, 9, 0, 9
	Data.i 8, 7, 6, 5
	Data.i 4, 3, 2, 1
	_VERTICES_END:
EndDataSection
here is a another way to use the data section (this one directly read and write the data section)

Code: Select all

Structure buffer
  StructureUnion
    int.i[0]
    float.f[0]
  EndStructureUnion
EndStructure
  
DataSection
  Arrayint:
  Data.i 1,2,3,4,5,6,7,8,9,10
  Data.i 9,8,7,6,5,4,3,2,1,0
  Arrayfloat:
  Data.f 1.0,1.2,1.3,1.4,1.5
EndDataSection


*my_bufferint.buffer = ?Arrayint

Debug *my_bufferint\int[0]
Debug *my_bufferint\int[8]
Debug *my_bufferint\int[12]
Debug *my_bufferint\int[15]
Debug *my_bufferint\int[19]

*my_bufferint\int[15] = 77
*my_bufferint\int[19] = 45

Debug *my_bufferint\int[15]
Debug *my_bufferint\int[19]






*my_bufferfloat.buffer = ?Arrayfloat

Debug *my_bufferfloat\float[0]
Debug *my_bufferfloat\float[1]
Debug *my_bufferfloat\float[2]
Debug *my_bufferfloat\float[3]
Debug *my_bufferfloat\float[4]
Debug *my_bufferfloat\float[5]

*my_bufferfloat\float[0] = 52.15785
*my_bufferfloat\float[1] = -454.1294
*my_bufferfloat\float[5] = (0.125+9.777)*0.7878

Debug *my_bufferfloat\float[0]
Debug *my_bufferfloat\float[1]
Debug *my_bufferfloat\float[5]

edit 2: this one should be easier to use

Code: Select all

DataSection
  Arrayint:
  Data.i 1,2,3,4,5,6,7,8,9,10
  Data.i 9,8,7,6,5,4,3,2,1,0
  Arrayfloat:
  Data.f 1.0,1.2,1.3,1.4,1.5
EndDataSection

Macro ArrayWriteI(address, slot, value)
  PokeI(address+(slot*4), value)
EndMacro

Macro ArrayReadI(address, slot)
  PeekI(address+(slot*4))
EndMacro


;-- test
Debug ArrayReadI(?Arrayint, 0)
Debug ArrayReadI(?Arrayint, 1)
Debug ArrayReadI(?Arrayint, 2)
Debug ArrayReadI(?Arrayint, 3)

ArrayWriteI(?Arrayint, 0, 4554)
ArrayWriteI(?Arrayint, 1, 1234)
ArrayWriteI(?Arrayint, 2, 8528)
ArrayWriteI(?Arrayint, 3, 9424)

Debug ArrayReadI(?Arrayint, 0)
Debug ArrayReadI(?Arrayint, 1)
Debug ArrayReadI(?Arrayint, 2)
Debug ArrayReadI(?Arrayint, 3)

Re: multiline and array

Posted: Sun Nov 28, 2021 1:13 pm
by Rinzwind
Yup, for something that wants to be more user-friendly than C it fails utterly in the array department.
Fred? Action time! How can you sleep at night knowing this? :twisted:

Inline array declaration & initialization, including of course passed as parameter to procedure. And add neat pointer arithmetic while you're at it.

Re: multiline and array

Posted: Fri Dec 17, 2021 11:02 pm
by eck49
following on Chris319's post of ten years ago (14 Dec 2011)

Why not

Code: Select all

Dim contents.i() = 1, 2, 3, 4, 5, 6
so that the () says it is an array and the compiler counts the values to define the size.

I think I would suggest that if there is a mismatch like

Code: Select all

Dim contents.i(55) = 1, 2, 3, 4, 5, 6
the compiler flags an error rather than (say) either (1) just initialising the first 6 elements or (2) cycling round the values until the array is full or (3) initialising all but the first 5 elements to 6

Re: multiline and array

Posted: Sat Dec 18, 2021 7:28 pm
by davido
I suggest that you look at this work-around by Little John:

https://www.purebasic.fr/english/viewto ... 49#p453449

Example:

Code: Select all

CreateArray(a$, "['dog', 'cat', 'mouse']").

Re: multiline and array

Posted: Sat Dec 18, 2021 10:57 pm
by eck49
Thanks davido, I already have a Macro for this purpose, but if the syntax I suggested were part of the native language it would be simpler and a natural extension.

This is, after all, in 'Feature Requests and Wishlists'.

But thanks for the thought.

Re: multiline and array

Posted: Sun Dec 19, 2021 12:29 am
by Rinzwind
davido wrote: Sat Dec 18, 2021 7:28 pm I suggest that you look at this work-around by Little John:

https://www.purebasic.fr/english/viewto ... 49#p453449

Example:

Code: Select all

CreateArray(a$, "['dog', 'cat', 'mouse']").
Ouch the (runtime) overhead of json for a basic compile time thing like this.

Should be part of the language syntax since day 1. Never understood why it's not there and it was never explained too.