Jump to content

Create array from an in string & (SRER )


Deye
 Share

Go to solution Solved by iamtheky,

Recommended Posts

Hi,

what is a way to evaluate and display the array from an in-string in the $str var

also any suggestions on how  the steps in this can be compacted  with SRER are welcome 

#include <File.au3>
Local $Array[9][10] = [ _
        [9], _
        [""], _
        ["B", 1], _
        [""], _
        ["C", 1, 2], _
        [""], _
        [""], _
        ["D", 1, 2, 3]]

$str = _ArrayToString($Array, Default, Default, Default, "'" & @LF)
$sString =  StringRegExpReplace($str, "\n[|']*'", "")
$str = StringRegExpReplace($sString, "'\n(.)", "'], " & "['" & '$1')
$str = StringReplace($str, "||", '')
$str = "[['" & StringReplace($str, "|", "', '")
$str = StringReplace($str, "[|", "['','") & "']]"
$str = StringReplace($str, ", ['', '']]", "]")
;~ _ArrayDisplay($str)
ConsoleWrite($str & @LF)

Thanks

Deye

Edited by Deye
Link to comment
Share on other sites

This is the path I would head down (i can put it in pretty brackets if you need help with that, but there are many hundreds of ways to format the resulting characters)

#include <Array.au3>

Local $Array[9][10] = [ _
        [9], _
        [""], _
        ["B", 1], _
        [""], _
        ["C", 1, 2], _
        [""], _
        [""], _
        ["D", 1, 2, 3]]


msgbox(0, '' , stringregexpreplace(_ArrayToString($Array , "," , -1 , -1 , ",") , ",,+" , @LF))

 

And while you wont be able to declare your variable as an array and debug it, you can split it on the brackets

local $aTest[0]

_ArrayAdd($aTest , $str , 0 , "]," , "[")

_ArrayDisplay($aTest)

 

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

Link to comment
Share on other sites

one more stab at this:

Local $Array[9][10] = [ _
        [9], _
        [""], _
        ["B", 1], _
        [""], _
        ["C", 1, 2], _
        [""], _
        [""], _
        ["D", 1, 2, 3]]


msgbox(0, '' , "[" & stringtrimright("[" & StringReplace(StringRegExpReplace(_ArrayToString($Array , " ") , "\s\s+" , "],["), " " , ",") , 2) & "]")

 

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

Link to comment
Share on other sites

If the final purpose is to get an array, maybe this ?

#Include <Array.au3>

Local $Array[9][10] = [ _
        [9], _
        [""], _
        ["B", 1], _
        [""], _
        ["C", 1, 2], _
        [""], _
        [""], _
        ["D", 1, 2, 3]]

$s = StringStripWS(StringRegExpReplace(_ArrayToString($Array , ","), '[,\s]{2,}', @crlf), 2)
Local $a[0][UBound($Array, 2)], $dummy = _ArrayAdd($a, $s, 0, ",")
_ArrayDisplay($a)

 

Link to comment
Share on other sites

The idea was to keep a structure without blank rows

So this will need to have a different structure made @obtaining the cols first , then adding them 1 by 1

In the end it won't be the fastest way to restructure 2D array But fast enough, And never the less good exercise ;)

Local $Array[9][10] = [ _
        [9], _
        ["", 'A'], _
        ["B", 1], _
        ["", 'B', 2], _
        ["C", 1, 2], _
        ["", "", ""], _
        [""], _
        ["D", 1, 2, 3]]

 

Edited by Deye
Link to comment
Share on other sites

:)

#Include <Array.au3>

Local $Array[9][10] = [ _
        [9], _
        ["", 'A'], _
        ["B", 1], _
        ["", 'B', 2], _
        ["C", 1, 2], _
        ["", "", ""], _
        [""], _
        ["D", 1, 2, 3]]

$s = StringStripWS(StringRegExpReplace(_ArrayToString($Array , ","), '(?m)^.*[^,\v](*SKIP)(*F)|[,\v]+\R|^,+$', @crlf), 2)
Local $a[0][UBound($Array, 2)], $dummy = _ArrayAdd($a, $s, 0, ",")
_ArrayDisplay($a)

 

Link to comment
Share on other sites

  • Solution

and one more, that introduces new and fun edge cases

#include <Array.au3>

Local $Array[9][10] = [ _
        [9], _
        ["", 'A'], _
        ["B", 1], _
        ["", 'B', 2], _
        ["C", 1, 2], _
        ["", "", ""], _
        [""], _
        ["D", 1, 2, 3]]

local $a[0][ubound($Array , 2)]
_ArrayAdd($a , StringRegExpReplace(_ArrayToString($Array) , "\R\|{" & ubound($Array , 2) - 1 & "}" , ""))
_ArrayDisplay($a)

 

Edited by iamtheky

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

Link to comment
Share on other sites

lol didn't think about using _ArrayAdd in that way ..

 

Spoiler
#include <Array.au3>

Local $Array[50][10] = [ _
        [9], _
        ["", 'A'], _
        ["B", 1], _
        ["", 'B', 2], _
        ["C", 1, 2], _
        ["", "", ""], _
        [""], _
        ["D", 1, 2, 3]]

_ArrayDisplay(_ArrayStringFromArray($Array))
;~ $a = _GenRandomArray(1000)
;~ _ArrayDisplay($a)
;~ _ArrayDisplay(_ArrayStringFromArray($a, 1))

Func _ArrayStringFromArray(ByRef $aArray, $bUBound = False, $sDelim = "'")
    If UBound($aArray, 2) >= 1 Then
        $str = _ArrayToString($aArray, Default, Default, Default, $sDelim & @LF)
        $sString = StringRegExpReplace($str, "\n[|']*'", "")
        $sString = $sDelim & StringReplace($sString, @LF, "")
        Local $UBound = @extended + 1
        $a = StringRegExp($sString, "[^|]+", 3)
        Local $aTmp[$UBound][UBound($aArray, 2)]
        Local $i = -1, $k = -1, $j = 0
        Do
            $i += 1
            $k += 1
            While Not StringInStr($a[$i], $sDelim)
                $aTmp[$k][$j] = $a[$i]
                $j += 1
                $i += 1
            WEnd
            $j = 1
            If $a[$i] = $sDelim Then
            Else
                $aTmp[$k + 1][0] = StringReplace($a[$i], $sDelim, '')
            EndIf
        Until $i > UBound($a) - 2
        If $bUBound Then $aTmp[0][0] = $UBound - 1
        Return $aTmp
    Else
        Local $pattern = $sDelim & "[" & $sDelim & "]*[" & $sDelim & "]"
        $str = StringRegExpReplace($sDelim & _ArrayToString($aArray, $sDelim), $pattern, $sDelim)
        $sString = StringRegExpReplace($str, $sDelim & "$", "")
        Return StringSplit(StringTrimLeft($sString, 1), $sDelim, $bUBound ? "" : 3)
    EndIf
EndFunc

Func _GenRandomArray($icount)
    Local $aArrayReturn[$icount]
    For $i = 0 To UBound($aArrayReturn) - 1
        If Mod($i, 5) = 0 Then $aArrayReturn[$i] = Random(1, 100)
    Next
    Return $aArrayReturn
EndFunc

 

 

Edited by Deye
Link to comment
Share on other sites

so this, in essence, being a removing blank rows exercise; seems a flavor of this loop would also be quick.

#include <Array.au3>

Local $Array[9][10] = [ _
        [9], _
        ["", 'A'], _
        ["B", 1], _
        ["", 'B', 2], _
        ["C", 1, 2], _
        ["", "", ""], _
        [""], _
        ["D", 1, 2, 3]]


For $i = ubound($Array) - 1 to 0 step - 1
    For $j = 0 to ubound($Array , 2) - 1

If $Array[$i][$j] <> "" Then ExitLoop
If $j = ubound($Array , 2) - 1 Then _ArrayDelete($Array , $i)

    Next
Next

_ArrayDisplay($Array)

 

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

Link to comment
Share on other sites

_arrayDelete is faster then reevaluating the array with _arraytostring which takes time to expand

Not to think about arrays inside arrays ..

with my test the only secure and fastest way for deleting rows was with using Inpho's function that quickly determines the rows to delete into a string of rows that then get used with _arraydelete , no faster way,

https://www.autoitscript.com/forum/topic/200038-_arraydelemptyrows/?tab=comments#comment-1435176

Edit : just like you put it 

with what we did here I knew there was no faster way,  just got  caught playing around, no biggy ;)

 

Edited by Deye
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...