Sign in to follow this  
Followers 0
face

multiply 5+ columns of numbers in a text file (tab)

11 posts in this topic

#1 ·  Posted (edited)

i have a script that multiplies by 3 a single column of numbers (50) in a text file. how can i make it multiply 5+ columns at the same time (in 1 file) exatly as when it multiplies 1 column? its set to multiply by 3(+1) every 50 rows. it gives an error message but it still does the multiplying.

 

#include <File.au3>
$file = @DesktopDir & "temp.txt"
$file_output = @DesktopDir & "output.txt"

Local $array[1]
_FileReadToArray($file,$array)
_FileCreate($file_output)
$hOutput = FileOpen($file_output,2)
$iMultiplier = 3
$iCounter = 1
While True
    For $i = 1 To 50
        FileWriteLine($hOutput, $array[$iCounter] * $iMultiplier)
        $iCounter += 1
    Next
    $iMultiplier += 3
WEnd

FileClose($hOutput)

temp.txt

Edited by face

Share this post


Link to post
Share on other sites



FileReadToArray only creates a 1D array, there's no columns, so you won't be able to do what you want to do. You'd need to loop through the array, one row at a time, and StringSplit the row to get the columns of numbers. Then loop through THAT array to do the actual multiplication. Something like this might do the trick, although you lost me when you mentioned adding 3 to the multiplier, I'll leave that part to you because I have no clue what you meant.

#include <File.au3>
$file = @DesktopDir & "\temp.txt"
$file_output = @DesktopDir & "\output.txt"

Local $array[1]
_FileReadToArray($file, $array)
;~ _FileCreate($file_output) <<<<<<<<<<< not needed for this because the next line will do the same thing
$hOutput = FileOpen($file_output, 2)
$iMultiplier = 3
Global $aTemp, $Text
For $i = 1 To $array[0]
    $aTemp = StringSplit($array[$i], @TAB)
    $text = ""
    For $j = 1 To $aTemp[0]
        $text &= $aTemp[$j] * $iMultiplier & @TAB
    Next
    $text = StringTrimRight($text, 1)
    FileWriteLine($hOutput, $text)
Next

FileClose($hOutput)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

 

FileReadToArray only creates a 1D array, there's no columns, so you won't be able to do what you want to do. You'd need to loop through the array, one row at a time, and StringSplit the row to get the columns of numbers. Then loop through THAT array to do the actual multiplication. Something like this might do the trick, although you lost me when you mentioned adding 3 to the multiplier, I'll leave that part to you because I have no clue what you meant.

#include <File.au3>
$file = @DesktopDir & "\temp.txt"
$file_output = @DesktopDir & "\output.txt"

Local $array[1]
_FileReadToArray($file, $array)
;~ _FileCreate($file_output) <<<<<<<<<<< not needed for this because the next line will do the same thing
$hOutput = FileOpen($file_output, 2)
$iMultiplier = 3
Global $aTemp, $Text
For $i = 1 To $array[0]
    $aTemp = StringSplit($array[$i], @TAB)
    $text = ""
    For $j = 1 To $aTemp[0]
        $text &= $aTemp[$j] * $iMultiplier & @TAB
    Next
    $text = StringTrimRight($text, 1)
    FileWriteLine($hOutput, $text)
Next

FileClose($hOutput)

thank you,

it works well, can it be made to use this part? how can i implement this, i need it to multiply by +1 multiplier every 50 rows

    For $i = 1 To 50

        FileWriteLine($hOutput, $array[$iCounter] * $iMultiplier)

        $iCounter += 1

    Next

    $iMultiplier += 3

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I wouldn't even think of putting that into any code, it's written completely wrong. What are you adding 3 to $iMultiplier for if you want it to increase by one every 50 rows? Also, lose the $iCounter variable, it's completely unnecessary.

This might be closer to what you're trying to achieve.

#include <File.au3>
$file = @DesktopDir & "\temp.txt"
$file_output = @DesktopDir & "\output.txt"

Local $array[1]
_FileReadToArray($file, $array)
$hOutput = FileOpen($file_output, 2)
$iMultiplier = 3
Global $aTemp, $Text
For $i = 1 To $array[0]
    $aTemp = StringSplit($array[$i], @TAB)
    If Not Mod($i, 50) Then $iMultiplier += 1
    $text = ""
    For $j = 1 To $aTemp[0]
        $text &= $aTemp[$j] * $iMultiplier & @TAB
    Next
    $text = StringTrimRight($text, 1)
    FileWriteLine($hOutput, $text)
Next

FileClose($hOutput)
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

thanks again

Share this post


Link to post
Share on other sites

BrewManNH,

 

FileReadToArray only creates a 1D array

Not in the latest Beta - that version can return 2D arrays as well. ;)

But do please read the "Script-breaking changes" and look at the Help file before using it. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

I never assume anyone is using the beta version, I always go on the assumption that they're using the latest production version. If something I post is only available in a beta version, I'll mention it, but assuming anything else only leads to multiple confusing posts further down the line.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

how can i make it so it multiplies until a row number

If Not Mod($i, 50) Then $iMultiplier += 1

this multiplies every 50 times and adds +1 on the multiplier

i want to multiply every 50 times and add +1 on the multiplier until a certain row number like 1000 and then every 60 times and add +1 on the multiplier

If Not Mod($i, 60) Then $iMultiplier += 1

Edited by face

Share this post


Link to post
Share on other sites

sorry to bump this post

i really need this to work

Share this post


Link to post
Share on other sites

face,

Please do not bump your own threads within 24hrs - it might be urgent to you but it is most certainly not to anyone else. ;)

And in addition, i have read this thread at least 3 times and I still have absolutely no idea what you are trying to do. How about you give us a short example of the file as it is and then as you want it to be - that might lift the gloom and allow us to help you. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I am really not sure whether I am anywhere near what you want with this example.  You may be able to adapt this example to what you want.

Local $sFilePath = @ScriptDir & "\temp1.txt"
Local $sOutputFilePath = @ScriptDir & "\Output.txt"
Local $iLimit = 50 ; Wanted 50.  Use 2 for testing. <---------------- CHANGE ---------------------
Local $EOF = 1, $iCounter = 0
Local $iMultiplier = 0
Local $sText, $sOutText
Local $hFileOpen = FileOpen($sFilePath)
If FileExists($sOutputFilePath) Then FileDelete($sOutputFilePath)
Local $hFileOutput = FileOpen($sOutputFilePath, 1)

While $EOF
    $sText = ""  ; Initialize $sText for each While...WEnd loop.
    For $i = 1 To $iLimit
        $sText &= FileReadLine($hFileOpen) & @CRLF
        If @error = -1 Then
            $EOF = 0
            ExitLoop ; Exit For..Next loop
        EndIf
        $iCounter += 1
    Next
    $iMultiplier += 3
    If $iCounter >= 1000 Then ; Wanted 1000.  Use 50 for testing. <-------------- CHANGE ---
        $iMultiplier = 3      ; Initialize $iMultiplier after 1000 lines
        $iCounter = 0         ; Initialize counter
        $iLimit += 10         ; Wanted 10 so that 50+10 = 60.  Use 1  for testing. <--------- CHANGE ---
    EndIf

    ; Multiply all columns x $iMultiplier. The number of rows at a time is in $iLimit.
    Local $sNew = StringRegExpReplace($sText, "(\d+)(\D+)(\d+)(\D+)(\d+)(\D+)(\d+)(\D+)(\d+)(\D+)(\d+)(\v*)", _
            "(" & $iMultiplier & " * ${1}) & '${2} '& " & _  ; Column 1
            "(" & $iMultiplier & " * ${3}) & '${4} '& " & _  ; Column 2
            "(" & $iMultiplier & " * ${5}) & '${6} '& " & _  ; Column 3
            "(" & $iMultiplier & " * ${7}) & '${8} '& " & _  ; Column 4
            "(" & $iMultiplier & " * ${9}) & '${10} '& " & _ ; Column 5
            "(" & $iMultiplier & " * ${11}) & @CRLF & ")     ; Column 6

    $sOutText = Execute(StringTrimRight($sNew, 3))
    FileWrite($hFileOutput, $sOutText)
    ;ConsoleWrite($sNew & @LF)
    ;ConsoleWrite("----------------------------------------------" & @LF)
    ;ConsoleWrite($sOutText)
WEnd

FileClose($hFileOpen)
FileClose($hFileOutput)
Edited by Malkey

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
Sign in to follow this  
Followers 0