How to convert this If-Else statement into Ternary Operator statement?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 09:30

I have these codes which are working fine. By these codes I can remove last line feed (`n) from a column of data-

Code: Select all

FileRead,v2,C:\Users\ADMIN\Desktop\New Text Document.txt

for k, v in b:= StrSplit(v2, "`n")
	{
	if (k = b.MaxIndex()-1)
	{
		t.= v
		Break
	}
	else
		t.= v "`n"
}
msgbox % t
I have these values in New Text Document.txt-
12_05_21 @7_32_40.PNG
12_05_21 @7_32_40.PNG (12.26 KiB) Viewed 587 times
I want to replace if else statement with ternary operator and for this i tried this-

Code: Select all

FileRead,v2,C:\Users\ADMIN\Desktop\New Text Document.txt

for k, v in b:= StrSplit(v2, "`n")
	(k = b.MaxIndex()-1)?(t.=v, Break):(t.=v "`n")
msgbox % t
but these codes are not working. Perhaps here is the problem-
12_05_21 @7_45_38.PNG
12_05_21 @7_45_38.PNG (3.88 KiB) Viewed 587 times
In the above red box of the image you can see that I am trying to assign the value of v into variable t as well as break the loop at the very same time. I think I am unable to code the red marked part of the above codes correctly. Please tell me how we can perform two tasks in ternary operator, like assigning the value of a variable into other variable and at the same time breaking the loop? Please help. Thanks..
I don't normally code as I don't code normally.
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 09:45

Try

Code: Select all

While (!Br, b? "": b:= StrSplit(v2, "`n"))
    (A_Index = b.MaxIndex()-1)? (t.=b[A_Index], Br:=1):(t.=b[A_Index] "`n")
msgbox % t
or

Code: Select all

loop, % (b:= StrSplit(v2, "`n")).MaxIndex()
    (A_Index = b.MaxIndex()-1)? (t.=b[A_Index], Br:=1):(t.=b[A_Index] "`n")
Until, Br
msgbox % t
[Edit]: More concise while exemple
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 10:45

Maybe I misundertood something, but anyway I would do it as follows:

Code: Select all

SampleText := "
(
1000
2000
3000
4000
)"

t := ""

For k, v in b := StrSplit(SampleText, "`n") {
	t .= v ((k = b.MaxIndex()) ? "" : "`n")
}

MsgBox, % t

; Output:

; 1000
; 2000
; 3000
; 4000
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 11:00

To remove characters at the end of a string, use Trim.
14.3 & 1.3.7
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 11:19

sofista wrote:
12 May 2021, 10:45
Maybe I misundertood something, but anyway I would do it as follows:

Code: Select all

SampleText := "
(
1000
2000
3000
4000
)"

t := ""

For k, v in b := StrSplit(SampleText, "`n") {
	t .= v ((k = b.MaxIndex()) ? "" : "`n")
}

MsgBox, % t

; Output:

; 1000
; 2000
; 3000
; 4000
Sir, your codes are not working correctly as when i assign the value of t into clipboard using this-
MsgBox, % Clipboard:= t
and paste it into new text file then it shows the data like this-
12_05_21 @9_48_30.PNG
12_05_21 @9_48_30.PNG (11.08 KiB) Viewed 543 times
I don't normally code as I don't code normally.
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 11:44

@Sabestian Caine
Use another text editor (Example: Notepad++)
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: How to convert this If-Else statement into Ternary Operator statement?

12 May 2021, 13:40

Sabestian Caine wrote:
12 May 2021, 11:19
Sir, your codes are not working correctly as when i assign the value of t into clipboard using this-
MsgBox, % Clipboard:= t
and paste it into new text file then it shows the data like this-
Don't know what went wrong on your side. I works fine here —Windows 10, AutoHotkey 1.1.33.06—. Even I try to fileread data and paste the output in a MS Notepad file, but I get the same output as I posted before.

Maybe someone else can shed some light on this issue.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 312 guests