Jump to content

Data converted to 2DArray


Deye
 Share

Recommended Posts

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

  • Moderators

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

"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

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)

 

Link to comment
Share on other sites

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

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

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...