 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Wed Feb 24, 2010 7:26 pm Post subject: Loop parse question: Index count/break and invalid info |
|
|
I am using COM to read some customer information from one of our tools, and a parse loop to split it into individual bits for output/reporting ect.
---> My issue is, sometimes the 4th field of the msgbox or report data will contain all of the parse data. This only happens if field 4 is blank. If it is blank, it instead gets filled with ALL the information. If it is NOT blank, it contains what it should naturally contain.
| Code: |
;code I am using
Clipboard := COM_Invoke(pwb,"document.getElementById[divInfo].innerText")
Loop, parse, Clipboard, `n,
If A_LoopField is not Space
{
Info := (Info = "") ? A_LoopField : Info . "`n" . A_LoopField
If (A_Index = 4)
Break
}
StringSplit, Info, Info, `n
;and mgsbox out as a test with this:
msgbox,
(
CustName: %Info1%
Address1: %Info2%
Address2: %Info3%
Address3: %Info4%
Acct Num: %AcctNum1%
.......
)
|
Example1:
Name: Joe Shmoe
Addr1: 123 Fake St
Addr2: Apartment 0
Addr3: Townsville, CA
Acct#: 12345678931216549874321
TN1: 555-555-1234
TN2: 555-555-1235
Example2:
Name: Joe Shmoe
Addr1: 123 Fake St
Addr2: Townsville, CA (no appartment)
Addr3: <blank>
Acct#: 12345678931216549874321
TN1: 555-555-1234
TN2: 555-555-1235
Example 1 works fine, everything pases to the msgbox as intended. It has the name, and all three bits of address information.
However- Example2, if that 4th loop field (or third address bit, technically) is blank... the msgbox gets messed up. The msgbox will output everything EXCEPT on line 4/addr3.... that line contains ALL the data. It is like double posting, see example3 below
Example3:
[[This is the msgbox result, note line 4]]
Name: Joe Shmoe
Addr1: 123 Fake St
Addr2: Townsville, CA (no appartment)
Addr3: Name: Joe ShmoeAddr1: 123 Fake StAddr2: Townsville, CAAcct#: 12345678931216549874321
Acct#: 12345678931216549874321
TN1: 555-555-1234
TN2: 555-555-1235
So, the Addr3 line actually contains ALL the data that the msgbox pops up. The other lines, Addr2, Addr1, name, acct, TN... still populate as needed.
Thoughts? |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Wed Feb 24, 2010 8:23 pm Post subject: |
|
|
Hmm...maybe this?
| Code: | Clipboard := COM_Invoke(pwb,"document.getElementById[divInfo].innerText")
Loop, parse, Clipboard, `n,
If A_LoopField is not Space
{
Info .= ((A_Index=1) ? "" : "`n") A_LoopField
If (A_Index = 4)
Break
}
StringSplit, Info, Info, `n
;and mgsbox out as a test with this:
msgbox,
(
CustName: %Info1%
Address1: %Info2%
Address2: %Info3%
Address3: %Info4%
Acct Num: %AcctNum1%
.......
) |
EDIT: Corrected the ternary, please try it again. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies.
Last edited by sinkfaze on Wed Feb 24, 2010 10:08 pm; edited 2 times in total |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Wed Feb 24, 2010 9:20 pm Post subject: |
|
|
| The actual code you posted gives an error becuase it is missing an ')', I changed it to Info .= (A_Index=1 ? "" : "`n") ? A_LoopField which also doesn't work, it makes 1,2,3 and 4 empty, as oppsed to 3 or 4. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Wed Feb 24, 2010 9:24 pm Post subject: |
|
|
For what its worth (haven't looked at your question really) for parsing loops I ALWAYS use | Code: | | Loop, parse, Clipboard, `n, `r | just to be sure the A_LoopField doesn't include the `r char which may cause issues... my 2cts _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Wed Feb 24, 2010 9:46 pm Post subject: |
|
|
| hugov wrote: | For what its worth (haven't looked at your question really) for parsing loops I ALWAYS use | Code: | | Loop, parse, Clipboard, `n, `r | just to be sure the A_LoopField doesn't include the `r char which may cause issues... my 2cts |
@HugoV
Good point, I need to be in that habbit too. Thanks.
(Didn't change the script results though)
@Sinkfaze
Tried the corrected changes, lines 1 2 3 and 4 are now all blank, but lines 5+ still work. I am not questioning your help, but I have a 'question' about it. Why would you put a_index=1 up front? As opposed to adding the index number to the end of 'info' |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Wed Feb 24, 2010 10:13 pm Post subject: |
|
|
Sorry Carcophan, it needed correcting again (off day ), try it again. All this is:
| Code: | | ((A_Index=1) ? "" : "`n") |
Is a ternary designating when newlines will be inserted. On the first iteration of the parsing loop, presumably the variable we're saving to is blank so no newline will need to be inserted but for every iteration after that one is needed. So in a four-iteration parsing loop that ternary will do this:
| ternary pattern wrote: | | A_LoopField "`n" A_LoopField "`n" A_LoopField "`n" A_LoopField |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Thu Feb 25, 2010 3:53 am Post subject: |
|
|
| sinkfaze wrote: | | Sorry Carcophan.... |
Ha. Look, don't apologize, I appreciate the help. I will run this tomorrow when I get in. Your latest example/explanation actually makes a lot of sense the way you worded it. Thanks boss. |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Thu Feb 25, 2010 6:56 pm Post subject: |
|
|
So I've finally had a chance today to play around with this.
Sadly the latest example also does not work. I did notice though, when rereading your comment: | Quote: | | presumably the variable we're saving to is blank so no newline will need to be inserted |
did you mean the variable itself, or the field in which is trying to populate the variable WITH data?
Field #1, #2 and #3 should 'always' contain data, field #4 is the 'optional' one. Field #4 would only ever have content if the customers dwelling is an apartment or needs other address information.
I have changed the loop break from index=4 to index=3, and works as a temporary fix. Downside is changing it to '3' cuts off information WHEN there is the 4th field, but as most accounts only have 3.... i can live with a few accounts missing 1 bit of info.
The A_Index being up front though, will always display 1,2,3 and 4 as blank for some reason. Which is probably happening becuase we are assuming variable1 is blank.
I wish I could just take a screenshot of the msgbox sample to show you exactly how the code come up in field4 in error, but cannot bc of security stuff.
| Code: |
;as an example, I have tried the following and other variations
CustInfo := (CustInfo = "") ? A_LoopField : CustInfo . "`n" . A_LoopField ;works as long as index=3/break, index=4 causes error
;CustInfo .= (A_Index=1 ? "" : "`n") ? A_LoopField
;CustInfo .= ((A_Index=1) ? "1" : "`n") ? A_LoopField
;CustInfo := ((A_Index=1) ? "`n" : "") ? A_LoopField
;CustInfo := ((A_Index=1) ? "`n" : "")
;CustInfo := (CustInfo = "") ? A_LoopField : CustInfo . "`n" . A_LoopField
;CustInfo := ((A_Index=1) ? "`n" : "") ? A_LoopField : CustInfo . "`n" . A_LoopField
;CustInfo := ((CustInfo = "") ? "`n" : "") ? A_LoopField : CustInfo . "`n" . A_LoopField
If (A_Index =3) ; || CustInfo4 = "") ;shouldn't be 'three', bc there are 4 address fields
Break
;If (A_Index = 4) ;works if only 3 address fields, 4th filled with error
;Break
;If (A_Index =3 || CustInfo4 = "") ;nope
;Break
|
|
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Thu Feb 25, 2010 6:59 pm Post subject: |
|
|
Can you post a sample of what's in the variable before you attempt to parse it (both with and without the blank field)? _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Thu Feb 25, 2010 7:39 pm Post subject: |
|
|
We have our famous tool, that I have hinted at various times in the past. The Kiosk style IE page, that is a skin for an old DOS-type database.
Data is all over the place, but house/customer info is in a set location. The address/house/customer info is also always (luckly) in the same order.
Information in our tool, displays as follows::
Name (Line1 = First/Last)
Address (Line2= Street)
Address (Line3= TOWN/zip, unless it is an appartmnet, then it is the appt #)
Address (Line4= TOWN if there was an appartment number in line 3, BLANK if no apparment)
Rough sample IF A_INDEX=4:
1.) Joe Blow
2.) 123 Fake St
3.) Apt #7D
4.) Townsville, XY 12345
5.) Account number
6 - 10) Whatever else, working with/without index=4
1.) Joe Blow
2.) 123 Fake St
3.) Townsville, XY 12345
4.) <contains data for fields 5, 6, 7, 8 ,9 and 10>
5.) Account number
6 - 10) Whatever else, working with/without index=4
Changing a_Index to 3, removed an/all content that would be displayed in line #4. So in theory it would cut off the persons Town name/state/zip, just becuase they do not live in an appartment building.
| Quote: | | Can you post a sample of what's in the variable before you attempt to parse it (both with and without the blank field)? |
I just used the line "Clipboard := COM_Invoke(pwb,"document.getElementById[divCustInfo].innerText")" and msgbox % clipboard.
The msgbox, exactly as it appears with no parse/delimiting(with fake info obviously):
| Code: |
Homer Simpson
123 Fake Street
SpringField, XY 01234
Account: 123456789123456789SSN: XXX-XX-XXXXHome Phone: (123) 123-1234Other Phone: (000) 000-0000Email Address: xyz@somewhere.commSecondaryEmail: noneBilling Address
|
| Code: |
Homer Simpson
123 Fake Street
Unit# 69-F
SpringField, XY 01234
Account: 123456789123456789SSN: XXX-XX-XXXXHome Phone: (123) 123-1234Other Phone: (000) 000-0000Email Address: xyz@somewhere.commSecondaryEmail: noneBilling Address
|
Note the white space, and lack of it, between the data past account number.
Also, notice the name on line 1, address on 2 and 3. The -ONLY- difference between this msgbox and one with an 'appartment' is that the appartment info will be up there. The remaining data from 'account number' to the end.... is in that long single string, missing whitespace and stuff, as shown. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Thu Feb 25, 2010 9:31 pm Post subject: |
|
|
This one is bizarrely more difficult than it looks. A parsing loop is not sufficient, since it will only loop for the number of delimiters found; similar a while loop will also not work for the same reason. If the number of loop iterations is less than the amount of variables needed the task simply becomes too cumbersome to work with well. What you have to do is create a pseudo while loop that can work based off of the number of delimiters in the file (in this case, newlines). If there are 4 newlines then there is data in the apartment field; if only three it is blank, Here are two tests of the code:
| Code: | var=
(
Homer Simpson
123 Fake Street
Unit# 69-F
SpringField, XY 01234
Account: 123456789123456789SSN: XXX-XX-XXXXHome Phone: (123) 123-1234Other Phone: (000) 000-0000Email Address: xyz@somewhere.commSecondaryEmail: noneBilling Address
)
RegExReplace(var,"`n","`n",e) ; counts the number of newlines and saves to var 'e'
Pos=1 ; needed for pseudo while loop
Loop 4 {
if (A_Index=3) && (e=3) { ; if A_Index is 3 and 3 newlines (no apt field)
line%A_Index%:=""
continue
}
Pos:=RegExMatch(var,"s).*?`n",m,Pos+StrLen(m))
line%A_Index%:=RegExReplace(m,"`n")
}
RegExMatch(var,"s).*",line5,Pos+StrLen(m))
MsgBox, % "Name: " line1
. "`nAddress1: " line2
. "`nAddress2: " line3
. "`nAddress3: " line4
. "`nOther: " line5
return |
| Code: | var=
(
Homer Simpson
123 Fake Street
SpringField, XY 01234
Account: 123456789123456789SSN: XXX-XX-XXXXHome Phone: (123) 123-1234Other Phone: (000) 000-0000Email Address: xyz@somewhere.commSecondaryEmail: noneBilling Address
)
RegExReplace(var,"`n","`n",e) ; counts the number of newlines and saves to var 'e'
Pos=1 ; needed for pseudo while loop
Loop 4 {
if (A_Index=3) && (e=3) { ; if A_Index is 3 and 3 newlines (no apt field)
line%A_Index%:=""
continue
}
Pos:=RegExMatch(var,"s).*?`n",m,Pos+StrLen(m))
line%A_Index%:=RegExReplace(m,"`n")
}
RegExMatch(var,"s).*",line5,Pos+StrLen(m))
MsgBox, % "Name: " line1
. "`nAddress1: " line2
. "`nAddress2: " line3
. "`nAddress3: " line4
. "`nOther: " line5
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Thu Feb 25, 2010 10:16 pm Post subject: |
|
|
Dumb question, but can I just plug in my 'clipboard' variable inplace of the 'var' variable?
and I should also be able to .= the values of my other regex stuff, output 'line1, line2, line3, line4, regexVar1, regexVar2, var3...'
I didn't think it would turn into something so complicated. Thanks for all your time with this boss. I can't test this till tomorrow again, but it makes a lot of sense the way you layed it out. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Thu Feb 25, 2010 11:03 pm Post subject: |
|
|
Yes you could just replace var with Clipboard to test.
| Carcophan wrote: | | and I should also be able to .= the values of my other regex stuff, output 'line1, line2, line3, line4, regexVar1, regexVar2, var3...' |
Well, the pseudo-while loop only concerns itself with vars 1 through 4, you could match for other info, like this:
| Code: | var=
(
Homer Simpson
123 Fake Street
SpringField, XY 01234
Account: 123456789123456789SSN: XXX-XX-XXXXHome Phone: (123) 123-1234Other Phone: (000) 000-0000Email Address: xyz@somewhere.commSecondaryEmail: noneBilling Address
)
RegExReplace(var,"`n","`n",e)
Pos=1
Loop 4 {
if (A_Index=3) && (e=3) {
line%A_Index%:=""
continue
}
Pos:=RegExMatch(var,"s).*?`n",m,Pos+StrLen(m))
line%A_Index%:=RegExReplace(m,"`n")
}
RegExMatch(var,"is)(?<=Account: ).*(?=SSN:)",Acct)
RegExMatch(var,"is)(?<=Home Phone: ).*(?=Other Phone:)",HomePh)
RegExMatch(var,"is)(?<=Email Address: ).*(?=SecondaryEmail:)",Email)
MsgBox, % "Name: " line1
. "`nAddress1: " line2
. "`nAddress2: " line3
. "`nAddress3: " line4
. "`nAccount: " Acct
. "`nHome Phone: " HomePh
. "`nEmail: " Email
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Carcophan
Joined: 24 Dec 2008 Posts: 1308 Location: :noitacoL
|
Posted: Fri Feb 26, 2010 12:08 am Post subject: |
|
|
| sinkfaze wrote: |
RegExMatch(var,"is)(?<=Account: ).*(?=SSN:)",Acct)
RegExMatch(var,"is)(?<=Home Phone: ).*(?=Other Phone:)",HomePh)
RegExMatch(var,"is)(?<=Email Address: ).*(?=SecondaryEmail:)",Email) |
This is actually very close to the code I was planning on using (am using thanks to a coworker) !!
| me wrote: |
RegExMatch( Clipboard, "Account: (.*)SSN:", AcctNum)
RegExMatch( Clipboard, "Home Phone: (.*) Other", HomePhone)
.....
|
Real trick will be, when I figure out what the difference between your and my examples. I need to get the CHM out and compare ! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|