Jump to content

Parse String based on multiple conditions to array


Marnie
 Share

Recommended Posts

Hello everyone,

 

I desperately need help with parsing the string.

"+" is delimiter, but "/" is telling you the number of variants 

Example of string (input): +AA1+BB1+CC1/CC2/CC3+DD4

Array should look like (output):  image.png.183f9c62b252c4392b5681c991fb65b8.png

 

 

Thanks

Link to comment
Share on other sites

So far i have this and I am lost. It might be trivial ... how to proceed with For cycle and am I right at all ?

 

#include <Array.au3>
#include <String.au3>

Global $word = "+AA1+BB1+CC1/CC2/CC3+DD4"

Local $positionSlash = StringInStr($word,"/")
if $positionSlash = 0 Then
   ConsoleWrite($word & @CRLF)
   ;pripadne arrayexplode podle +
Else
   Local $firstPart = StringMid($word,1,$positionSlash-1)
   Local $positionPlus = StringInStr($word,"+",0,1,$positionSlash)
   if $positionPlus = 0 Then
      Null
   Else
      Local $followingPart1 = StringMid($word,$positionPlus,4)
   EndIf
EndIf

ConsoleWrite($firstPart & $followingPart1)
ConsoleWrite($followingPart1)

Local $FirstView = _StringExplode($word,"+")

;let's split to array according "+" sign
_ArrayDisplay($FirstView)

;but how to proceed next - no idea ?


Local $DivideSlash = _StringExplode($FirstView[4],"/")
_ArrayDisplay($DivideSlash)

;StringRegExp($ZakladniBattle

For $i=1 to UBound($FirstView)
   ConsoleWrite(StringRegExp($FirstView[$i],"/",2))
Next

 

Link to comment
Share on other sites

based off your input how did you infer there was a CC4 entry?

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

Link to comment
Share on other sites

  • Moderators

Marnie,

And can you have multiple variants in the same line - such as: +AA1+BB1/BB2+CC1/CC2/CC3+DD4 ?

And if so, what is the required output?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Assuming that you cannot have multiple variants as @Melba23 has asked, (because that would just be a huge headache). This code should work, but may need some tweaking to fit into your script. Or if it does not work for what you want, it will point you in the right direction.

;but how to proceed next - no idea ?

; Trim any blank values in the array
; Move backwards through the array deleting the blank lines
For $i = UBound($FirstView) - 1 To 0 Step -1
    If $FirstView[$i] = "" Then
        _ArrayDelete($FirstView, $i)
    EndIf
Next

;Find where you need to duplicate and split that duplication into another array
for $x = 0 to UBound($FirstView)-1 Step 1
    if Not StringInStr($FirstView[$x], "/") = 0 Then
        $location = $x
        $aData = _StringExplode($FirstView[$x], "/")
        ExitLoop
    EndIf
Next

;Make a new array with bounds you have found
$col = UBound($FirstView)
$row = UBound($aData)
local $FinalArray[$row][$col]

;double for loop to fill in the array
For $x = 0 to UBound($FirstView)-1 Step 1
    for $t = 0 to UBound($aData)-1 Step 1
        if $x = $location Then
            $FinalArray[$t][$x] = $aData[$t]
        Else
            $FinalArray[$t][$x] = $FirstView[$x]
        EndIf
    Next
Next

_ArrayDisplay($FinalArray)

 

Link to comment
Share on other sites

33 minutes ago, Melba23 said:

Marnie,

And can you have multiple variants in the same line - such as: +AA1+BB1/BB2+CC1/CC2/CC3+DD4 ?

And if so, what is the required output?

M23

Yes, right. You can have multiple variants. In this case output would be look like this:

image.png.8c6672c886827c2751970a5396a3542e.png

Link to comment
Share on other sites

Funny challenge  :)

#Include <Array.au3>

$str = "+AA1/AA2/AA3+BB1/BB2+CC1/CC2/CC3/CC4+DD4"

$a = StringRegExp($str, '[^+]+', 3)
Local $aRes[0][UBound($a)], $string, $sRes

_LetsGo(0, "")

_ArrayAdd($aRes, StringTrimRight($sRes, 2))
_ArrayDisplay($aRes)


Func _LetsGo($k, $string)
    Local $tmp = StringSplit($a[$k], "/", 2)
    For $i = 0 to UBound($tmp) -1
        If $k = UBound($a) -1 Then
            $sRes &= $string & $tmp[$i] & @crlf
        Else
            _LetsGo($k + 1, $string & $tmp[$i]  & "|")
        EndIf
    Next
EndFunc

 

Edited by mikell
typo...
Link to comment
Share on other sites

2 minutes ago, mikell said:

Funny challenge  :)

#Include <Array.au3>

$str = "+AA1/AA2/AA3+BB1/BB2+CC1/CC2/CC3/CC4+DD4"

$a = StringRegExp($str, '[^+]+', 3)
Local $aRes[0][UBound($a)], $string = "", $sRes

_LetsGo(0, "")

_ArrayAdd($aRes, StringTrimRight($sRes, 2))
_ArrayDisplay($aRes)


Func _LetsGo($k, $string)
    Local $tmp = StringSplit($a[$k], "/", 2)
    For $i = 0 to UBound($tmp) -1
        If $k = UBound($a) -1 Then
            $sRes &= $string & $tmp[$i] & @crlf
        Else
            _LetsGo($k + 1, $string & $tmp[$i]  & "|")
        EndIf
    Next
EndFunc

 

Wow, thank you mate. Works like a charm :)

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