ToArray() results
BlitzMax Forums/BlitzMax NG/ToArray() results
| ||
Hi, Could someone please take a look at the following code and tell me what I may possibly be doing wrong, as it does not appear to populate the word_array on windows for some reason. Thanks in advance SuperStrict Framework brl.standardio Import brl.linkedlist Local word_list:TList=CreateList() ListAddLast word_list,"One" word_list.addlast "Two" ListAddLast word_list,"Three" word_list.addlast "Four" Local word_array:String[]=String[](word_list.ToArray()) For Local word$=EachIn word_list Print word Next For Local word$=EachIn word_array Print word Next |
| ||
I don't think you can cast an object array to a string array like that, you can just do:SuperStrict Framework brl.standardio Import brl.linkedlist Local word_list:TList=CreateList() ListAddLast word_list,"One" word_list.addlast "Two" ListAddLast word_list,"Three" word_list.addlast "Four" Local word_array:Object[]=word_list.ToArray() For Local word$=EachIn word_list Print word Next For Local word$=EachIn word_array Print word Next |
| ||
Hi Pete, Thank you very much that worked a treat. I have been trying to convert as many things as possible using ng in order to better understand its ins and outs and was a little lost when trying to convert the following snippet http://www.blitzbasic.com/codearcs/codearcs.php?code=1338 Thanks in advance |
| ||
Hi, Almost getting there just must be something obvious that I am missing. I still need to use brl.retro as I can't seem to use string slicing to replace Mid$ just yet in order to return the desired results. Thanks in advance |
| ||
Kind of feels like the casting to string should work as it worked ok in blitzmax, so maybe it's a bug in NG? but in the meantime you could do something like: |
| ||
Slicing behaves a bit differently than Mid. First its Zero based instead of One based and allows out of bounds slicing which uses spaces in that case. Maybe this will help, the + and - for indices is to simulate the inputs from Mid, though you normally wouldnt type it like that. Local s:String = "Hello World!" Print Mid( s, 4, 6) + "|" Print s[4-1..4+6-1] + "|" Print Print Mid( s, -1, 6) + "|" Print s[-1+1..6-2] + "|" Print Print Mid( s, 8, 6) + "|" Print s[8-1..8+6-2] + "|" Print Print Mid( s, 1, 1) Print s[1-1..1] Notice the last one, where Mid uses position and size of 1, but slicing uses a range from 0 to 1. Actually, the Mid implementation uses slicing itself, so does Left/Right/LSet and RSet. |
| ||
Hi Pete, Thank you so very much that works rather brilliantly and I really do appreciate the insight. I wasn't sure if I was understanding the whole type casting concept correctly when returning the result from the function. Thank you as well Grable for that excellent explanation and your examples it is greatly appreciated, I did not realise that slicing was zero based hence why it was always returning different results. Thanks in advance |
| ||
Awesome so with slicing it becomes. Thank you again both very much, slowly getting there with my understanding of ng and loving it :) Thanks in advance |
| ||
Hi, just to add a little, it's possible to get a straight string array, but you need to extend TList to add a new method like in here http://www.blitzmax.com/Community/posts.php?topic=103368. -Henri |
| ||
Think it is only a problem for "string"-objects. If you have custom types then the cast should work. Am not sure but Brucey used that somewhere when improving my code. So I assume it works on NG too (the tests say "yes" at least). PS: When adding a "string" to the tword-list you will see that it indeed casts all the array entries to "tword" - interestingly it even seems to cast it "blindly". This leads to BlitzMax thinking of the 5th entry to be a valid "TWord" while it is just a memory address to a string. So we get a segfault at the end. (both, vanilla and ng, behave the same way here) bye Ron |