Sign in to follow this  
Followers 0
thepip3r

Is there a way to split a string into a 2-dimensional array given 2 separators?

4 posts in this topic

if i have a string that's formatted like:

$futureArray = "test1|||test2|||test3|||test4" & @CRLF

$futureArray &= "test5|||test6|||test7|||test8" & @CRLF

and I want to the output to be like:

array([test1,test2,test3,test4], [test5,test6,test7,test8])

is there some easy way to do this without specific array declarations as to the sizes of the array using like stringsplit? I've tried something like:

$arrCPL = StringSplit(StringSplit($strCPL, @CRLF), "|||")

...but that no worky. anyone know an easy way to accomplish this?


My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

StringSplit() returns an array, so it can't be a direct input to another StringSplit().

Do the first StringSplit() on @CRLF to get the rows.

At this point you can create the target array since you now know how many rows are needed.

Then walk through the array doing StringSplit() on each line with "|||" to get the columns.

For each column array, walk through the array and apply the data to the target array.

Example (add error checking):

#include <Array.au3> ; Only for _ArrayDisplay()

$futureArray = "test1|||test2|||test3|||test4" & @CRLF
$futureArray &= "test5|||test6|||test7|||test8" & @CRLF
$futureArray &= "test9|||test10|||test11|||test12" & @CRLF

$aRows = StringSplit(StringStripWS($futureArray, 2), @CRLF, 1)
Global $arrCPL[ $aRows[0] ][4] ; assumes there is always 4 columns
For $r = 1 To $aRows[0]
    $aCols = StringSplit($aRows[$r], "|||", 1)
    For $c = 1 To $aCols[0]
        $arrCPL[$r - 1][$c - 1] = $aCols[$c]
    Next
Next

_ArrayDisplay($arrCPL, "$arrCPL")

The "$r - 1" and "$c - 1" are so the target array will be 0-based.

;)

Edit: Made full demo.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

You can stringsplit to get rid of returns, the following is how i would turn the string into the 2D described.

#include <Array.au3>

$test = "test1|||test2|||test3|||test4|||test5|||test6|||test7|||test8"

$testARR = stringsplit($test , "|||" , 1)

$M = ubound($testARR) / 2

Dim $FINarray[$M][2]
$i = 1
For $k = 0 to $M - 1
$FINarray[$k][0] = $testARR[$i]
$FINarray[$k][1] = $testARR[$i + 1]
$i = $i + 2
Next


_ArrayDisplay ($FINarray)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

@Psalty:

In an attempt to make this reusable and a little more dynamic, I tried to create a function around the general logic of creating a multi-dimensional array from a string and this is what I have so far:

#include <Array.au3> ; Only for _ArrayDisplay()

$futureArray = "test1|||test2|||test3|||test4" & @CRLF
$futureArray &= "test5|||test6|||test7|||test8" & @CRLF
$futureArray &= "test9|||test10|||test11|||test12" & @CRLF


testMultiArrayFromString($futureArray, StringSplit("@CRLF,|||", ',', 2))

Func testMultiArrayFromString($strArray, $arrIndex) 
    If Not IsArray($arrIndex) Or Not IsString($strArray) Then
        MsgBox(0, 'Parameter Fail', "The 1st parameter must be a string and the 2nd parameter must be an array of the indexes to use to build the multi-dimensional array.")
        SetError(1)
    EndIf
    
    ; Build two dimensional array which will represent the number of dimensions for the final array
    Dim $arrTemp[Ubound($arrIndex)]
    For $i = 0 to Ubound($arrIndex) - 1
        $arrTemp[$i] = StringSplit($strArray, $arrIndex[$i], 2)
    Next
    
    ; Code for final multi-dimensional array where the depth is n 
    
    
    
        _ArrayDisplay($arrTemp)
EndFunc

I'm guessing there's a problem with the way I'm assigning the resulting array from StringSplit() to $arrTemp[$i]? I say that because _ArrayDisplay is showing 2 cols and 2 rows, both blank. Essentially what I was going to do if the the above code would've worked is Dim a $finalArray[ubound($arrTemp[0])]... but nm. As I'm writing this, I can't think of a way to dynamically Dim the multi-dimensional array even if the above code would've worked. I left the above post the way I was going to post it to ask for help just so you can see where my line of logic was going. Is there any way to make this work or am I wracking my brain over a fruitless endeavor?


My Additions:- RunAs AdminDeviant Fun:- Variable Sound Volume

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