Jump to content

StringSplit and arrays


Recommended Posts

What I am trying to do is to not only split the words from "$line" but to count each split as well. Hope you understand.

I have a combo-box with a list of words in it, and it also gets read. The list is pretty long so i thought I'd make an array to do it for me.

$id =  GUICtrlCreateCombo("example_A", 64, 8, 161, 25)
GUICtrlSetData(-1, "example_B|example_C"
$ITEM = GUICtrlRead($id)

The words from "$line" I am hoping to assign a number to those words without having to manually put them in.

$array = StringSplit($line, "|", 1)
$line = "example_A|example_B|example_C"
            for $i = 1 to $array[0]
                Switch $ITEM
                Case $array[$i]
                        $MAT = $array[0]
                EndSwitch
            Next
Link to comment
Share on other sites

I don't understand this. The number of splits is counted automatically, and the total is assigned to the first element of the array (array index = 0). The rest of the strings are assigned to the following elements in the order that they appear in the original full string. The index number of each array element is a count of each substring split as it was created by the function StringSplit.

Edited by czardas
Link to comment
Share on other sites

I understand this :

$array[1] is example_A

$array[2] is example_B

$array[3] is example_C

after the code below is executed.

$array = StringSplit($line, "|", 1)
$line = "example_A|example_B|example_C"

Id like to use the combo-box to select one of these words("example_B"). and when i click a button it reads what us selected in the combo-box and returns What number it was in the list. But i am not sure how to do this task. maybe it is simple but i am not seeing it. Can you please give an example piece of code doing exactly that ?

Edited by Mikki
Link to comment
Share on other sites

You might want to change the order of the statements.

You are splitting something BEFORE you're putting something inside.

Shouldn't be the other way??

$array = StringSplit($line, "|", 1)
$line = "example_A|example_B|example_C"
            for $i = 1 to $array[0]
                Switch $ITEM
                Case $array[$i]
                        $MAT = $array[0]
                EndSwitch
            Next

this code above.. it returns the value 258 which is the number of words in the $line on my pc. instead of bringing out the total amount i want it to say what number it is for where that word was...

example_B should give me the value 2

example_C should give me the value 3 etc.

Link to comment
Share on other sites

$array = StringSplit($line, "|", 1)
$line = "example_A|example_B|example_C"
            for $i = 1 to $array[0]
                Switch $ITEM
                Case $array[$i]
                        $MAT = $array[0]
                EndSwitch
            Next

this code above.. it returns the value 258 which is the number of words in the $line on my pc. instead of bringing out the total amount i want it to say what number it is for where that word was...

example_B should give me the value 2

example_C should give me the value 3 etc.

Or maybe this.

local $line = "example_A|example_B|example_C"
local $array = StringSplit($line, "|", 1)
local $ITEM = "example_B"

For $i = 1 To $array[0]
    Switch $ITEM
        Case $array[$i]
            $MAT = $i
    EndSwitch
Next
MsgBox(0, "example_B", "index = " & $MAT)

#cs
; Instead of Switch...EndSwitch try:-
If $ITEM == $array[$i] Then $MAT = $i
#ce
Link to comment
Share on other sites

Mmm. Who needs arrays! (working but somewhat unfinished as I got to go)

#include <GUIConstantsEx.au3>
main()
Func main()
    Local Const $DELIM = '|'
    Local $lWords = 'word1|word2|word3'
    ;; GuiSet
    Local $hGui = GUICreate('Gui')
    Local $iCombo = GUICtrlCreateCombo('dummy', 10, 10)
    GUICtrlSetData(-1, '|' & $lWords)
    GUISetState()
    _GuiWait($iCombo, $lWords, $DELIM)
    ;; cleanup
    GUIDelete($hGui)
EndFunc
Func _GuiWait($iCombo, $lWords, $DELIM)
    Local $iMsg, $sWord, $iID = 0
    While 1
        $iMsg = GUIGetMsg()
        Select
            Case Not $iMsg
            Case $iMsg < 0
                If $iMsg = $GUI_EVENT_CLOSE Then ExitLoop
            Case $iMsg > 0
                $sWord = GUICtrlRead($iCombo)
                DebugOut('$sWord', $sWord) ;### Debug DebugOut.
                $iID = _GetId($sWord, $lWords, $DELIM)
                DebugOut('$iID', $iID) ;### Debug DebugOut.
        EndSelect
    WEnd
EndFunc
Func _GetId($sWord, $lWords, $DELIM)
    ;; prep for StringInString.
    $lWords = $DELIM & $lWords & $DELIM
    $sWord = $DELIM & $sWord & $DELIM
    Local $ipos = StringInStr($lWords, $sWord, 1)
    StringReplace(StringLeft($lWords, $ipos), $DELIM, $DELIM, 0, 1)
    Return @extended
EndFunc

Func DebugOut($1, $2 = '!NuLL!', $iErr = @error, $iExt = @extended, $iLine = @ScriptLineNumber)
    If Not ($2 == '!NuLL!') Then
        If $2 == '' Then $2 = '[EmptyString]'
        ConsoleWrite($iLine & ') ' & $1 & ' = ' & $2 & @CRLF)
    Else
        ConsoleWrite($iLine & ') ' & $1 & @CRLF)
    EndIf
    Return SetError($iErr, $iExt, 0)
EndFunc
OUTPUT:
45) $sWord = word1
47) $iID = 1
45) $sWord = word2
47) $iID = 2
45) $sWord = word3
47) $iID = 3
Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

OMG dude you rock Malkey :) Thanks for all your help guys.

_GUICtrlComboBox_FindStringExact()

Not sure how you would use this line of code to give the value in the combo-box a number to it lol. Thanks any way.

this is part of my code :

$id =  GUICtrlCreateCombo("example_01", 64, 8, 161, 25)
GUICtrlSetData(-1, "example_02|example_03|example_04|example_05|example_A|example_B|example_C", "example_02")
$ITEM = GUICtrlRead($id)
Statements()


Func Statements() ;Finds the id that fits the material desciption
$line = "example_01|example_02|example_03|example_04|example_05|example_A|example_B|example_C"  
$array = StringSplit($line, "|", 1)
            for $i = 0 to $array[0]
              If $ITEM == $array[$i] Then $MAT = $i
              Next
EndFunc

And this works "Perfectly now thanks to Malkey ;)

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