fill array in one shot

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

fill array in one shot

Post by applePi »

suggestion:

Code: Select all

dim a(4)
a(0)=1,2,3,4,5
debug a(2)
output: 3
Last edited by applePi on Sun Jun 22, 2014 3:32 pm, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: fill array in one shot

Post by STARGÅTE »

Why again?
Prefill Array
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: fill array in one shot

Post by applePi »

i have used this syntax in thinbasic interpreter, the arrays there begins with 1 not 0
so the syntax is a(1)=1,2,3,4
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: fill array in one shot

Post by Little John »

As I wrote in the old thread mentioned by Stargate, I would also appreciate this feature.

However, with the new JSON library in PB 5.30, it is now almost built-in:

//edit 2014-09-21: Code moved to the Tricks 'n' Tips section
Last edited by Little John on Sun Sep 21, 2014 12:27 pm, edited 3 times in total.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: fill array in one shot

Post by davido »

Very neat! :D


Just wondering. Could you join the two macros to make data entry simpler?
It shouldn't affect number arrays as there aren't any quotes to convert to double quotes.
DE AA EB
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: fill array in one shot

Post by Little John »

davido wrote:Just wondering. Could you join the two macros to make data entry simpler?
It shouldn't affect number arrays as there aren't any quotes to convert to double quotes.
Done. 8)

Great idea, Davido. Thanks a lot! :D

I also changed the order of the Macro parameters.
Now it looks more similar to a normal assignment, where the target variable is on the left side.
Additionally, the array name is always good visible now, even when there is a long list of elements.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: fill array in one shot

Post by rsts »

Very nice LJ. It's good to have someone who doesn't just talk about how they write good code, but also provides examples.

Thanks for posting.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: fill array in one shot

Post by davido »

Hi Little John,

Thank you, very much, for the update.


Would it be OK to 'unhinge' the brackets? Like this:

Code: Select all

EnableExplicit


Macro CreateArray (_array_, _content_)
   Define.i m_jArray = ParseJSON(#PB_Any, ReplaceString("[" + _content_ + "]", "'", #DQUOTE$))
   If m_jArray
      Dim _array_(0)
      ExtractJSONArray(JSONValue(m_jArray), _array_())
      FreeJSON(m_jArray)
   EndIf
EndMacro


; =====  DEMO  =====

Define.i last, i

CreateArray(a.i, "1, 3, 5, 7, 9")

last = ArraySize(a())
For i = 0 To last
   Debug a(i)
Next

Debug "-----"

CreateArray(x.d, "1.5, 3.4, 5.3, 7.2, 9.1")

last = ArraySize(x())
For i = 0 To last
   Debug StrD(x(i), 1)
Next

Debug "-----"

CreateArray(t$, "'dog', 'cat', 'mouse'")

last = ArraySize(t$())
For i = 0 To last
   Debug t$(i)
Next
DE AA EB
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: fill array in one shot

Post by luis »

rsts wrote:Very nice LJ. It's good to have someone who doesn't just talk about how they write good code, but also provides examples.
Anyone particular in mind ? I want names !

And yes, nice idea little john, thanks. Brutally inefficient but stylish ! :wink:

I miss not being able to initialize an array when defining it.
"Have you tried turning it off and on again ?"
A little PureBasic review
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: fill array in one shot

Post by PB »

Short and easy on the eyes/typing:

Code: Select all

Macro MakeArray(name,items)
  For i=1 To CountString(items,",")+1
    name(i-1)=Val(StringField(items,i,","))
  Next
EndMacro

Dim a(4)
MakeArray(a,"0,1,2,3,4")

For n=0 To 4
  Debug a(n)
Next
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: fill array in one shot

Post by Little John »

Hi Davido, rsts, and Luis,

thanks for your kind words! You are welcome.
davido wrote:Would it be OK to 'unhinge' the brackets?
Yes, it would. I can't imagine that doing so could pose any problem.
luis wrote:Brutally inefficient but stylish ! :wink:
Yes, it's not optimized for speed, but for elegance and lazy writing. 8)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: fill array in one shot

Post by Little John »

PB wrote:Short and easy on the eyes/typing:
I agree.

However, that way you need at least 3 different macros for different types of arrays:
  • one macro without any Val*() function for string arrays
  • one macro with ValD()
  • one macro with Val(),
    because ValD() (and also ValF()) does not handle big Quad numbers correctly (see code example below)

Code: Select all

; PB 5.22 LTS x86 on Windows

Define s$
Define.q a, b, c

s$ = "9223372036854775100"  ; a valid quad number
a = ValD(s$)
b = ValF(s$)
c = Val (s$)
Debug a      ; =>  9223372036854774784  (wrong)
Debug b      ; => -9223372036854775808  (wrong)
Debug c      ; =>  9223372036854775100  (correct)
The trick with my suggestion is, that the job is done for different types of arrays by a single macro. :-)
Last edited by Little John on Tue Jun 24, 2014 12:06 pm, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: fill array in one shot

Post by applePi »

thats make the life easier Little John thank you. especially when we have many different short arrays which needs to be filled with short lists of items.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: fill array in one shot

Post by Little John »

Hi applePi, you are welcome.

I've slightly changed the code (in my first post in this thread), so that the macro now also can create multi-dimensional arrays. :-)
When someone needs more than 2 dimensions, s/he just needs to add the appropriate 'CompilerElseIf' conditions to the macro.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: fill array in one shot

Post by davido »

Hi Little John,

Thank you for sharing this very useful code.
Even better with multi-dimensional arrays in the latest update. :D

Now I see why you left the brackets in. :oops:
DE AA EB
Post Reply