Deye

Data converted to 2DArray

10 posts in this topic

#1 ·  Posted (edited)

Hi,
A small exercise:
I need an StringRegExp command that will convert this data to a 2d array
With $STR_STRIPLEADING + $STR_STRIPTRAILING using delimiters of @CRLF and "="

Thanks

#include <Array.au3>
$aData = "  " & @CRLF & _
        "type= Button " & @CRLF & _
        "  name = " & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF

see "name = " has no value , will need the array in spite of that ( if possible )

Edited by Deye

Share this post


Link to post
Share on other sites



#2 ·  Posted

@Deye you have been around long enough to know that we follow more of a "Teach a man to fish" motto here, rather than you putting in a request and someone spoon-feeding the code to you. Have you tried anything based on the help file? If so, how about posting what you have attempted on your own, and where you're struggling?


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

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks

* what i have posted is what i'm interested to find out more about ..if possible
https://www.autoitscript.com/forum/topic/73382-stringsplit-with-multiple-delimiters/

for now:

1. the data is some data I'm receiving from the internet that looks like what I have posted
the array in this example shows up strange ..

2 ReDiming the array is where I'm stuck in this example as in my script
 

#include <Array.au3>

$aData = "type= Button " & @CRLF & _
        "  name = tt" & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF

$aData = StringStripWS($aData, 3)

$aArray = StringSplit($aData, @CRLF)

ReDim $aArray[UBound($aArray)][2]

For $i = 1 To UBound($aArray) - 1
    $aSplitted = StringSplit($aArray[$i], "=")
    $aArray[$i][0] = StringStripWS($aSplitted[1], 3)
    $aArray[$i][1] = StringStripWS($aSplitted[2], 3)
Next

_ArrayDisplay($aArray)

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

that's close enough:

fysa: when redimming, if you change the dimensions the original array is discarded

#include <Array.au3>
$sData ="type= Button " & @CRLF & _
        "  name =Button1 " & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF


$a1 = StringRegExp($sData , "(.*)=" , 3)
$a2 = StringRegExp($sData , "=(.*)" , 3)

local $a3[ubound($a1)][2]

for $i = 0 to ubound($a1) - 1
    $a3[$i][0] = stringstripws($a1[$i] , 3)
    $a3[$i][1] = stringstripws($a2[$i] , 3)
next

_ArrayDisplay($a3)

 

Edited by iamtheky
1 person likes this

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

53 minutes ago, iamtheky said:

fysa: when redimming, if you change the dimensions the original array is discarded

Derived of my knowledge of this, I thought that's an OK

#include <Array.au3>
Local $aWinList = WinList()
ReDim $aWinList[UBound($aWinList)][3]
_ArrayDisplay($aWinList)

 

Share this post


Link to post
Share on other sites

because winlist already returns a 2D array, you are just redefining the number of elements in the second dimension, and everything stays in tact.  

Whereas in the previous post you were adding a second dimension to a 1D array, which blows everything up and starts over with the new specifications.


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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

StringRegExp cannot return a 2D array.

You have to build a 2D array and fill it using a loop (like @iamtheky does)
There are some example on the forum to transform a 1D to a 2D (i remember _Array1DTo2D)

Here is another example, using the option 4 with StringRegExp :

#include <Array.au3>

$sData ="type= Button " & @CRLF & _
        "  name =Button1 " & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF

$aValues = StringRegExp($sData, "(\w+)\h*=\h*(\w+)", 4)
Local $aResult[UBound($aValues)][2]
For $i = 0 To UBound($aValues) - 1
    $aResult[$i][0] = ($aValues[$i])[1]
    $aResult[$i][1] = ($aValues[$i])[2]
Next
_ArrayDisplay($aResult)

 

Edited by jguinch

Share this post


Link to post
Share on other sites
#include <Array.au3>

$sData ="type= Button " & @CRLF & _
        "  name =Button1 " & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF

FileWrite(@tempdir & "\test.ini", "[data]" & @crlf & $sData)
$res = IniReadSection(@tempdir & "\test.ini", "data")
FileDelete(@tempdir & "\test.ini")
_ArrayDisplay($res)

:P

Share this post


Link to post
Share on other sites

#10 ·  Posted

Another method.

#include <Array.au3>

$sData = "type= Button 1 " & @CRLF & _
        "  name =  " & @CRLF & _
        "   value=Convert " & @CRLF & _
        "  start_X=23 " & @CRLF & _
        " start_Y = 31 " & @CRLF & _
        "Width = 63 " & @CRLF & _
        " Height = 45" & @CRLF

; Remove line leading and equal sign leading spaces, and, equal sign trailing and line trailing spaces, and, trailing @CRLF of $Data.
$sData = StringRegExpReplace($sData, "(?m)^\h+|\h+(?==)|(?<==)\h+|\h+$|\Z\v+", "") ; Or, pattern: "(?m)\h+(?==|$)|(?<==|^)\h+|\Z\v+"
; "\h+(?==)"  - matches the spaces followed by an equal sign - Positive lookahead assertion
; "(?<==)\h+" - matches the space/s that is/are preceded by an equal sign - Positive lookbehind assertion

Local $res[0][2]
_ArrayAdd($res, $sData, 0, "=")
_ArrayDisplay($res)

 

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