Parse loop "over-omitting" spaces? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Parse loop "over-omitting" spaces?

Post by kunkel321 » 30 Nov 2021, 15:38

Hi Folks,
The documentation indicates that I can have a parameter to OmitChars from the ends of the parsed substrings in a parse loop. With the sample below, I don't have that parameter included, so the spaces before and after each line of var should be preserved -- yes?

When run, it appears to step through as this:
|first item|
| second item|
| this item|

Shouldn't it be like this?
| first item |
| second item |
| this item |

Code: Select all

var =
(
 first item 
 second item 
 third item 
)

Loop, Parse, var, `n,`r 
{
MsgBox Loop |%A_Index%| is |%A_LoopField%|
}
ste(phen|ve) kunkel


sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Parse loop "over-omitting" spaces?

Post by sofista » 30 Nov 2021, 18:04

Also, use expression mode, otherwise msgbox won't show first and last space of var
Spoiler

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Parse loop "over-omitting" spaces?

Post by kunkel321 » 01 Dec 2021, 16:59

Thanks for the good info fellas. That does indeed fix the script I posted. It occurs to me that I asked the wrong question though, or asked it poorly.

My actual project pulls the lines of text from an ini file. Something like this:

Code: Select all

[section]
 first item 
 second item 
 third item 

Code: Select all

#SingleInstance Force

/*
var := "
(RTrim0
 first item 
 second item 
 third item 
)"
*/

IniRead, var, sample.ini, section
MsgBox % var

;var = (RTrim0`n %var% `n) ; Nope.

Loop, Parse, var, `n,`r 
{
MsgBox Loop |%A_Index%| is |%A_LoopField%|
}

Esc::
ExitApp
Is it possible to use RTrim0 for that?

It's also worth noting that I plan to paste the content, so I can't embed {Space} before/after each line.

The section of actual code
Spoiler

The list of items get pushed into a GUI as checkboxes, then after I select a sub-set of them, they get re-combined into a sentence. Currently, my script occasionally pushes the segments together without spaces. I don't *always* want spaces between them though... Only if the original list in the INI has spaces.

EDIT !!!
Actually I just realized that this has nothing to do with the loop. With my sample code, in the first MsgBox, the spaces are already culled... I will now re-read the iniRead documenation....
Last edited by kunkel321 on 01 Dec 2021, 17:17, edited 1 time in total.
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Parse loop "over-omitting" spaces?

Post by mikeyww » 01 Dec 2021, 17:17

Your file is not in an INI format. My general understanding is that reading INI will strip some white space accordingly. You could just read your file as text instead. The bounding spaces would then be preserved.

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Parse loop "over-omitting" spaces?  Topic is solved

Post by kunkel321 » 01 Dec 2021, 17:41

Unfortunately, I have to use INIs (rather than ReadFile) or it will mess up my whole tool. It's true that the key is not formatted per INI standards. I don't see anything in the Documentation about preserving spaces in INI files. That's alright though. I'll mark this post as solved. :D
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Parse loop "over-omitting" spaces?

Post by mikeyww » 01 Dec 2021, 17:47

The operating system automatically omits leading and trailing spaces/tabs from the retrieved string. To prevent this, enclose the string in single or double quote marks. The outermost set of single or double quote marks is also omitted, but any spaces inside the quote marks are preserved.
I'm not sure whether this works with a whole section, but testing it would be straightforward.

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Parse loop "over-omitting" spaces?

Post by kunkel321 » 07 Dec 2021, 18:01

mikeyww wrote:
01 Dec 2021, 17:47
The operating system automatically omits leading and trailing spaces/tabs from the retrieved string. To prevent this, enclose the string in single or double quote marks. The outermost set of single or double quote marks is also omitted, but any spaces inside the quote marks are preserved.
I'm not sure whether this works with a whole section, but testing it would be straightforward.
Doesn't seem to work. Good thought though!
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”