Problem with unwanted blank line.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Problem with unwanted blank line.

22 Apr 2021, 02:46

I'm pulling lines of text from a text file and displaying them into a gui. For some reason I can't determine one line from the text file always seems to have a blank line at the end of it. I've tried the following to get rid of it:

Code: Select all

line := trim(line," `t`r`n")
But it still has a blank line at the end. Are there any other ways to clean lines? Thanks.
Rohwedder
Posts: 7626
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Problem with unwanted blank line.

22 Apr 2021, 04:05

Hallo,
here it works:

Code: Select all

lineOld := " Hallo `r`n "
line := trim(lineOld," `t`r`n")
MsgBox,%  "lineOld:`n >" lineOld "<`nline:`n >" line "<"
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Problem with unwanted blank line.

22 Apr 2021, 04:07

As @Rohwedder mentioned it works for me too, can you share the text you working with?
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with unwanted blank line.

22 Apr 2021, 04:54

I think it is something to do with the text and the font size. Depending upon what the font size is the problem line changes. I tried the following as an experiment but still got the extra space.

Code: Select all

word := line
word := trim(word,"`r`n")
line := ""
loop, parse, word
{
	line .= A_loopfield
	if instr(A_loopfield,".")
	break
}
The text is just sentences from a text file such as:

Use this to draw straight lines and to measure lengths.
Use this to prop up books.
Use this to keep your stationery items tidy.
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Problem with unwanted blank line.

22 Apr 2021, 06:10

just a test script / maybe use a hex editor to see the last characters in your text (EOL)
EDIT : remove leading spaces > x=%x%

Code: Select all

;- Problem with unwanted blank line. 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=89690

;- example remove last line's EOL (ascii 09-TAB  13-CR and 10-LF ) ( and other empty lines )
autotrim,on    ;- this is default

f1=%a_scriptdir%\test1.txt
f2=%a_scriptdir%\test2.txt
ifexist,%f2%
  filedelete,%f2%
transform,s,chr,34
e1=
(ltrim join`r`n

Use this to draw straight lines and to measure lengths.                 
   Use this to prop up books.

Use this to keep your stationery items tidy.

)
ifnotexist,%f1%
fileappend,%e1%,%f1%,utf-8
fileread,aa,%f1%
loop,parse,aa,`n,`r
 {
 x:=a_loopfield
 if (x="")
   continue
 x=%x%                        ;- remove leading spaces
 e2 .= s . x . s . "`r`n"
 }
;e3:= RTrim(e2, "`r`n")
e3:= Trim(e2," `t`r`n")       ;- Rohwedder's example
fileappend,%e3%,%f2%,utf-8
try
 run,%f2%
exitapp
Last edited by garry on 22 Apr 2021, 09:52, edited 1 time in total.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with unwanted blank line.

22 Apr 2021, 08:03

Thanks Gary, for taking the time to make and post the code. I ran it using my text file and then used it with my whole Gui.. I still got the same problem. For what it is worth your test2 file looks the same as my original file. I'm now thinking my problem must be in the following code:

Code: Select all

Guicontrol, Test:hide, SC
;use a dummy gui to calculate whether the new text fits on one line (if so center it
;or two or more lines (if so wrap it left)
Gui, w:new, -dpiscale
Gui, Font, s%SF% bold, arial	;SF is the size of the font that will fit the longest line of text in the space available.
Gui, Font, s%SF% bold, Comic Sans MF
Gui, Font, s%SF% bold, aakidprint
Gui, w:add, text, vs w%wid%, %line% ;wid is the width available for the text.
Guicontrolget, s, w:pos

Guicontrol, Test:,  SC, %line%
Guicontrolget, SC, Test:pos

if(sh > TP) ;TP height of single line of text with the font
Guicontrol, Test: +left, SC
else
Guicontrol, Test: +center, SC

newy := (_H - sH)/2	;_H is the height of my monitor
Guicontrol, Test:move, SC, % "y" newy "h" sh	; the aim is to center the text.
Guicontrol, Test:show, SC
I load 16 items one after the other and there's always one line that doesn't center correctly. Can anyone see problems with the code above that could create the kind of problem I'm having. Thanks
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with unwanted blank line.

22 Apr 2021, 19:39

I now think the problem is with the automatic wrapping of lines of ahk itself. If the text matches the width it "wraps" unnecessarily and produces a blank line on the fly. I confirmed this by using two slightly different widths for the same text and measuring their height. if the slightly wider text height is less then that suggests a blank line has appeared. Thanks for all the replies helping me confirm that the problem was not with the text file but with the wrapping process. I now have a method to work with to avoid the blank lines.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750 and 260 guests