Page 1 of 1
fill array in one shot
Posted: Sun Jun 22, 2014 3:28 pm
by applePi
suggestion:
Code: Select all
dim a(4)
a(0)=1,2,3,4,5
debug a(2)
output: 3
Re: fill array in one shot
Posted: Sun Jun 22, 2014 3:30 pm
by STARGĂ…TE
Re: fill array in one shot
Posted: Sun Jun 22, 2014 3:34 pm
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
Re: fill array in one shot
Posted: Sun Jun 22, 2014 4:49 pm
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
Re: fill array in one shot
Posted: Sun Jun 22, 2014 10:08 pm
by davido
Very neat!
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.
Re: fill array in one shot
Posted: Sun Jun 22, 2014 10:48 pm
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.
Great idea, Davido. Thanks a lot!
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.
Re: fill array in one shot
Posted: Sun Jun 22, 2014 11:57 pm
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.
Re: fill array in one shot
Posted: Mon Jun 23, 2014 9:45 am
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
Re: fill array in one shot
Posted: Mon Jun 23, 2014 2:53 pm
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 !
I miss not being able to initialize an array when defining it.
Re: fill array in one shot
Posted: Mon Jun 23, 2014 3:10 pm
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
Re: fill array in one shot
Posted: Mon Jun 23, 2014 3:31 pm
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 !

Yes, it's not optimized for speed, but for elegance and lazy writing.

Re: fill array in one shot
Posted: Mon Jun 23, 2014 3:40 pm
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.

Re: fill array in one shot
Posted: Tue Jun 24, 2014 4:25 am
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.
Re: fill array in one shot
Posted: Tue Jun 24, 2014 3:44 pm
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.
Re: fill array in one shot
Posted: Tue Jun 24, 2014 7:41 pm
by davido
Hi Little John,
Thank you for sharing this very useful code.
Even better with multi-dimensional arrays in the latest update.
Now I see why you left the brackets in.
