multiline and array

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

multiline and array

Post 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.
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: multiline and array

Post by Env »

I can honestly see that causing problems with how I imagine PB parses source code.. Perhaps nesting the data in braces instead?
Thanks!
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: multiline and array

Post 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.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: multiline and array

Post 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
Env
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 27, 2010 3:20 pm
Location: Wales, United Kingdom

Re: multiline and array

Post 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?
Thanks!
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: multiline and array

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: multiline and array

Post 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
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: multiline and array

Post 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)
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: multiline and array

Post 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.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: multiline and array

Post 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
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: multiline and array

Post 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']").
DE AA EB
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: multiline and array

Post 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.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: multiline and array

Post 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.
Post Reply