computergroove Posted August 5, 2016 Posted August 5, 2016 I have a file that has a large amount of line items like the following: Terms= Sub Total=2.49 Invoice Discount $= Tax Percentage=6 Sales Tax Amount or 'EXEMPT'=0.00 Invoice Total=2.49 Total Paid=0.00 Amount Due=2.49 Change=0.00 Clerk Name= Clerk Number=9998 I need to know how I can extract the dollar value after "Amount Due=" In this case its 2.49. Sometimes it will be between 0.01 and 20.00. How can I get that value? Also note that the number of items above the Amount Due= changes so I cannot use filereadline with the line number. Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html
computergroove Posted August 5, 2016 Author Posted August 5, 2016 Tried this with no luck $AmountDue = StringSplit($FileString,"Amount Due=,Change=") Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html
computergroove Posted August 5, 2016 Author Posted August 5, 2016 This worked: $AmountDue = _StringBetween($FileString,"Amount Due=","Change=") _ArrayDisplay($AmountDue) Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html
InunoTaishou Posted August 5, 2016 Posted August 5, 2016 (edited) #include <File.au3> #include <String.au3> Global $aFile _FileReadToArray(@ScriptDir & "\Data.txt", $aFile, $FRTA_NOCOUNT) For $i = 0 to UBound($aFile) - 1 Local $aLine = StringSplit($aFile[$i], "=", $STR_NOCOUNT) ConsoleWrite("Key: " & $aLine[0] & " | Value: " & $aLine[1] & @LF) If ($aLine[0] = "Amount Due") Then MsgBox("", "Amount Due", "Amount Due = " & $aLine[1]) EndIf Next Here's another way. In case the line after Amount due isn't Change Here's a regexp way (It's been a long time since I've done regexp so there's probably a much better way) Global $sFile = FileRead(@ScriptDir & "\Data.txt") Global $aRegex = StringRegExp($sFile, '(?i)Amount Due=(.*)', 3) If (Not @Error) Then MsgBox("", "Amount Due", "Amount Due = " & $aRegex[0]) EndIf Edited August 5, 2016 by InunoTaishou
kylomas Posted August 6, 2016 Posted August 6, 2016 (edited) computergroove, A little more stringent regexp. Allows for ".99". #include <array.au3> local $aResult = stringregexp(fileread(@scriptdir & '\test10.txt'),'Amount Due=((?:\d+)?\.\d+)',3) _arraydisplay($aResult) kylomas edit: file used for testing test10.txt Edited August 6, 2016 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
TheDcoder Posted August 6, 2016 Posted August 6, 2016 #include <Array.au3> Global Const $FILE = @ScriptDir & '\test.txt' Global Const $KEY = "Amount Due=" Global Const $KEY_LEN = StringLen($KEY) Global $aFile = FileReadToArray($FILE) Global $iLines = @extended Global $sAmounts For $iLine = 0 To $iLines - 1 If StringLeft($aFile[$iLine], $KEY_LEN) = $KEY Then $sAmounts &= StringTrimLeft($aFile[$iLine], $KEY_LEN) & '|' EndIf Next Global $aAmounts = StringSplit(StringTrimRight($sAmounts, 1), '|') _ArrayDisplay($aAmounts) EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
Robjong Posted August 8, 2016 Posted August 8, 2016 (edited) @kylomas Any particular reason you went with 'Amount Due=((?:\d+)?\.\d+)' instead of something like this 'Amount\h+Due=(\d*\.\d+)' ? While we are at it we may as well include negative amounts and match case insensitive. '(?i)Amount\h*Due=(-?\d*\.\d+)' Edited August 8, 2016 by Robjong added suggestion
mikell Posted August 8, 2016 Posted August 8, 2016 (edited) On 6/8/2016 at 1:29 AM, InunoTaishou said: Here's a regexp way (It's been a long time since I've done regexp so there's probably a much better way) Global $sFile = FileRead(@ScriptDir & "\Data.txt") Global $aRegex = StringRegExp($sFile, '(?i)Amount Due=(.*)', 3) If (Not @Error) Then MsgBox("", "Amount Due", "Amount Due = " & $aRegex[0]) EndIf Why do you underestimate your way ? it's simple and correct Could be a little more precise using \S* instead of .* but really, this is just a detail Edit Also works with "Amount Due=3" Edited August 8, 2016 by mikell
kylomas Posted August 8, 2016 Posted August 8, 2016 Robjong, Hi, nice to see you around again. The editing was just to ensure digits with optional dollar amount and ensures a decimal point. Given the vague user requirements I just gave a nudge toward considering regex. Mikell may be right and there is no need for a more precise edit. Kykomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now