SyntaxBomb - Indie Coders

Languages & Coding => BlitzMax / BlitzMax NG => Topic started by: GW on July 15, 2017, 06:19:45

Title: Am I crazy? Didn't this used to work? (list to array)
Post by: GW on July 15, 2017, 06:19:45
Converting a list to an array.  Am I having a senior moment?
Code (blitzmax) Select


SuperStrict
Framework brl.basic


Global sList:TList = CreateList()


'// Fill a list with strings
For Local i% = 0 Until 200
sList.Addlast( String(Rand(9999)) )
Next


'// Create an array from the list
Global sArr$[] = String[]( sList.toArray() )


'// string array is null!??
DebugStop
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: degac on July 15, 2017, 08:38:52
No, your are not alone :)

Code (blitzmax) Select
SuperStrict
Framework brl.basic


Global sList:TList = CreateList()


'// Fill a list with strings
For Local i% = 0 Until 200
        sList.Addlast( String(Rand(9999)) )
Next


'// Create an array from the list
Global oArr:Object[]=Object[]( sList.toArray() )
Global sArr$[]
sArr=String[](ListToArray(sList))

'Arr=ListToArray(sList)


If sArr=Null
Print "sArr is null"

End If

'object convers
If oArr=Null
Print "oArr is null"

End If


It seems ListToArray() (or method) cast only to an 'OBJECT ARRAY'
Then you need to re-cast it manually (or via a second loop to create your string array)
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: Henri on July 15, 2017, 12:05:51
Hi,

I find that the easiest way is to create your own TList:


Strict


'Testing
Local list:MyList = New MyList

For Local i:Int = 0 To 10
list.addlast( String(i) )
Next

Local ar:String[] = list.ToStringArray()

For Local ii:Int = 0 Until ar.length
Print ar[ii]
Next


Type MyList Extends TList

Rem
bbdoc: convert a list to an string array
returns: An array of strings
End Rem
Method ToStringArray:String[]()
Local arr:String[Count()],i
Local link:TLink=_head._succ
While link<>_head
arr[i]=String(link._value)
link=link._succ
i:+1
Wend
Return arr
End Method
EndType



-Henri
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: Brucey on July 15, 2017, 16:39:23
This is how you might do it with the new NG collections module...
Code (blitzmax) Select

SuperStrict
Framework brl.basic
Import brl.collections

Global sList:TArrayList<String> = New TArrayList<String>

'// Fill a list with strings
For Local i% = 0 Until 200
        sList.Add( String(Rand(9999)) )
Next

'// Create an array from the list
Global sArr$[] = sList.toArray()

'// string array is null!??
DebugStop

Note that sList is a list that only stores Strings (as specified in the variable declaration). When you call ToArray(), you don't need to cast, as BlitzMax will only return a String array from this list.
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: Henri on July 15, 2017, 19:08:48
Hmm...where is the TArrayList defined ?

-Henri
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: GW on July 15, 2017, 19:58:51
hmm. I was sure this used to work.  But I now understand some of my confusion.
List casting works for types but not for strings.
Code (blitzmax) Select
SuperStrict

Framework brl.basic


Type tTestType
   Field a#=1
End Type


Global List:TList =CreateList()
Global Array:tTestType[]


For Local i% = 0 Until 200
   list.addlast( New tTestType)
Next


Array = tTestType[]( list.ToArray() )
DebugStop
Print "Done: It works!"

Thanks for the assist guys!


Brucey: I would love to use NG for my current project, but I have the all too common problem of the GC blowing up when I allocate many objects.
About half of my projects are forced to stay with classic bmx because of this. But i would really love to go with NG full time.
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: markcwm on July 15, 2017, 21:54:58
QuoteHmm...where is the TArrayList defined ?
You need to read more carefully Henri. :P
Import brl.collections
So it's a BRL module and as Brucey stated it's new for NG (since July 13).
https://github.com/bmx-ng/brl.mod/tree/master/collections.mod
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: Brucey on July 15, 2017, 22:25:58
Quote from: GW on July 15, 2017, 19:58:51
Brucey: I would love to use NG for my current project, but I have the all too common problem of the GC blowing up when I allocate many objects.
About half of my projects are forced to stay with classic bmx because of this. But i would really love to go with NG full time.

I've never had this issue, although I understand the reasons for it. I would suggest you may have a problem with the design of your program, in that there may be ways to perform the same function without creating so many object instances.
Would you happen to have any examples of a smallish program that I may look at please, as I might be able to provide you with improvements that could get around the issue?
Title: Re: Am I crazy? Didn't this used to work? (list to array)
Post by: GW on July 16, 2017, 01:05:05
I'll try to come up with an example.  In most situations i'm loading 500k objects from a file or sql db.