Jump to content

Read Numbers in a line and add them


Recommended Posts

I am reading a specific line in a log file. I need help to extract the numbers after specific text and then add these numbers. Another option would be to read the last 12 characters of the line, extract only numbers and then add them

 AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213

From this line I want to extract 12, 20 and 213 and then add them 12+20+213=245

1- I can read the last 12 characters, extract only numbers and then add them

2- I can read the text after 'TIM XON LIST' , extract only numbers and then add them

Thank you 

Link to comment
Share on other sites

$str = "AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213"

msgbox(0, '' , Execute(StringRegExpReplace(stringmid($str , stringinstr($str , " " , 0 , -1) + 1) , "\D" , "+")))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

Vikramjeet,

Not difficult at all. What have YOU tried that has not worked?

M23

P.S. If you get stuck:

Spoiler
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

$sLine = "AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213 "
; Extract digits from the final 12 characters
$aRet = StringRegExp(StringRight($sLine, 12), "(\d+)", $STR_REGEXPARRAYGLOBALMATCH)
; Declare variable to hold total
$iTotal = 0
; Loop through the array to add the values
For $i = 0 To UBound($aRet) - 1
    $iTotal += $aRet[$i]
Next
; And here it is
MsgBox($MB_SYSTEMMODAL, "Total", $iTotal)

 

Edit:

iamtheky - nice!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@iamtheky

Global $strString = "AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213", _
       $arrResult, _
       $strNumbers = "", _
       $fltSum = 0.0


$arrResult = StringRegExp($strString, ".{1}(\d{2}).{1}(\d{2}).{1}(\d{3})", 1)

For $i = 0 To UBound($arrResult) - 1 Step 1
    If $i < UBound($arrResult) - 1 Then
        $strNumbers &= $arrResult[$i] & "+"
    Else
        $strNumbers &= $arrResult[$i]
    EndIf

    $fltSum += $arrResult[$i]
Next

ConsoleWrite("Sum of " & $strNumbers & " = " & $fltSum & @CRLF)

What do you think about?
Please, let me know :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

33 minutes ago, Melba23 said:

Vikramjeet,

Not difficult at all. What have YOU tried that has not worked?

M23

P.S. If you get stuck:

  Reveal hidden contents
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

$sLine = "AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213 "
; Extract digits from the final 12 characters
$aRet = StringRegExp(StringRight($sLine, 12), "(\d+)", $STR_REGEXPARRAYGLOBALMATCH)
; Declare variable to hold total
$iTotal = 0
; Loop through the array to add the values
For $i = 0 To UBound($aRet) - 1
    $iTotal += $aRet[$i]
Next
; And here it is
MsgBox($MB_SYSTEMMODAL, "Total", $iTotal)

 

Edit:

iamtheky - nice!

I was able to extract ALL numbers from the string. However i was not able to extract the specific numbers 

Link to comment
Share on other sites

3 hours ago, FrancescoDiMuro said:

@iamtheky

Global $strString = "AX  123 26OCT   ABC 23   1800  UHS TIM XON LIST   A12B20C213", _
       $arrResult, _
       $strNumbers = "", _
       $fltSum = 0.0


$arrResult = StringRegExp($strString, ".{1}(\d{2}).{1}(\d{2}).{1}(\d{3})", 1)

For $i = 0 To UBound($arrResult) - 1 Step 1
    If $i < UBound($arrResult) - 1 Then
        $strNumbers &= $arrResult[$i] & "+"
    Else
        $strNumbers &= $arrResult[$i]
    EndIf

    $fltSum += $arrResult[$i]
Next

ConsoleWrite("Sum of " & $strNumbers & " = " & $fltSum & @CRLF)

What do you think about?
Please, let me know :)

Thank you so much Francesco

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...