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 

Ignor a minus sign?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
artbasement



Joined: 01 Sep 2008
Posts: 52

PostPosted: Sat Oct 24, 2009 11:46 pm    Post subject: Ignor a minus sign? Reply with quote

This is probably very simple to fix.

I have text in a file like this

002=SMALL CAPPUCCINO -1.50
002= REFUND
002=SMALL HOT CHOCOLATE -1.50
002= REFUND
002=YORKSHIRE TEA -1.35
002= REFUND
002= TAKE AWAY
002= ------------
002= SUBTOTAL (lots of spaces here) -4.35
002= C a r d - 4 . 3 5
002= CHRIS
002= #002-008-0055-0001 24/10/2009 11:26-R



and I need to copy the amount after SUBTOTAL to another text file but without the minus sign. As the number gets more digits it moves nearer the SUBTOTAL so I can't just define "SUBTOTAL -"

How can I define any amount of spaces before the "-" or ignor the "-"?

Or maybe there is another way?

Here is the code so far, top part works fine for a sale, lower part is for a refund but brings the "-" with the number.

Code:

loop
{
FileRead, currslip, c:\Uniwell\currslip.txt

StringReplace, currslip,currslip, `r`n, `n, All
StringReplace, currslip,currslip, `r, `n, All
StringSplit, L,currslip, `n

If (instr(currslip,"C a r d")) AND NOT (instr(currslip,"- "))

{
  needle := "SUBTOTAL                    "
  halfslip := SubStr(currslip, InStr(currslip, needle) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L7, %A_Space%
  output = 2=0`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\Uniwell\input.txt
  Sleep, 100
  Filedelete c:\Uniwell\currslip.txt
}

If (instr(currslip,"- ")) AND (instr(currslip,"C a r d"))

{
  needle := "SUBTOTAL"
  halfslip := SubStr(currslip, InStr(currslip, needle) + StrLen(needle))
  amount := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )

  StringSplit, T, L14, %A_Space%
  output = 2=20`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\Uniwell\input.txt
  Sleep, 100
  Filedelete c:\Uniwell\currslip.txt
}

}
return
[/u]
Back to top
View user's profile Send private message MSN Messenger
sinkfaze



Joined: 18 Mar 2008
Posts: 2424

PostPosted: Sun Oct 25, 2009 12:30 am    Post subject: Reply with quote

Well you're trying to obtain information more by the pattern of the characters than the characters themselves, sounds like a job for regular expressions:

Code:
loop
{
  FileRead, currslip, c:\Uniwell\currslip.txt
  FileAppend
    , % RegExReplace(currslip,".*SUBTOTAL.*?(\d+\.\d+).*","$1")
    , c:\Uniwell\input.txt
  Sleep, 100
  Filedelete c:\Uniwell\currslip.txt
}
return

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message
artbasement



Joined: 01 Sep 2008
Posts: 52

PostPosted: Sun Oct 25, 2009 1:33 am    Post subject: Reply with quote

Just got it like this....

Code:

If (instr(currslip,"- ")) AND (instr(currslip,"C a r d"))

{
  needle := "SUBTOTAL"
  halfslip := SubStr(currslip, InStr(currslip, needle) + StrLen(needle))
  refund := SubStr(halfslip, 1, InStr(halfslip, "`n") - 1 )
  stringReplace, amount, refund, `-, ,

  StringSplit, T, L14, %A_Space%
  output = 2=20`n1=%T1%`n3=%amount%`n99=0
  FileAppend, % output, c:\Uniwell\input.txt
  Sleep, 100
  Filedelete c:\Uniwell\currslip.txt
}

}
return
Back to top
View user's profile Send private message MSN Messenger
garry



Joined: 19 Apr 2005
Posts: 1498
Location: switzerland

PostPosted: Sun Oct 25, 2009 9:47 am    Post subject: Reply with quote

some examples
Example=1
Code:
setworkingdir, %a_scriptdir%
transform,S,chr,32   ;SPACE

F1=%A_scriptdir%\input12.txt
F2=%A_scriptdir%\output12.txt
;filedelete,%F1%

;------- for test --------------
AA=
(
002= SMALL CAPPUCCINO -1.50`r
002= SUBTOTAL   -1054.35   `r
002= C a r d - 4 . 3 5`r
002= CHRIS`r
)

ifnotexist,%F1%
  fileappend,%AA%,%F1%
ifexist,%F2%
  filedelete,%F2%
;-----------------------------------

FileRead, FileContent, %F1%
Loop, Parse, FileContent, `n, `
  {
  LR=%A_Loopfield%
  if A_LoopField contains SUBTOTAL
     stringreplace,LR,LR,-,%S%,all
  if A_LoopField contains C a r d
     stringreplace,LR,LR,-,%S%,all
  fileappend,%LR%,%F2%
  }
run,%F2%
return


Example=2 ABS removes minus sign
Code:

setworkingdir, %a_scriptdir%
transform,S,chr,32   ;SPACE
SetFormat, float, 0.2
F1=%A_scriptdir%\input12.txt

;------- for test --------------
AA=
(
002= SMALL CAPPUCCINO -1.50`r
002= SUBTOTAL   -1054.35   `r
002= C a r d - 4 . 3 5`r
002= CHRIS`r
)

ifnotexist,%F1%
  fileappend,%AA%,%F1%

SEARCHFOR=SUBTOTAL
   Loop,Read,%F1%
      {
      if A_LoopReadLine contains %SEARCHFOR%
         {
         Loop, Parse,A_LoopReadLine ,%S%,`
             {
             C=%A_LoopField%
             if (A_LoopField="")
                continue
             A=%C%
             }
          B:=(ABS(A))
          msgbox,%B%
         }
      }
return
Back to top
View user's profile Send private message
artbasement



Joined: 01 Sep 2008
Posts: 52

PostPosted: Sun Oct 25, 2009 2:49 pm    Post subject: Reply with quote

Code:
  stringReplace, amount, refund, `-, ,

Works well.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   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