Emanoel Posted January 15, 2021 Posted January 15, 2021 Hello everyone, this is my code: Func Write ($datafromuser) Local $a[0], $count = 0, $count2 = 0 While $count <= 20 _ArrayAdd ($a, $datafromuser, '', @CRLF) $a[$count] = StringStripWS ($a[$count], 2) $count += 1 WEnd While $count2 <= 20 If $a[$count2] == "" Then $count2 += 1 Else MsgBox(0, "title", "a[" & $count2 & "]=" & $a[$count2]) $count2 += 1 EndIf WEnd EndFunc $datafromuser is the text entered to editbox. I wrote msgbox to check my code and it works good, but I could not find any solution to find the number of last line. Autoit has _FileCountLines to calculate last line of a specific file. Is there an order to replace with the number 20? I placed 20 just to test my code.
JockoDundee Posted January 15, 2021 Posted January 15, 2021 Func Write ($datafromuser) Local $a[0], $count = 0 While $count <= 20 _ArrayAdd ($a, $datafromuser, '', @CRLF) $a[$count] = StringStripWS ($a[$count], 2) If Not $a[$count] Then MsgBox(0, "title", "a[" & $count & "]=" & $a[$count]) $count+= 1 WEnd EndFunc Emanoel 1 Code hard, but don’t hard code...
Dan_555 Posted January 15, 2021 Posted January 15, 2021 i would do something like this: #include <Array.au3> #include <String.au3> Func Writ ($datafromuser) if StringLeft($datafromuser,2) <> @crlf Then $datafromuser=@CRLF & $datafromuser EndIf if StringRight($datafromuser,2) <> @crlf Then $datafromuser= $datafromuser & @crlf EndIf Local $a=_StringBetween($datafromuser,@CRLF,@CRLF) ;_ArrayDisplay($a) Cw ("Last Item is: " & $a[UBound($a)-1]) EndFunc Writ("test" & @crlf & "line1" & @crlf & "line2" & @crlf & "line3" & @crlf & "line4") Func cw($txt) ConsoleWrite ($txt & @crlf) EndFunc Emanoel 1 Some of my script sourcecode
Emanoel Posted January 15, 2021 Author Posted January 15, 2021 (edited) Thanks, JockDundee Is this work for blank lines? and Dan_555, what if I want to know the index of last array? My text contains some blank lines thats why I used if in second while. If $a[$count2] == "" Then Edited January 15, 2021 by Emanoel
Subz Posted January 16, 2021 Posted January 16, 2021 (edited) You can use "Ubound($a)-1" to get the last row number, personally I would do something like the following: Global $g_sString = "First Line" & @CRLF & "Second Line" & @CRLF & "" & @CRLF & "Third Line" Write($g_sString) Func Write ($datafromuser) Local $sDatafromUser = StringRegExpReplace($datafromuser, "(?:\r?\n|\r)+", @CRLF) ;~ Removes empty lines from the string Local $aDatafromUser = StringSplit($sDatafromUser, @CRLF, 1) ;~ Creates an array without empty lines For $i = 1 To $aDatafromUser[0] MsgBox(4096, "Title", $i & " of " & $aDatafromUser[0] & @CRLF & "$aDatafromUser[" & $i & "] = " & $aDatafromUser[$i]) Next EndFunc Edited January 16, 2021 by Subz Emanoel 1
JockoDundee Posted January 16, 2021 Posted January 16, 2021 5 hours ago, Emanoel said: Thanks, JockDundee Is this work for blank lines? I have no idea what the code actually does, it should however do whatever the old code did Emanoel 1 Code hard, but don’t hard code...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now