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!

How to get your question answered on this forum!

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!

How to get your question answered on this forum!

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

    • lattey
      By lattey
      hi,
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
       
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
       
      What is the best approach to accomplishing this method?
             
    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
×