Extended array definition

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Lunasole
Addict
Addict
Posts: 984
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Extended array definition

Post by Lunasole »

Suspect this already was proposed sometime by someone, but at least my thread will be another vote ^^
Would be cool to have some short form of predefining arrays (like variables).

I think something like this is nice:

Code: Select all

; predefined array in short form
Dim Dat.l (8) = 2,5,1,2,3,4,7,7,6
Should be expanded as

Code: Select all

Dim Dat.l (8)
Dat(0) = 2
Dat(1) = 5
Dat(2) = 1
Dat(3) = 2
Dat(n) ...
Dat(8) = 6

Close to how "Data" keyword works, working with strings too; for multidimensional/structured arrays should not be applicable.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
davido
Addict
Addict
Posts: 1842
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Extended array definition

Post by davido »

@Lunasole,

Have a look at this post by Little John, it achieves such a definition with a macro using JSON.

http://www.purebasic.fr/english/viewtop ... 49#p453449
DE AA EB
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Extended array definition

Post by DontTalkToMe »

It was asked so many times... but unfortunately it's still missing.

You can look at all the limited and convoluted contraptions we have to go through instead here

http://www.purebasic.fr/english/viewtop ... 13&t=60372
User avatar
idle
Addict
Addict
Posts: 3637
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Extended array definition

Post by idle »

simple work around for arrays of a single type

Code: Select all

Structure Ar   ;generic 1D array itterator 
  StructureUnion 
    a.a[0]
    b.b[0]
    c.c[0]
    u.u[0]
    w.w[0]
    l.l[0]
    f.f[0]
    i.i[0]
    q.q[0]
    d.d[0]
  EndStructureUnion 
  s.s[0]
EndStructure   

Structure MyPoint 
  x.i
  y.i
EndStructure   

Structure MyPointAR ;a structured itterator 
  e.myPoint[0]
EndStructure   


DataSection   ;datasection can be declared anywhere 
QuadArray:  
Data.q 1,2,3,4,5,6 
ByteArray:
Data.b 9,8,7,6,5,4 
FloatArray:
Data.f 1.1,1.2,1.3,1.4,1.5,1.6
MyPointArray:  ;x,y 
Data.i 1,6,2,5,3,4,4,3,5,2,6,1 
StringArray: 
Data.i @"One",@"Two",@"Three",@"Four",@"Five",@"Six"
EndDataSection 

*myarray.AR = ?QuadArray ;set address of typed itterator pointer 

For a = 0 To 5 
  Debug *myarray\q[a] 
Next   
Debug "-----"

*myarray.AR = ?ByteArray 

For a = 0 To 5 
  Debug *myarray\b[a] 
Next   
Debug "-----"

*myarray.AR = ?FloatArray 

For a = 0 To 5 
  Debug *myarray\f[a] 
Next   
Debug "-----"

*mypoint.MyPointAR = ?MyPointArray 

For a = 0 To 5 
  Debug Str(*mypoint\e[a]\x) + "," + Str(*mypoint\e[a]\y)
Next   
Debug "-----"

*myarray.AR = ?QuadArray 

For a = 0 To 5 
  *myarray\q[a] = 6-a  ;overwrite it  
Next   

For a = 0 To 5 
  Debug *myarray\q[a] 
Next   
Debug "-----" 

*myarray.AR = ?StringArray 

For a = 0 To 5 
  Debug *myarray\s[a] 
Next   

DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Extended array definition

Post by DontTalkToMe »

Emphasis on simple !

Not to discredit the workarounds, they are all nice ideas in their own way, it's just they are so convoluted, depressing to type and to read later compared to what you should be able to do in the first place.

Just think when you have to initialize simple vectors for points or colors in game or graphic programming.

Would be so nice to be able to do

Code: Select all

Dim vec.f() = (1.0, 2.0, 3.0) 
or

Code: Select all

Dim vec.f(2) = (1.0, 2.0, 3.0) 
if the precedent is "too difficult" for the compiler

resulting in

debug vec(0) = 1.0
...
debug vec(2) = 3.0

Lunasole wrote:for multidimensional/structured arrays should not be applicable.
why not ? at least for multidimensional arrays

Code: Select all

Dim mat.f() = (1.0, 2.0, 3.0) (4.0, 5.0, 6.0) (7.0, 8.0, 9.0)  or Dim mat.f() = (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0) 
or

Code: Select all

Dim mat.f(2,2) = (1.0, 2.0, 3.0) (4.0, 5.0, 6.0) (7.0, 8.0, 9.0) or Dim mat.f(2,2) = (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0) 
if the precedent is "too difficult" for the compiler

resulting in (row major order)

debug mat(0,0) = 1.0
debug mat(0,1) = 2.0
debug mat(0,2) = 3.0
debug mat(1,0) = 4.0
debug mat(1,1) = 5.0
debug mat(1,2) = 6.0
debug mat(2,0) = 7.0
debug mat(2,1) = 8.0
debug mat(2,2) = 9.0

All the above while keeping the support for the Static keyword to initialize an array only once inside a procedure if possible.

Simple but useful enhancements to the language are much more needed IMO than another library and this one in particular should be extremely easy to add for the predefined data types.
User avatar
mk-soft
Addict
Addict
Posts: 3110
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Extended array definition

Post by mk-soft »

Perhaps with Macro

Code: Select all

EnableExplicit

Macro MyDim(name, size, v0=0, v1=0, v2=0, v3=0, v4=0, v5=0, v6=0, v7=0, v8=0, v9=0, v10=0, v11=0, v12=0, v13=0, v14=0, v15=0, v16=0, v17=0, v18=0, v19=0)
  Dim name(size)
  CompilerIf v0 : name(0) = v0 : CompilerEndIf
  CompilerIf v1 : name(1) = v1 : CompilerEndIf
  CompilerIf v2 : name(2) = v2 : CompilerEndIf
  CompilerIf v3 : name(3) = v3 : CompilerEndIf
  CompilerIf v4 : name(4) = v4 : CompilerEndIf
  CompilerIf v5 : name(5) = v5 : CompilerEndIf
  CompilerIf v6 : name(6) = v6 : CompilerEndIf
  CompilerIf v7 : name(7) = v7 : CompilerEndIf
  CompilerIf v8 : name(8) = v8 : CompilerEndIf
  CompilerIf v9 : name(9) = v9 : CompilerEndIf
  CompilerIf v10 : name(10) = v10 : CompilerEndIf
  CompilerIf v11 : name(11) = v11 : CompilerEndIf
  CompilerIf v12 : name(12) = v12 : CompilerEndIf
  CompilerIf v13 : name(13) = v13 : CompilerEndIf
  CompilerIf v14 : name(14) = v14 : CompilerEndIf
  CompilerIf v15 : name(15) = v15 : CompilerEndIf
  CompilerIf v16 : name(16) = v16 : CompilerEndIf
  CompilerIf v17 : name(17) = v17 : CompilerEndIf
  CompilerIf v18 : name(18) = v18 : CompilerEndIf
  CompilerIf v19 : name(19) = v19 : CompilerEndIf
EndMacro

Global MyDim(daten.f, 3, 200.6, 300.3, 99.1)

Define index
For index = 0 To ArraySize(Daten())
  Debug daten(index)
Next

CallDebugger
No Strings :(
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
Little John
Addict
Addict
Posts: 4000
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Extended array definition

Post by Little John »

mk-soft wrote:No Strings :(
This works also with strings: :D
http://www.purebasic.fr/english/viewtop ... 65#p487965
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
User avatar
Lunasole
Addict
Addict
Posts: 984
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Extended array definition

Post by Lunasole »

DontTalkToMe wrote: why not ? at least for multidimensional arrays
I think it will look dirty for multidimensional arrays. Also they are used much less often than simple arrays.
But generally I'm not against that, just this is secondary and let Fred first do something for plain arrays at least



Btw you said nicely about workarounds. They are OK, but not usable in any case you want or need. There is a big difference between just opening IDE and using one keyword to do something or messing before this with extra code to do the same trivial thing at last.
Surely can also write custom precompiler for more automation, but that brings additional limits and for example makes problems with further code sharing.


> Simple but useful enhancements to the language are much more needed IMO than another library and this one in particular should be extremely easy to add for the predefined data types.

And this is right too, usability (or maybe there is some better word which I can't pick right now^^) is probably main PB advantage (it simply almost has no others against modern languages) and syntax is key part of it. Well, attached libraries are another key part :) But libraries keep growing greatly for last years, syntax evolution -- not, few small changes like string escaping are nice and marking some progress, but still missing lot of trivial stuff implemented in most languages and IDEs years ago.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Extended array definition

Post by DontTalkToMe »

Lunasole wrote: Surely can also write custom precompiler for more automation, but that brings additional limits and for example makes problems with further code sharing.
Also PB does not offer anything to help writing a precompiler, see for example the LINE directive in C languages used to get the error on the right line on the pre-processed source file (and so meaningful) instead of the line in the post-processed source file saw by the compiler.

https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html

This also would be trivial to add (should be added tomorrow really), and would help a lot.

A preprocessor has other problems. Just think about debugging. And most of all as you mentioned, your code becomes something else and not PB anymore, so no sharing.
Lunasole wrote: But libraries keep growing greatly for last years, syntax evolution -- not, few small changes like string escaping are nice and marking some progress, but still missing lot of trivial stuff implemented in most languages and IDEs years ago.
Same here.

Most trivial things could be implemented without fears of breaking something else and would result in a language a lot more comfortable to use. No idea why Fred is not considering these.

Array initialization is just one of them.
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: Extended array definition

Post by sys64802 »

Is this going to be finally added ?

I'm too tired to struggle with these things every day. I did it on the side because I was hopeful the language was going to be enhanced just a little but in almost 2 years nothing really happened.

Working with math related stuff in this language is becoming unbearable for me. Too archaic, too limited. This is just the tip of the iceberg but at least this should be added today :|

I understand the error was mine, adoption in the hope of things to come. Bad idea.
There were no milestones announced and no promises about any evolution.
I think it's better if I forget about the experiment and move on.
Cheers.
User avatar
luis
Addict
Addict
Posts: 3706
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy
Contact:

Re: Extended array definition

Post by luis »

This would be useful.
Starting holding breath ... now.
User avatar
HeX0R
Enthusiast
Enthusiast
Posts: 660
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Extended array definition

Post by HeX0R »

R. I. P. Luis :evil:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Extended array definition

Post by Dude »

My simple solution for both numbers and strings:

Code: Select all

Macro MakeNumericArray(name,items)
  n=CountString(items,",")+1
  Dim name(n)
  For i=1 To n
    name(i)=Val(StringField(items,i,","))
  Next
EndMacro

Macro MakeStringArray(name,items)
  n=CountString(items,",")+1
  Dim name(n)
  For i=1 To n
    name(i)=StringField(items,i,",")
  Next
EndMacro

MakeNumericArray(num,"1,2,3,4,5,6,7")
For n=1 To ArraySize(num())
  Debug num(n)
Next

MakeStringArray(txt$,"one,two,three")
For n=1 To ArraySize(txt$())
  Debug txt$(n)
Next
firace
Enthusiast
Enthusiast
Posts: 699
Joined: Wed Nov 09, 2011 8:58 am

Re: Extended array definition

Post by firace »

Dude wrote:My simple solution for both numbers and strings:

Code: Select all

Macro MakeNumericArray(name,items)
  n=CountString(items,",")+1
  Dim name(n)
  For i=1 To n
    name(i)=Val(StringField(items,i,","))
  Next
EndMacro

Macro MakeStringArray(name,items)
  n=CountString(items,",")+1
  Dim name(n)
  For i=1 To n
    name(i)=StringField(items,i,",")
  Next
EndMacro

MakeNumericArray(num,"1,2,3,4,5,6,7")
For n=1 To ArraySize(num())
  Debug num(n)
Next

MakeStringArray(txt$,"one,two,three")
For n=1 To ArraySize(txt$())
  Debug txt$(n)
Next
Nice tip, thanks for sharing :)
User avatar
skywalk
Addict
Addict
Posts: 3532
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Extended array definition

Post by skywalk »

HeX0R wrote:R. I. P. Luis :evil:
:lol: :lol:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply