Jump to content
FrancescoDiMuro

Convert a string into 2D Array

Recommended Posts

FrancescoDiMuro

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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
spudw2k

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.

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
spudw2k

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

Share this post


Link to post
Share on other sites
spudw2k

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

 

Share this post


Link to post
Share on other sites
JLogan3o13

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

 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
spudw2k
iamtheky

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.


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

Share this post


Link to post
Share on other sites
spudw2k

@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.

Share this post


Link to post
Share on other sites
iamtheky
#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
  • Like 1

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

Share this post


Link to post
Share on other sites
spudw2k

@iamtheky I didn't know you could declare an array like that ($aArray[0][2]), pretty cool.

@Luigi That ^ would eliminate the need for the _ArrayDelete

Share this post


Link to post
Share on other sites
Luigi
#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

Share this post


Link to post
Share on other sites
iamtheky

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


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

Share this post


Link to post
Share on other sites
Luigi
35 minutes ago, iamtheky said:

local $aArray[0][2]

 

I did not know this was possible ! WOW!

I learned one more  today.
Edited by Luigi

Visit my repository

Share this post


Link to post
Share on other sites
Luigi
4 minutes ago, iamtheky said:

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

I thinking about this... if the size is multiple of 2, I believe have no problem... But, if not... There is a problem.

 


Visit my repository

Share this post


Link to post
Share on other sites
FrancescoDiMuro

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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
JLogan3o13
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?


√-1 2^3 ∑ π, and it was delicious!

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

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
×