Sign in to follow this  
Followers 0
forger

Use Stringsplit For 2-dimensional Arrays

7 posts in this topic

Hello again :)

Can I use StringSplit for arrays 5 by 2 ?

I mean:

$list = StringSplit("1 thing,2 things,3 things",",")

This would create automatically an array:

$list[0] = 3
 $list[1] = "1 thing"
 $list[2] = "2 things"
 $list[3] = "3 things"

How can I use stringsplit again to make an array 5 by 2:

$list[0][0] = Total items
  $list[1][0] = "Something"
  $list[1][1] = "Something else"

  $list[2][0] = "Something"
  $list[2][1] = "Something else"

  $list[3][0] = "Something"
  $list[3][1] = "Something else"

  $list[4][0] = "Something"
  $list[4][1] = "Something else"

  $list[5][0] = "Something"
  $list[5][1] = "Something else"

Something similar to ProcessList() I believe

I hope it's possible 'cause it would save me a lot of lines :mellow:

Can someone please provide me with the format of the StringSplit for this, if possible?

Share this post


Link to post
Share on other sites



hi,

I did a UDF once for this, though it may buggy; examples of usage at;

Array2D.au3, A try to simplify 2D Array management and views

But Arrrays of arrays are slow if large; I always meant to re-write it with just arrays of delimited strings; perhaps a project for you?.....

eg;

;Array2DExample4.au3

#include-once

#include <Array2D.au3>

Dim $ar1_Array_Arrays[4]; base 0 for Array of arrays; you can add empty zero row later if needed

;Use either _ArrayCreate or _Stringsplit_B0 or StringSplit to make the 1D Array rows

$ar1_Array_Arrays[0] = _ArrayCreate('animal names', 'aardvark', 'badger','beaver','cat') ; this is always strings starting at zero; base "0"

; OR ; SYNTAX; _StringSplit_B0($s_String,[$s_Delimiter="|",[$i_Flag="0"]]); splits array to base zero (no index at [0])

$ar1_Array_Arrays[1] = _Stringsplit_B0('plant names|Tree|flower|grass|petunia'); else "StringSplit" has strings starting at 1; base "1"

$ar1_Array_Arrays[2] = _Stringsplit_B0('people names|Anne|Joanne|Joan|Cherry')

$ar1_Array_Arrays[3] = _Stringsplit_B0('car names|holden|ford|volkswagon|mitsubishi')

$ar2_Array=_Array2DCreateFromArray($ar1_Array_Arrays )

;OR; .... if you already have named arrays $ar1_Array1,... or whatever, use _ArrayCreate to create your Array of Arrays

;$ar1_Array_Arrays=_ArrayCreate($ar1_Array1,$ar1_Array2,$ar1_Array3,$ar1_Array4,.........)

; Params; $i_RowStart//$i_ColumnStart (zero or 1 for first row//Column to display)

;SYNTAX_ArrayViewText($ar2_Array, 'Title', $i_RowStart, $i_ColumnStart, $i_ZeroRowAsHeader,$Transpose, $Width, $Height, $Left, $Right)

_ArrayViewText($ar2_Array, 'Display', 0,0)

Best, randall

Share this post


Link to post
Share on other sites

Probably not what your after, but you could do somthing like this:

#include <Array.au3>
$str = "11;21,22;31,32,33"
$arr1 = Stringsplit($str, ";")
_ArrayDisplay($arr1, "First splitt")
dim $arrRet[1]
Redim $arrRet[UBound($arr1)]
For $i = 1 to $arr1[0]
   $arrRet[$i] = StringSplit($arr1[$i], ",")
Next
Dump($arrRet)
Exit
Func Dump(ByRef $arr)
    If IsArray($arr) Then
        For $i = 0 to Ubound($arr) - 1
                _ArrayDisplay( $arr[$i], "Array in item " & $i)
        Next 
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

randallc, looks like Uten is faster than me :mellow:

You got the concept I wanted Uten, thanks!

Damn, and I started thinking about it :)

I didn't know about UBound, I'll have to read a bit more about arrays I guess.

Edited by forger

Share this post


Link to post
Share on other sites

And here is the function:

Func _Create2DArray($_2DString,$_Array1Chr,$_Array2Chr)
    If IsNumber($_Array1Chr) Then
        $_Array1Chr = Chr($_Array1Chr)
    EndIf
    If IsNumber($_Array2Chr) Then
        $_Array2Chr = Chr($_Array2Chr)
    EndIf
    Local $_Array1 = Stringsplit($_2DString, $_Array1Chr), $ArrayRet[1]
    ReDim $ArrayRet[UBound($_Array1)]
    For $i = 1 to $_Array1[0]
        $ArrayRet[$i] = StringSplit($_Array1[$i], $_Array2Chr)
    Next
    Return $ArrayRet
EndFunc

;Example, you need to include <Array.au3> to display the string
$test = _Create2DArray("11.21,22.31,32,33",46,44)

#include <Array.au3>
MsgBox("","",_ArrayToString($test[1],","))

Thanks for the help guys! (or girls, I dunno :think:)

Share this post


Link to post
Share on other sites

Hi,

I see what you want; but to be clear, it might be worth re-naming your function to "_Create_ArrayOfArrays", as you have not yet made it into a 2D array?...

I still think you might be better leaving this part as Array of Strings, anyway..... [if you ever want to use it to convert back and forward between large 2D arrays, that might be quicker]

Best, Randall

Share this post


Link to post
Share on other sites

I must've mistook the "dimension" explanation, sorry! Just renamed it: _CreateArrayFromStr

Array of Strings sounds like Lord of the Rings :think: Too catchy :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0