Jump to content

How the variable-name-string in the edit-box is converted to the variable itself - (Moved)


Recommended Posts

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=D:\MESS\桌面\Form1_1.kxf
$Form1_1 = GUICreate("Form1",720, 406, 192, 114)
GUISetFont(12)

GUICtrlCreateLabel("String1", 14, 8, 60, 17)
$idEdt1 = GUICtrlCreateEdit("", 80, 8, 630, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "Boats sail on the rivers,And ships sail on the seas")

GUICtrlCreateLabel("String2", 14, 63, 60, 17)
$idEdt2 = GUICtrlCreateEdit("", 80, 63, 630, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "But clouds that sail across the sky,Are prettier far than these.")

GUICtrlCreateLabel("hyphen", 14, 135, 60, 17)
$idEdt3 = GUICtrlCreateEdit("@CRLF", 80, 135, 201, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetFont($idEdt3,18)

$idJoin = GUICtrlCreateButton("JOIN", 317, 135, 129, 49)

GUICtrlCreateLabel("Result", 14, 205,60, 17)
$idEdt4 = GUICtrlCreateEdit("", 80, 205, 630, 185, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL,$WS_HSCROLL))

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $idJoin
            local $String1=GUICtrlRead($idEdt1)
            local $String2=GUICtrlRead($idEdt2)
            local $sHyphen=GUICtrlRead($idEdt3)
            local $sResult=$String1 & $sHyphen & $String2
            GUICtrlSetData($idEdt4,$sResult)
    EndSwitch
WEnd

If  want to fill in the following string in the hyphen-edit-box:

"@CRLF"
"\r\n"
";"

$Var="/"
"Var"

and display the results correctly in the Result-edit-box,

how do convert it?

Thanks in advance!

Edited by Letraindusoir
Link to comment
Share on other sites

GuiCtrlRead returns a string, and in this case you get "@crlf" as a string litteral
You should build your own conversion table (using a 2D array could be a good idea)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1_1 = GUICreate("Form1",720, 406, 192, 114)
GUISetFont(12)

GUICtrlCreateLabel("String1", 14, 8, 60, 17)
$idEdt1 = GUICtrlCreateEdit("", 80, 8, 630, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "Boats sail on the rivers,And ships sail on the seas")

GUICtrlCreateLabel("String2", 14, 63, 60, 17)
$idEdt2 = GUICtrlCreateEdit("", 80, 63, 630, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetData(-1, "But clouds that sail across the sky,Are prettier far than these.")

GUICtrlCreateLabel("hyphen", 14, 135, 60, 17)
$idEdt3 = GUICtrlCreateEdit("newline", 80, 135, 201, 49, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL))
GUICtrlSetFont($idEdt3,18)

$idJoin = GUICtrlCreateButton("JOIN", 317, 135, 129, 49)

GUICtrlCreateLabel("Result", 14, 205,60, 17)
$idEdt4 = GUICtrlCreateEdit("", 80, 205, 630, 185, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_VSCROLL,$WS_HSCROLL))

GUISetState(@SW_SHOW)

local $sHyphen
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $idJoin
            local $String1=GUICtrlRead($idEdt1)
            local $String2=GUICtrlRead($idEdt2)
            If GUICtrlRead($idEdt3) = "newline" Then $sHyphen=@crlf
            local $sResult=$String1 & $sHyphen & $String2
            GUICtrlSetData($idEdt4,$sResult)
    EndSwitch
WEnd

 

Link to comment
Share on other sites

48 minutes ago, mikell said:

GuiCtrlRead returns a string, and in this case you get "@crlf" as a string litteral
You should build your own conversion table (using a 2D array could be a good idea)

 

Thank you, Mikell!

I ask this problem not just to type a specific string into a variable, but to get a general approach.

To put it simply,

how to convert a  variable-name-string into the variable self ,

especially when GUI_Edit_Box in the software interface allows users to enter uncertain strings,

can automatically convert them correctly when they are found to be macro-variable or regular-metacharacters, or declared-variable-name

Link to comment
Share on other sites

That's what Mikell is suggesting :) You'll want to build a 2D array with a name in the first column and a replacement value in the second.

; Your list of replacements
Local $aReplacements[10][2] = [ _
        ["@CRLF", @CRLF], _
        ["@TAB", @TAB], _
        ["@CR", @CR] _
    ]

; For each replacement
For $i=0 To UBound($aReplacements) - 1
    ; Make the replacement
    $something = StringReplace($something, $aReplacements[$i][0], $aReplacements[$i][1])
Next

As for already declared variables, that seems strange... do you mean that you would allow the user to store variables or that they could use variables that you program into the program? For the former, you could build another replacement array, where it contains the variable name and the value. For the latter, you'll want to look at Eval.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

7 hours ago, Letraindusoir said:

how to convert a  variable-name-string into the variable self

Like this?

Local $myVar = 3.1415926
Local $s = "my var name = $myvar$"
Opt("ExpandVarStrings", 1)
ConsoleWrite($s & @LF)
ConsoleWrite("CR then LF is @CRLF@" & "CR then LF inserted!" & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Local $sStr="Boats sail on the rivers,And ships sail on the seas,But clouds that sail across the sky,Are prettier far than these."
local $sNew=StringRegexReplace($sStr,",","\r\n")
consolewrite($sNew)

ask a regular question, as shown above,

how to replace all commas in a string with whitespace represented by regular-metacharacters (for example,\ r\ n)?

It's not working above.

 

Link to comment
Share on other sites

In the replacement part of StringRegexReplace(), only $1, $2, $3 ... (or \1, \2, \3 ...) are recognized.  \r\n is a C thing while AutoIt conveniently offers @CRLF.  But here the regexp isn't even necessary:

Local $sStr = "Boats sail on the rivers,And ships sail on the seas,But clouds that sail across the sky,Are prettier far than these."
local $sNew = StringReplace($sStr, ",", @CRLF)
consolewrite($sNew)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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