Sign in to follow this  
Followers 0
jamesstp20

For Each Letter in my string..

11 posts in this topic

#1 ·  Posted (edited)

Hello, I would like to do something like this but I don't know how:

$String = "AABABD"

For Each $letter in $String : If $letter =A then.. elseif $letter = B then .... And I would like to do that with each letter of my string.

Thanks

Edited by jamesstp20

Share this post


Link to post
Share on other sites



jamesstp20,

Use StringSplit to get the characters into an array and then loop through them, or use StringMid to extract each character in turn - then use Switch: :>

$sString = "AABABD"

ConsoleWrite("Using StringSplit" & @CRLF)
$aString = StringSplit($sString, "")
For $i = 1 To $aString[0]
    Switch $aString[$i]
        Case "A"
            ConsoleWrite("Character " & $i & " is an A" & @CRLF)
        Case "B"
            ConsoleWrite("Character " & $i & " is an B" & @CRLF)
        Case Else
            ConsoleWrite("Character " & $i & " is neither A nor B" & @CRLF)
    EndSwitch
Next

ConsoleWrite(@CRLF & "Using StringMid" & @CRLF)
For $i = 1 To StringLen($sString)
    Switch StringMid($sString, $i, 1)
        Case "A"
            ConsoleWrite("Character " & $i & " is an A" & @CRLF)
        Case "B"
            ConsoleWrite("Character " & $i & " is an B" & @CRLF)
        Case Else
            ConsoleWrite("Character " & $i & " is neither A nor B" & @CRLF)
    EndSwitch
Next

All clear? :unsure:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Try this:

$String = "AABABD"
$aString = StringSplit($String, "", 2)
For $Letter In $aString
    If $Letter = "B" Then MsgBox(0, "Information", "Letter B found")
Next

Probably M23 was faster...

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

I would do something like this maybe?

$string = "AABABD"
$len = StringLen($string)
For $i = 1 To $len
    $left = StringLeft($string, 1)
    Select
        Case $left = "A"
            MsgBox(0, "A", "The character is A")
        Case $left = "B"
            MsgBox(0, "B", "The Character is B")
        Case $left = "D"
            MsgBox(0, "D", "The character is D")
    EndSelect
    $string = StringTrimLeft($string, 1)
Next

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites

UEZ,

Probably M23 was faster...

You need to speed up yout typing! :>

Frohe Ostern! :unsure:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

And you need to speed up youtyour spell checking!


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks to both of you :unsure: !!! All those 3 methods are working fine :> I will take the one of UEZ because it's cleaner ^^

Thanks ! ;)

Share this post


Link to post
Share on other sites

UEZ,

You need to speed up yout typing! ;)

Frohe Ostern! :>

M23

I'm 39 and speeding up typing will probably not work. :unsure:

Dir auch Frohe Ostern! :D

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Ok well now it's a little more complicated... :unsure:

I want to do the same thing

if $Letter = "s" then If the next letter is "A" than.. Else If the next letter is B.....

Thanks..

Share this post


Link to post
Share on other sites

I know this post is old and my comment redundant, since its nothing new, but perhaps it could help some unexperienced autoitters ;D

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $str[20], $trimmed = 0, $alltrimmed = 0, $stout = "", $rx_sub = 0, $tx_sub = 0

$Form1 = GUICreate("Consumption", 120, 85, 5, 5, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS), $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
$Label1 = GUICtrlCreateLabel("Down Mb", 3, 5, 55, 20)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("0", 65, 3, 55, 20, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Label2 = GUICtrlCreateLabel("Up Mb", 3, 25, 55, 20)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput("0", 65, 23, 55, 20, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Label3 = GUICtrlCreateLabel("Used Mb", 3, 45, 55, 20)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Input3 = GUICtrlCreateInput("0", 65, 43, 55, 20, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$reset = GUICtrlCreateButton("Reset", 3, 63, 114, 20)
GUISetState(@SW_SHOW)


adlibregister("GetInfo", 1000)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $reset
            Global $rx_sub = $rx_sub + $rx
            Global $tx_sub = $tx_sub + $tx
            GUICtrlSetData($reset,"Resetting..")
            GetInfo()
            GUICtrlSetData($reset,"Reset")
    EndSwitch
WEnd

Func GetInfo()
    $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE,  $STDIN_CHILD & $STDOUT_CHILD)
    Sleep(500)
    $line = StdoutRead($cmd)
    ;getting from STDOUT only number of received bytes and update control whith this info:
    $str = StringSplit($line,@CR)
    For $i = 1 to $str[0]-1 Step 2
        If $str[$i] <> "" And $i = 5 Then
            $strn = StringSplit($str[$i],"")
            $string = StringReplace($str[$i],"Bytes","")

            While 1
                $test = StringMid($string, 1, 1)
                If Not StringIsDigit($test) Then
                    $string = StringTrimLeft($string,1)
                ElseIf StringIsDigit($test) Or @error Then
                    ExitLoop
                EndIf
            WEnd

            While 1
                $test = StringMid($string, StringLen($string) -1, 1)
                If Not StringIsDigit($test) Then
                    $String = StringTrimRight($string,1)
                ElseIf StringIsDigit($test) Or @error Then
                    ExitLoop
                EndIf
            WEnd
            $strings = StringSplit($string, " ")
            Global $rx = $strings[1]
            Global $tx = $strings[$strings[0]]
            $all = $rx + $tx

            GUICtrlSetData($Input1, Round(($rx - $rx_sub)/ (1024 * 1024), 2))
            GUICtrlSetData($Input2, Round(($tx - $tx_sub)/ (1024 * 1024), 2))
            GUICtrlSetData($Input3, Round(($all - $tx_sub - $rx_sub)/ (1024 * 1024), 2))
            $stout = ""
            $trimmed = 0
            $alltrimmed = 0
        EndIf
    Next

    EndFunc

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
Sign in to follow this  
Followers 0