January 19, 2020, 03:26:52 AM

Author Topic: Am I crazy? Didn't this used to work? (list to array)  (Read 1581 times)

Online GW

  • Full Member
  • ***
  • Posts: 173
Am I crazy? Didn't this used to work? (list to array)
« on: July 15, 2017, 06:19:45 AM »
Converting a list to an array.  Am I having a senior moment?
Code: BlitzMax
  1.  
  2. SuperStrict
  3. Framework brl.basic
  4.  
  5.  
  6. Global sList:TList = CreateList()
  7.  
  8.  
  9. '// Fill a list with strings
  10. For Local i% = 0 Until 200
  11.         sList.Addlast( String(Rand(9999)) )
  12. Next
  13.  
  14.  
  15. '// Create an array from the list
  16. Global sArr$[] = String[]( sList.toArray() )
  17.  
  18.  
  19. '// string array is null!??
  20. DebugStop
  21.  

Offline degac

  • Full Member
  • ***
  • Posts: 123
    • graphio.net
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #1 on: July 15, 2017, 08:38:52 AM »
No, your are not alone :)

Code: BlitzMax
  1. SuperStrict
  2. Framework brl.basic
  3.  
  4.  
  5. Global sList:TList = CreateList()
  6.  
  7.  
  8. '// Fill a list with strings
  9. For Local i% = 0 Until 200
  10.         sList.Addlast( String(Rand(9999)) )
  11. Next
  12.  
  13.  
  14. '// Create an array from the list
  15. Global oArr:Object[]=Object[]( sList.toArray() )
  16. Global sArr$[]
  17. sArr=String[](ListToArray(sList))
  18.  
  19. 'Arr=ListToArray(sList)
  20.  
  21.  
  22. If sArr=Null
  23.         Print "sArr is null"
  24.  
  25. End If
  26.  
  27. 'object convers
  28. If oArr=Null
  29.         Print "oArr is null"
  30.  
  31. End If
  32.  

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)
If there's a problem, there's at least one solution.
www.blitzmax.org

Offline Henri

  • Full Member
  • ***
  • Posts: 217
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #2 on: July 15, 2017, 12:05:51 PM »
Hi,

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

Code: [Select]
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
- Got 01100011 problems, but the bit ain't 00000001

Offline Brucey

  • Jr. Member
  • **
  • Posts: 93
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #3 on: July 15, 2017, 04:39:23 PM »
This is how you might do it with the new NG collections module...
Code: BlitzMax
  1. SuperStrict
  2. Framework brl.basic
  3. Import brl.collections
  4.  
  5. Global sList:TArrayList<String> = New TArrayList<String>
  6.  
  7. '// Fill a list with strings
  8. For Local i% = 0 Until 200
  9.         sList.Add( String(Rand(9999)) )
  10. Next
  11.  
  12. '// Create an array from the list
  13. Global sArr$[] = sList.toArray()
  14.  
  15. '// string array is null!??
  16. DebugStop
  17.  
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.

Offline Henri

  • Full Member
  • ***
  • Posts: 217
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #4 on: July 15, 2017, 07:08:48 PM »
Hmm...where is the TArrayList defined ?

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Online GW

  • Full Member
  • ***
  • Posts: 173
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #5 on: July 15, 2017, 07:58:51 PM »
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
  1. SuperStrict
  2.  
  3. Framework brl.basic
  4.  
  5.  
  6. Type tTestType
  7.    Field a#=1
  8. End Type
  9.  
  10.  
  11. Global List:TList =CreateList()
  12. Global Array:tTestType[]
  13.  
  14.  
  15. For Local i% = 0 Until 200
  16.    list.addlast( New tTestType)
  17. Next
  18.  
  19.  
  20. Array = tTestType[]( list.ToArray() )
  21. DebugStop
  22. Print "Done: It works!"
  23.  
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.

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 410
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #6 on: July 15, 2017, 09:54:58 PM »
Quote
Hmm...where is the TArrayList defined ?
You need to read more carefully Henri. :P
Code: [Select]
Import brl.collectionsSo 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

Offline Brucey

  • Jr. Member
  • **
  • Posts: 93
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #7 on: July 15, 2017, 10:25:58 PM »
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?

Online GW

  • Full Member
  • ***
  • Posts: 173
Re: Am I crazy? Didn't this used to work? (list to array)
« Reply #8 on: July 16, 2017, 01:05:05 AM »
I'll try to come up with an example.  In most situations i'm loading 500k objects from a file or sql db.