Jump to content

Recommended Posts

Posted

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

Posted (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 by InunoTaishou
Posted (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 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

Posted
#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

Posted (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 by Robjong
added suggestion
Posted (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 by mikell
Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...