Stringreplace function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Stringreplace function

Post by majstang » 09 Aug 2020, 05:36

Having problems with applying ternary operators to this function. Perfer it to return empty if String=0 and return 0 if String=1. Failed with the InStr attempt in function.

Code: Select all

String := "0"
msgbox % mid$(String, 1, 0)

mid$(input, startPos, replacement) {
 ;vr_on := InStr(SubStr(vr_output,1,1),0) ? StrReplace(vr_output,0,"1",,1) : vr_output
 Return, SubStr(input,1,startPos-1) . replacement . SubStr(input,startPos+StrLen(replacement))
}

majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Stringreplace function

Post by majstang » 11 Aug 2020, 07:06

hI AGAIN,
I tried it in an another way but its hard executing functions inside in expressions.
Here is what im doing.

Code: Select all

;EasyIni[Section].Vr_output := mid$(EasyIni[Section].Vr_output, 1, 0) ;original write easyini command
  EasyIni[Section].Vr_output ? "" : (:= mid$(EasyIni[Section].Vr_output, 1, 0)) ; trying to conditioning a one-liner but it not producing the correct result. Which would be if empty no do else write it

majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Stringreplace function

Post by majstang » 11 Aug 2020, 08:02

nah...not very hard...i am daft
This works,

Code: Select all

EasyIni[Section].live ? (EasyIni[Section].live := mid$(EasyIni[Section].live, 1, 0)) : ""

A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Stringreplace function

Post by A_AhkUser » 11 Aug 2020, 16:09

hi majstang,
majstang wrote:
11 Aug 2020, 07:06
I tried it in an another way but its hard executing functions inside in expressions.

Code: Select all

string := "test"
returnValueIfFalse := "It is false!"
MsgBox % var := (string = "") ? "never executed..." : (returnValueIfFalse, stuffIfFalse())
; the left most expression in the winning branch will be used as return value (use comma and parentheses to execute multiple expressions, when relevant, within the operand)
MsgBox % var := (string <> "") ? function() : ; there is no need to prefix the function with a ':='; also, the third operand can be omitted

stuffIfFalse() {
MsgBox % "stuff before returning false..."
}
function() {
return "Once again it is false!"
}
hope this helps

A_AhkUser
my scripts

majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Stringreplace function

Post by majstang » 11 Aug 2020, 16:50

Thank you A_AhkUser that was good information :)
These one-liners is nice exercise.
; there is no need to prefix the function with a ':='; also, the third operand can be omitted
Normally yes, but in this case I had to include the ':=' because it is required EasyINI syntax for write to inis. Applying it in a ternary was what confused me. Oh, so the third operand can be omitted! That is perfect because these one-liners tend to be very space consuming.

A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Stringreplace function

Post by A_AhkUser » 11 Aug 2020, 17:40

majstang wrote:
11 Aug 2020, 16:50
Thank you A_AhkUser that was good information :)
i'm glad that was helpful :D
(...) Normally yes, but in this case I had to include the ':=' because it is required EasyINI syntax for write to inis.
I would agree without reservation for the last example but, unless I'm missing something, I am more doubtful for the penultimate one (where your := assignment operator lacks its first member (the variable which is intented to store the result of the expression): in this case, := is, at least, dispensable (but I could be wrong).
Applying it in a ternary was what confused me. Oh, so the third operand can be omitted! That is perfect because these one-liners tend to be very space consuming.
For conditional assignment, I prefer use a simple expression instead of a ternary operation:

Code: Select all

var := false
(not var && var:=function())
MsgBox % var

function() {
return true
}
; ((EasyIni[Section].live <> "") && EasyIni[Section].live:=mid$(EasyIni[Section].live, 1, 0))
These one-liners is nice exercise.
I fully agree :D

cheers

A_AhkUser
my scripts

Post Reply

Return to “Ask for Help (v1)”