Need a little help with Loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Need a little help with Loop

28 Jun 2017, 05:16

I try to add an extra row if the result in the loop is correct. If I try to do manually, the new row will appear. If I try to do that via Loop, the new row will not appear. What I'm doing wrong?

Code: Select all

linas1 = some random text
if (linas1 <> "")
	linas1 = %linas1%`n`n ; in that situation I get the new row
Loop, 1
{
	if (linas[A_Index] <> "")
		linas[A_Index] = %linasA_Index%`n`n ; here I don't get any new row
}	
sikk = %linas1%
msgbox, %sikk%
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Need a little help with Loop

28 Jun 2017, 05:39

It's nothing to do with the loop
linas1 is a pseudo-array, not a real array. You cannot access elements of a pseudo-array with arr[index] syntax.

Code: Select all

	if (linas%A_Index% <> "")
		linas%A_Index% = linas%A_Index%`n`n
If you have no reason to be using pseudo-arrays (ie you are not trying to use a command that needs pseudo-arrays), then avoid them like the plague, they are horrid and should be burned with fire IMHO.

I would write the code thus:

Code: Select all

linas := ["some random text"]
Loop, 1
{
	if (linas[A_Index] <> "")
		linas[A_Index] .= "`n`n"
}	
sikk = linas[1]
msgbox, % sikk
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Need a little help with Loop

28 Jun 2017, 06:52

evilC wrote:It's nothing to do with the loop
Oh, now I see... Your example works, but it doesn't fit in my code.. I have to use clipboard as a source of data and there can be more than 1 value then. I have tried to add it to my code but without result :/ It could be nice to learn to use that method, because now I need to create many extra lines for each possible scenario...

Code: Select all

page := clipboard
nsutr = 0
Loop , parse , page, `n, %A_Space%%A_Tab%
{
	line := A_LoopField
	if line contains TTE: ; now the right line has been found, parse that line
	{
		lina%A_Index% := A_LoopField
		++nsutr
		linas%nsutr% := lina%A_Index%
	}
}
Loop, 10
{
	if (linas[A_Index] <> "")
		linas[A_Index] .= "`n`n"
	if (linas[A_Index] = "")
		linas[A_Index] := ""
}
sikk := linas[1]linas[2]linas[3]linas[4]linas[5]linas[6]linas[7]linas[8]linas[9]linas[10]
msgbox, % sikk
return
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Need a little help with Loop

28 Jun 2017, 07:05

... then avoid them like the plague, they are horrid and should be burned with fire IMHO.
:lol: :twisted:
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Need a little help with Loop

28 Jun 2017, 08:04

I did it that way:

Code: Select all

nsutr = 0
Loop , parse , page, `n, %A_Space%%A_Tab%
{
	line := A_LoopField
	if line contains TTEXT: ; now the right line has been found, parse that line
	{
		lina%A_Index% := A_LoopField
		++nsutr
		linas%nsutr% := lina%A_Index%
		linas := [ linas%nsutr% ]
	}
Loop, %nsutr%
{
	if (linas%A_Index% <> "")
		linas%A_Index% .= "`n"
	if (linas%A_Index% = "")
		linas%A_Index% =
}
sikkeras2 = %linas1%%linas2%%linas3%%linas4%
Msgbox, % sikkeras2
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Need a little help with Loop

28 Jun 2017, 08:27

As evilC has already advised use StrSplit() instead of dealing with a Loop creating a pseudo-array.
Additionally to that you should have a look at InStr() and how to use a ternary-operator.
Good luck :)
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Need a little help with Loop

28 Jun 2017, 08:31

Also be aware that this syntax is all fine and well when alone on one line, but does something quite specific when used within a command:

++nsutr

If you had code like this:

Code: Select all

nustr := 1
SomeFunc(++nsutr)
Then somefunc would be called with the value 2, not 1
++var means "increment and THEN use".

It's far more common to use nsutr++ ("Use and THEN increment")
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Need a little help with Loop

28 Jun 2017, 08:34

thank you for your advices guys, it will help me to improve my AHK skills :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, mikeyww and 327 guests