Sign in to follow this  
Followers 0
Skrip

Letter Counter

6 posts in this topic

#1 ·  Posted (edited)

I need to make a letter counter. Like this.

Well...more of a letter adder.

#include <GUIConstants.au3>
; == GUI generated with Koda ==
$Form1 = GUICreate("Letter ", 507, 94, 192, 125)
GUICtrlCreateLabel("Enter the letters to calculate:", 16, 16, 139, 17)
$Input1 = GUICtrlCreateInput("", 160, 16, 329, 21, -1, $WS_EX_CLIENTEDGE)
$Input2 = GUICtrlCreateInput("", 160, 48, 329, 21, -1, $WS_EX_CLIENTEDGE)
ControlDisable($Form1, "", $Input2)
GUISetState(@SW_SHOW)
While 1
    $string = StringSplit(GUICtrlRead($Input1), "")
    GUICtrlSetData($Input2, $string[0])
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop

    EndSelect
WEnd
Exit

This is a basic design.

I know this is wrong but here's what I need to do.

Like this:

A=1

B=2

...

Z=26

So in the $input1 box (from the code above) if I put ABCD then 1+2+3+4 then $input2 will show 10.

Can somebody please help me out 'ere?

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites



I think the best way to do that is to go with ASCII codes.

Asc("A") = 65, Asc("B") = 66, etc.. A full list of ASCII is in the help file, and note that "A" has another ASCII code than "a". Anyway, read the letter in question, say "C" (67). So, subtract 64 from the ASCII code (67-64=3) and you have the alphabet position of the letter in question (C = third letter in alphabet).


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

HI,

; RegExp String occurrences

Global $s = "Hello World, I will count letters or Numbers like 1,2,3. And so on. 111!"

MsgBox(0, "", _stringCount($s, "z"))
MsgBox(0, "", _stringCount($s, "1"))
MsgBox(0, "", _stringCount($s, "num", 1)); Case-insensitivity flag.

Func _stringCount($string, $toFind, $case = "(?-i)", $startPosition = 0)
    If $case = 1 Then $case = "(?i)"
    Local $count = StringRegExp($string, $case & $toFind, 3, $startPosition)
    Switch @error
        Case 0
            If IsArray($count) Then Return UBound($count)
        Case 1
            If Not IsArray($count) Then Return 0
        Case 2
            Return -1
    EndSwitch
EndFunc  ;==>_stringCount

Func StringOccurrences($data, $str, $case, $c = 1)
    While StringInStr($data, $str, 0, $c)
        $c += 1
    WEnd
    Return $c
EndFunc  ;==>StringOccurrences

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

HI,

; RegExp String occurrences

Global $s = "Hello World, I will count letters or Numbers like 1,2,3. And so on. 111!"

MsgBox(0, "", _stringCount($s, "z"))
MsgBox(0, "", _stringCount($s, "1"))
MsgBox(0, "", _stringCount($s, "num", 1)); Case-insensitivity flag.

Func _stringCount($string, $toFind, $case = "(?-i)", $startPosition = 0)
    If $case = 1 Then $case = "(?i)"
    Local $count = StringRegExp($string, $case & $toFind, 3, $startPosition)
    Switch @error
        Case 0
            If IsArray($count) Then Return UBound($count)
        Case 1
            If Not IsArray($count) Then Return 0
        Case 2
            Return -1
    EndSwitch
EndFunc ;==>_stringCount

Func StringOccurrences($data, $str, $case, $c = 1)
    While StringInStr($data, $str, 0, $c)
        $c += 1
    WEnd
    Return $c
EndFunc ;==>StringOccurrences

So long,

Mega

I may be good at autoit...but uh. What does that do exactly..?

(I'm a little embarrased that I don't know this..)


[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites

Thanks for the reply.

But i'm unsure how to use this.

I tried

$string = StringSplit(GUICtrlRead($Input1), "")
    If $string = Asc("A") Then
        GUICtrlSetData($Input2, "1")
    EndIf
; This is just a test. I'll redo the math later (A=65  65-64=1)
StringSplit will return an array. So if you need to do what showed, you should do something like this (however I can't see the idea of it, perhaps you want to explain more about what your goal is and show spme more code, to enable people here to give a more to-the-point answer about handling the situation?

$aString = StringSplit(GUICtrlRead($Input1), "")

$sResult = ""

$iResult = 0

For $i = 1 To $aString[0] ;<- loops for every array element

$sResult += Asc($aString[$i]) - 64

$iResult += Asc($aString[$i]) - 64 ; <- $iResult will be the ((ascii-number) - 64) value of all letters added together.

Next

GUICtrlSetData($Input2, $sResult)


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

StringSplit will return an array. So if you need to do what showed, you should do something like this (however I can't see the idea of it, perhaps you want to explain more about what your goal is and show spme more code, to enable people here to give a more to-the-point answer about handling the situation?

$aString = StringSplit(GUICtrlRead($Input1), "")

$sResult = ""

$iResult = 0

For $i = 1 To $aString[0] ;<- loops for every array element

$sResult += Asc($aString[$i]) - 64

$iResult += Asc($aString[$i]) - 64 ; <- $iResult will be the ((ascii-number) - 64) value of all letters added together.

Next

GUICtrlSetData($Input2, $sResult)

Ahha! Thanks for helping. Works great.

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

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