AutoHotkey Community

It is currently May 27th, 2012, 5:30 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: March 12th, 2010, 6:09 am 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
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 March 12th, 2010, 7:42 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 6:18 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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%


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 6:28 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
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%


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 6:38 am 
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...


Report this post
Top
  
Reply with quote  
PostPosted: March 12th, 2010, 7:16 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
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?

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 7:41 am 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 7:50 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
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. :)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 8:19 am 
None wrote:
Its not a bug its a error.

...:shock: huh? :shock:...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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 8:31 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
@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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 9:41 am 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
OK,I admit it, it was me, my misunderstanding, not a bug. :oops:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 9:47 am 
Don't feel bad, it's still kinda annoying, even if that's "how it's supposed to work"...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 9:56 am 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 12th, 2010, 1:19 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
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,
Image
"Cee, hot cakes.." :lol:

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

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:
Image


htms

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 4:09 pm 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: dra, HotkeyStick, rbrtryn, Yahoo [Bot] and 68 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group