Jump to content

Find and replace some numbers after calculation.


Recommended Posts

Hello friends!

This is my first post here (so please be patient - i am not a professional).

What i want to do is the following:

I want to open a txt format file,

find and replace some numbers.

The syntax is not allways the same inside the text file.

The numbers are allways after a specific latter.

For example:

T00 A100.2 B11.0

T01 A119.3 B211.8 C21.4 D21.0

So i would like to add certain values. For example after "A" i want to add +100.6 and after "B" +150.0.

Finaly the file will look like:

T00 A200.8 B161.0

T01 A219.9 B361.8 C21.4 D21.0

I have found in the forums some useful code for FileRead and FileReplace,

but still not enough for me.

Can anyone help?

Link to comment
Share on other sites

  • Moderators

giannis121,

Welcome to the AutoIt forum. :graduated:

Here is a way to do what you want:

#include <Array.au3>

$sInput = "T00 A100.2 B11.0" & @CRLF & "T01 A119.3 B211.8 C21.4 D21.0"

; Split into lines
$aInput_Lines = StringSplit($sInput, @CRLF, 1)
; Take each line in turn
For $i = 1 To $aInput_Lines[0]
    ; Split the line into elements
    $aInput_Items = StringSplit($aInput_Lines[$i], " ")
    ; This will hold the recreated line
    $sTempLine = ""
    ; For each element in the line
    For $j = 1 To $aInput_Items[0]
        Switch StringLeft($aInput_Items[$j], 1)
            ; if it begins with A
            Case "A"
                ; Remove the A
                $sTempItem = StringTrimLeft($aInput_Items[$j], 1)
                ; Convert the number into an actual number and add as required
                $iTempNumber = Number($sTempItem) + 100.6
                ; Add to the new line with a trailing space
                $sTempLine &= "A" & $iTempNumber & " "
            Case "B"
                $sTempItem = StringTrimleft($aInput_Items[$j], 1)
                $iTempNumber = Number($sTempItem) + 150
                $sTempLine &= "B" & $iTempNumber & " "
            Case Else
                $sTempLine &= $aInput_Items[$j] & " "
        EndSwitch
    Next
    ; Replace the exisitng line with the new one after removing the final space
    $aInput_Lines[$i] = StringTrimRight($sTempLine, 1)

Next
; Convert the array of lines back into a string
$sOutput = _ArrayToString($aInput_Lines, @CRLF, 1)
; Display it
ConsoleWrite($sOutput & @CRLF)

Please ask if you have any questions on what is going on in the script. :(

You could also use an SRE - but that might be rushing things a bit! :D

M23

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

Thank you for the reply!

Can you make it more specific for me, please?

The input data is a file "test.txt" (see attached file)

The values for adding to the existing ones are inside the "value.txt"

The original text inside the "test.txt" will not be touched.

Under it there will be the new calculated text.

If you find it difficult to create it for me,

please just give me the commands & functions that i may need to use.

Thank you in advance.

Giannis

test.txt

values.txt

Link to comment
Share on other sites

  • Moderators

giannis121,

- Use FileRead to get the text from the original file.

- Use the code I gave you to amend the text.

- Use FileWrite to append the new text to the file.

You will now have a file which contains both sets of text.

Try it yourself and come back if you have problems. :graduated:

M23

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

  • Moderators

giannis121,

My pleasure! :graduated:

Always nice to help those who help themselves too! :(

M23

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

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...