Results 1 to 15 of 15

Thread: extracting integers from a text file and totalling them up

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    extracting integers from a text file and totalling them up

    hi all just struggling on best way to do this


    this is contents of my richtextbox

    15/04/2021 Job Total Income 300 Job Description ngdhghghghhg Wages Paid 120 Expenses 50 Tax 26

    15/04/2021 Job Total Income 34434 Job Description gfgfgfdggf Wages Paid 43 Expenses 43 Tax 6869.6

    15/04/2021 Job Total Income 344 Job Description gfgfgfdggf Wages Paid 43 Expenses 43 Tax 51.6



    i cant figure out how to loop through and extract all wages paid and add them together

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,540

    Re: extracting integers from a text file and totalling them up

    Show us what you have done so far and we can try and help.
    Please remember next time...elections matter!

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,263

    Re: extracting integers from a text file and totalling them up

    The RichTextBox control has a Lines property, which returns every line in a String array. What I would do is:
    1. Get the lines
    2. Use RegEx to select just the wages paid
    3. Sum the values


    Here is an example:
    Code:
    Dim wagesPaid = From line In RichTextBox1.Lines
                    Where pattern.IsMatch(line)
                    Select Convert.ToInt32(pattern.Match(line).Value.Replace("Wages Paid ", String.Empty))
    Dim total = wagesPaid.Sum()
    Last edited by dday9; Apr 15th, 2021 at 12:34 PM. Reason: Replaced ToDecimal with ToInt32

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    this is what i did but you can see that on the line that contains "Jobtotalstring = Split(RichTextBox1.Text" there is no line index so it is just adding up the first line over and over

    Code:
            Dim TempJobtotal As Decimal = 0
            Dim Jobtotalstring As String
            For Each line As String In RichTextBox1.Lines
                Jobtotalstring = Split(RichTextBox1.Text, "Job Total Income ")(1)
                Jobtotalstring = Split(Jobtotalstring, "Wages Paid ", " ")(0)
                TempJobtotal = TempJobtotal + CDec(Jobtotalstring)
            Next

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    with dday's code im not sure what i am supposed to be declaring pattern as

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    i tried
    Code:
            Dim TempJobtotal As Decimal = 0
            Dim Jobtotalstring As String
            For i = 0 to RichTextBox1.Lines.count - 1
                Jobtotalstring = Split(RichTextBox1.lines(i), "Job Total Income ")(1)
                Jobtotalstring = Split(Jobtotalstring, "Wages Paid ", " ")(0)
                TempJobtotal = TempJobtotal + CDec(Jobtotalstring)
            Next
    index out of range

  7. #7
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,561

    Re: extracting integers from a text file and totalling them up

    Take a step back for a second. Your thread title indicates extracting "from a text file", but then your post mentions data in a rich text box.

    The first question that needs to be asked is where this data is coming from? If it is from a text file, what does the data look like in that text file? Is it exactly (character for character) as it appears in your rich text box? Or are you modifying/formatting the data for display purposes?

    I've seen posts in the past where a user starts with data that is nicely formatted (such as a .csv file), takes that data, formats it in a way that they perceive as being "appropriate for display to the end user", and then wants to extract pertinent information from this formatted text. In those cases, the solution is to work with the original data as-is, rather than trying to work backwards.

    That's like taking a file that contains 1,2,3, turning it into a string of "One two three", and then wanting to write a function that turns the words back into numbers for the sake of adding them together.

    Please address this because it will almost certainly change the direction and complexity of assistance you might need.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,263

    Re: extracting integers from a text file and totalling them up

    Quote Originally Posted by Beanoid View Post
    with dday's code im not sure what i am supposed to be declaring pattern as
    My apologies, I forgot to include the RegEx pattern:
    Code:
    Dim pattern = New Regex("Wages Paid \d*")

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    i am writing to and from a text file and the text file when loaded is displayed in a richtextbox and is displayed exactly as shown above , i cant figure out how to loop through the richtextbox line by line like i would with an array using an index
    Code:
    for  i = 0 to array.count - 1
    array.index(i)
    next

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    Quote Originally Posted by dday9 View Post
    My apologies, I forgot to include the RegEx pattern:
    Code:
    Dim pattern = New Regex("Wages Paid \d*")
    worked perfectly thank you, i dont understand the "\d*" part of the code

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,263

    Re: extracting integers from a text file and totalling them up

    Those are Regular Expression constants. This is the RegEx explained more in detail:
    1. Wages Paid : Literally match those characters
    2. \d: Match any character between 1- 9
    3. *: Match the prior match zero or more times


    So what it is saying is: Match "Wages Paid " followed by any optional digit.

    Thinking about it some more, the * wildcard should be replaced with +. The plus sign says match one or more times.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    ahhhh so using "\d: Match any character between 1- 9" would not work when using decimal numbers

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,263

    Re: extracting integers from a text file and totalling them up

    Correct. The title of your thread stated Integers and so I assumed that was the data type you were actually targeting.

    Number matching in Regular Expressions can range from extremely simple (like my example) to extremely complex (like this one).

    Do you want to support Decimals or are you fine with Integers?

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2017
    Posts
    125

    Re: extracting integers from a text file and totalling them up

    no your solution works perfectly for what i need , but as a novice i like to understand the code instead of just copy and paste

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,263

    Re: extracting integers from a text file and totalling them up

    In that case, let me rewrite my LINQ example in a manner that uses traditional looping techniques. I think you'd be able to understand the .NET code a little bit better that way:
    Code:
    ' declare a new RegEx pattern to match: Wages Paid , followed by 1 or more digits
    Dim pattern = New Regex("Wages Paid \d+")
    
    ' declare an integer to store the total value of each wage paid
    Dim total = 0
    
    ' loop over each line
    For Each line In RichTextBox1.Lines
        ' check if the line matches the RegEx pattern
        If (pattern.IsMatch(line)) Then
            ' get the RegEx match's value
            Dim value = pattern.Match(line).Value
    
            ' remove everything but the numbers
            value = value.Replace("Wages Paid ", String.Empty)
    
            ' convert the String to an Integer
            Dim convertedValue = Convert.ToInt32(value)
    
            ' increment the total
            total += convertedValue
        End If
    Next
    My LINQ statement I originally provided you makes the code in the post much more concise and easier to read, but they both effectively do the same thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width