Jump to content

Convert a string into 2D Array


Recommended Posts

Good evening :)
I was looking around the forum if I could find a function that allows to convert a string into a 2 dimensional array...
The first column of the array is always the same, but the rows could change...
I have a pattern like:

Column A|Column B
Static Text 1|Data 1
Static Text 2|Data 2
Static Text 3|Data 3
Static Text 4|Data 4

Where, Static Text (1...4) will be always the same, and I don't want to change them... But, Data 1...4 are dynamic fields... So, I could have the pattern above AND I coould have the pattern I'm going to show you right below :)

Column A|Column B
Static Text 1|Data 1
Static Text 2|Data 2
Static Text 3|Data 3
Static Text 4|Data 4
Static Text 1|Data 5
Static Text 2|Data 6
Static Text 3|Data 7
Static Text 4|Data 8

How can I do in this case?

Thanks for everyone's help :)


 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

That should be easy enough to accomplish.  One question I have is: where, and in what format, does the data come in (text file, string at a time, etc.)?  The clearer the requirements, the less re-work will be needed to shape the code to fit the data input.

 

Link to comment
Share on other sites

Hi @spudw2k:)
Thanks for asking.
The string comes from a For...In Loop, and it's in the string format...
So I do something like this:

 

For $oElement In $oQuery
    $sStringToArray &= $oElement.SomeData1 & _
                       $oElement.SomeData2 & _
                       $oElement.SomeDataN
Next

And the, I use StringSplit() to obtain the array...

Thanks for your help! :) 

 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Ok, so is the following produced after StringSplit?

Column A|Column B
Static Text 1|Data 1
Static Text 2|Data 2
Static Text 3|Data 3
Static Text 4|Data 4
So this ^ is already a single dimension array?

 

I am curious, what does the string look like before you perform the StringSplit, particularly what delimiter are you using?  Just trying to get a clear picture.

Edited by spudw2k
Link to comment
Share on other sites

Here is one fairly simple solution.

#include <MsgBoxConstants.au3>  ;$STR_NOCOUNT Const
#include <Array.au3>            ;_Array* Functions

Local $aArray[]=["Column A|Column B","Static Text 1|Data 1","Static Text 2|Data 2","Static Text 3|Data 3","Static Text 4|Data 4"]   ;Demo array to mimic dataset
_ArrayDisplay($aArray, "Starting Dataset")      ;Dataset look correct?

Local $a2DArr = StringSplit($aArray[0],"|",$STR_NOCOUNT)    ;Create new Array with first row of dataset above (Header)
_ArrayTranspose($a2DArr)    ;Convert to 2D array

For $iX = 1 To UBound($aArray)-1        ;Fill 2D Array
    _ArrayAdd($a2DArr,$aArray[$iX])
Next

_ArrayDisplay($a2DArr, "Populated 2D array")        ;Display filled 2D array

 

Link to comment
Share on other sites

  • Moderators

Just out of curiosity, why not move the logic up a level and use _ArrayAdd, like so:

;instead of
For $oElement In $oQuery
    $sStringToArray &= $oElement.SomeData1 & _
                       $oElement.SomeData2 & _
                       $oElement.SomeDataN
Next
...
StringSplit...
etc...

;Define your array ahead of time, then populate it through your For Loop
For $oElement In $oQuery
    _ArrayAdd($myArray, $oElement.SomeData1 & "|" & $oElement.SomeData2 & "|" & $oElement.SomeDataN
Next

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

can you show a couple of legit values? 

What are you splitting on?  There is no pipe or CR added during the loop you provided just somedataX smashed together for ubound number of times.

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

Link to comment
Share on other sites

@Luigi My first shot was like yours, but I opted to avoid using _ArrayDelete for the first (empty) row.  Doing some quick perf comparrisons, it doesn't seem to have major impact.  Good stuff.

Link to comment
Share on other sites

#include <array.au3>

local $aArray[0][2]

local $oQuery = ["Static Text 1" , "Data 1" , "Static Text 2" , "Data 2" , "Static Text 3" , "Data 3" ,"Static Text 4" , "Data 4"]

$flag = ""
$i = 0

For $oElement In $oQuery
    If $flag = 0 Then
        _ArrayAdd($aArray , $oQuery[$i]  , 0)
        $flag = 1
        $i+=1
        Else
        $aArray[ubound($aArray) - 1][1]=$oQuery[$i]
        $flag = 0
        $i+=1
    EndIf
Next

_ArrayDisplay($aArray)

 

I ask because it seems you could do it in the loop, if the return is any kind of reliable format.  I don't have a nice object example but the above array example should accurately reflect the thought

Edited by iamtheky

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

Link to comment
Share on other sites

Link to comment
Share on other sites

#include <array.au3>

Local $aArray[0][2]

Local $oQuery = ["Static Text 1", "Data 1", "Static Text 2", "Data 2", "Static Text 3", "Data 3", "Static Text 4", "Data 4", "IMPAR(ODD)"]


Local $iSize = UBound($oQuery, 1) - 1
If Not Mod($iSize, 2) Then
    _ArrayAdd($oQuery, "")
    $iSize += 1
EndIf


For $ii = 0 To $iSize Step 2
    _ArrayAdd($aArray, $oQuery[$ii] & "|" & $oQuery[$ii + 1], "|")
Next

_ArrayDisplay($aArray)

 

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

you need the additional conditions if using a For..In Loop, or you will blow the bounds

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

Link to comment
Share on other sites

Good evening guys :) Thanks everyone for the replies... Sorry for my late and not deyailed answer, but,natm, I'm sick ( a bad cold with some fever ), and with no Internet connection, so, thanks and sorry! As soon as I get better, I'll try to reply to you guys :) Again, a big thank you, and have a good day :)

Francesco :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • 3 weeks later...
On 14/4/2017 at 6:55 PM, JLogan3o13 said:

Just out of curiosity, why not move the logic up a level and use _ArrayAdd, like so:

;instead of
For $oElement In $oQuery
    $sStringToArray &= $oElement.SomeData1 & _
                       $oElement.SomeData2 & _
                       $oElement.SomeDataN
Next
...
StringSplit...
etc...

;Define your array ahead of time, then populate it through your For Loop
For $oElement In $oQuery
    _ArrayAdd($myArray, $oElement.SomeData1 & "|" & $oElement.SomeData2 & "|" & $oElement.SomeDataN
Next

 

Good evening @JLogan3o13!
In this case, how can I have a 2D array? Should I do something like this? :)

For $oElement In $oQuery
    _ArrayAdd($myArray, $sHeaderRow1 & "|" & $oElement.SomeData1 & @CRLF & _
                        $sHeaderRow2 & "|" & $oElement.SomeData2 & @CRLF & _
                        $sHeaderRowN & "|" & $oElement.SomeDataN
Next


Thank you :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators
6 minutes ago, FrancescoDiMuro said:

Should I do something like this? 

Why are you putting a carriage return in your elements? Have you tried it yourself, to determine if it comes out as you would expect?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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
 Share

×
×
  • Create New...