computergroove

String extraction question

9 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#4 ·  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

Share this post


Link to post
Share on other sites

#5 ·  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

Share this post


Link to post
Share on other sites
#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)

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#8 ·  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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • FroVN
      By FroVN
      Hi, i have a problem :" can't set the name of file with a special character like: \;/;";|;...  have anyway to short the StringInSrt and Stringreplace? i am using this code but too long
      $title=InputBox(0,'','','')
         if StringInStr($title,'\') or StringInStr($title,'/') or StringInStr($title,':') or StringInStr($title,'*') or StringInStr($title,'?') or StringInStr($title,'"') or StringInStr($title,'<') or StringInStr($title,'>') or StringInStr($title,'|') Then
             $title=StringReplace($title,'\','-')
              $title=StringReplace($title,'/','-')
               $title=StringReplace($title,':','-')
                $title=StringReplace($title,'*','-')
                 $title=StringReplace($title,'?','-')
                  $title=StringReplace($title,'"','-')
                   $title=StringReplace($title,'<','-')
                    $title=StringReplace($title,'>','-')
                     $title=StringReplace($title,'|','-')
         EndIf
       
    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question related to strings items in an Array and sorting. Maybe someone can advice me how to solve the issue.
      I have an Array of strings, every item of the Array is as following:
      INFO [13.06.2017 11:48:01] [Thread-13] [ConGenImpUsb -> waitForConnection]  INFO [07.06.2017 08:55:44] [main] MDU5 - Ver 5.1x I want to sort the item in the array by date and time, is there any function which allows me to sort by date/time?
      Thanks in advance