AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Fixed - thanks] Loop, Parse - looping one too many times?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Fri Mar 12, 2010 5:09 am    Post subject: [Fixed - thanks] Loop, Parse - looping one too many times? Reply with quote

Can anyone see why the following shows 4 lines of output for three fields in the test data? The fourth line has a blank A_LoopField

Code:
Test(SI_HotK, SI_String)
{
   Loop, Parse, SI_String,},{
      {
         result.=A_Index . ": " . A_LoopField . ":`n"
      }
   Return, %result%
}

TestData={F5}{enter}{click 50,150}
TestResult:=Test("!1",TestData)
MsgBox,,Test, Data %TestData%`nResult`n%TestResult%

I copied the simple Red,Green,Blue example from help and it showed the correct three fields.
I can work around it, but hate it when unexplained things happen.

Any help appreciated


Last edited by Michael@Oz on Fri Mar 12, 2010 6:42 am; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Fri Mar 12, 2010 5:18 am    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

Michael@oz wrote:
I can work around it, but hate it when unexplained things happen.


I do not know enough to explain it.. But I would workaround it with ternary, like:

Code:
Test(SI_HotK, SI_String)
{
   Loop, Parse, SI_String,},{
      {
         result .= A_LoopField ? A_Index . ": " . A_LoopField . ":`n" :
      }
   Return, %result%
}

TestData={F5}{enter}{click 50,150}
TestResult:=Test("!1",TestData)
MsgBox,,Test, Data %TestData%`nResult`n%TestResult%
Back to top
View user's profile Send private message Send e-mail
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Fri Mar 12, 2010 5:28 am    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

Michael@oz wrote:
Code:
Test(SI_HotK, SI_String)
{
   Loop, Parse, SI_String,},{
      {
         result.=A_Index . ": " . A_LoopField . ":`n"
      }
   Return, %result%
}

TestData={F5}{enter}{click 50,150} ;you have a Delimiter at the end of your string it assumes one more value
TestResult:=Test("!1",TestData)
MsgBox,,Test, Data %TestData%`nResult`n%TestResult%

Code:
Test(SI_HotK, SI_String)
{
   StringReplace, SI_String, SI_String, }{ ,‡, All ; you could add a string replace so only }{ is a Delimiter
      Loop, Parse, SI_String,‡,{} ;used a weird Char for the replace so it would not likely come up in real data
      {
         result.=A_Index . ": " . A_LoopField . ":`n"
      }
   Return, %result%
}

TestData={F5}{enter}{click 50,150}
TestResult:=Test("!1",TestData)
MsgBox,,Test, Data %TestData%`nResult`n%TestResult%
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Mar 12, 2010 5:38 am    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

While a ternary might be shorter code here, I normally workaround this with...

Code:
if (A_LoopField="") {
   continue
}

...at the top of the Loop...

I have no idea why None put his reply inside his quote of you, but he's right, Loop loops over each delimiter, even if it's at the end...if you change your Loop to...

Code:
   Loop, Parse, SI_String, {, }

...then you just have the "empty" at the beginning...so I think the best solution is to tell the Loop to continue on empty...
Back to top
sinkfaze



Joined: 18 Mar 2008
Posts: 5044
Location: the tunnel(?=light)

PostPosted: Fri Mar 12, 2010 6:16 am    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

I'd think the parsing loop should be avoided altogether since you're trying to parse the string by a pattern:

Code:
Test(SI_HotK, SI_String) {

   Pos=1
   While Pos:=RegExMatch(SI_String,"{.*?}",m,Pos+StrLen(m))
      result.=A_Index . ": " m ":`n"
   Return %result%

}


And just for my own curiosity, why is there a function parameter that doesn't do anything?
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Fri Mar 12, 2010 6:41 am    Post subject: [Fixed] Loop, Parse - looping one too many times? Reply with quote

Thanks everyone, if others have come across this should it be reported as a bug?
sinkfaze wrote:

And just for my own curiosity, why is there a function parameter that doesn't do anything?

I came across this situation early on in building the script, so I had not got to that bit yet. SI_HotK is going to define a hotkey name which will send the SI_String which will include some enhancements to the normal keycodes
Back to top
View user's profile Send private message
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Fri Mar 12, 2010 6:50 am    Post subject: Reply with quote

Code:
Colors = red,green,blue, ;added , gives the same problem
Loop, parse, Colors, `,
{
    MsgBox, Color number %A_Index% is %A_LoopField%.
}

Its not a bug its a error. If you put a extra Delimiter in the red,green,blue Example you mentioned you get the same thing there. Smile
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Mar 12, 2010 7:19 am    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

None wrote:
Its not a bug its a error.

...Shocked huh? Shocked...I thought a bug was an error?

I think the best fix would be a way to tell Loop to "skip empty at beginning", "skip empty at end" & "skip all empties"...3 separate (but combinable) options...maybe even "skip empties in the middle"...instead of the all option...

For another problem, I'd like an A_LoopLast, var/flag so you can know when a Loop is the last one...to avoid adding a separator at the end of a list...
Back to top
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Fri Mar 12, 2010 7:31 am    Post subject: Reply with quote

@Guest
To me a Bug is when the program does not do what it's supposed to do, and an Error is when you tell it to do somthing you don't want.
By using } as a Delimiter Michael@oz added one extra because all his values had them at the end and they are only supposed to go between values. So it was his mistake not the AHK's.
Back to top
View user's profile Send private message
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Fri Mar 12, 2010 8:41 am    Post subject: Reply with quote

OK,I admit it, it was me, my misunderstanding, not a bug. Embarassed
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Mar 12, 2010 8:47 am    Post subject: Reply with quote

Don't feel bad, it's still kinda annoying, even if that's "how it's supposed to work"...
Back to top
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Fri Mar 12, 2010 8:56 am    Post subject: Reply with quote

To finish this off, this is what I ended up with
Code:
; replace }{ with a single char delimiter
StringReplace, SI_String, SI_String,}{,‡,All    
; get rid of whitespace,{,},plus commas used for continuation sections   
Loop, Parse, SI_String,‡,{}`,%A_Space%%A_Tab%   
{
}
Thanks to all
Back to top
View user's profile Send private message
TLM



Joined: 21 Aug 2006
Posts: 2926
Location: The Shell

PostPosted: Fri Mar 12, 2010 12:19 pm    Post subject: Re: Loop, Parse - looping one too many times? Reply with quote

sinkfaze wrote:
I'd think the parsing loop should be avoided altogether since you're trying to parse the string by a pattern:

Code:
Test(SI_HotK, SI_String) {

   Pos=1
   While Pos:=RegExMatch(SI_String,"{.*?}",m,Pos+StrLen(m))
      result.=A_Index . ": " m ":`n"
   Return %result%

}


And just for my own curiosity, why is there a function parameter that doesn't do anything?
Sure it does,

"Cee, hot cakes.." Laughing

BTW, while does resolve the need for redundant 'look for blank' conditions Wink.

Wrox Press wrote:
The while Loop
A second kind of loop in C++ is the while loop. Where the for loop is primarily used to repeat a statement or a block for a prescribed number of iterations, the while loop will continue as long as a specified condition is true. The general form of the while loop is:

while(condition)
loop_statement;

where loop_statement will be executed repeatedly as long as the condition expression has the value true. Once the condition becomes false, the program continues with the statement following the loop. Of course, a block of statements between braces could replace the single loop_statement. The logic of the while loop can be represented like this:


htms
_________________
paradigm.shift:=(•_•)┌П┐RTFM||^.*∞
Back to top
View user's profile Send private message
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Fri Mar 12, 2010 3:09 pm    Post subject: Reply with quote

My perspective is,
Code:
While (IsThereA_BuiltInThingToDoSomething)
     UseIt

Hence my refined code
Code:
SI_String:=substr(SI_String,2,-1) ; remove extra delimiters
; parse with } & get rid of whitespace,},plus commas used in continuation sections   
Loop, Parse,SI_String,},{`,%A_Space%%A_Tab%   
   {
   result .= Process( A_LoopField)
   }

ps I'm still getting my head around regex & will use it when I can
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group