Jump to content

Find the last line of text entered from GUI


Recommended Posts

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.

Link to post
Share on other sites
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

 

Code hard, but don’t hard code...

Link to post
Share on other sites

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

 

Link to post
Share on other sites

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 by Emanoel
Link to post
Share on other sites

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 by Subz
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...