Jump to content

match data names, insert data in text file columns


Recommended Posts

i have 6 columns (the 3rd column is empty from left to right in the example) in a text file
eg:
itemname1 61 itemname2 52
itemname1 612 itemname2 52
itemname2 613 itemname3 24
itemname3 62 itemname3 24
itemname3 631 itemname1 35

result needed is:
itemname1 61 35 itemname2 52
itemname1 612 35 itemname2 52
itemname2 613 52 itemname3 24
itemname3 62 24 itemname3 24
itemname3 631 24 itemname1 35

column 4 matches column 1, column 6 gets inserted on column 3 after the mach of column 4 with column 1

Link to comment
Share on other sites

  • Moderators

That is awesome, you have successfully told us what you want. Now, how about showing what you have tried on your own?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

You are missing the point. This forum is for helping people with their own scripts; it is not a place where you put in a request and we barf up code for you. Post what you have tried on your own, and we will be happy to help you get it going.

For starters, why don't you look at FileReadToArray in the help file, and try out the example.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Someone might find this example interesting.
  - The added column method "()";
  - Imagining your 1D array as a 2D array; and,
  - The "Re-constitute the 1D array into text" simple code snippet.

#include <Array.au3>
; http://www.autoitscript.com/forum/topic/162613-match-data-names-insert-data-in-text-file-columns/#entry1182507

;Local $sFile = FileRead("FullPath\File\Name.txt")
; or
Local $sFile = _
        "itemname1 61 itemname2 52" & @CRLF & _
        "itemname1 612 itemname2 52" & @CRLF & _
        "itemname2 613 itemname3 24" & @CRLF & _
        "itemname3 62 itemname3 24" & @CRLF & _
        "itemname3 631 itemname1 35"
Local $sFile2, $iCols = 5 ; Total number of columns in resulting text, $sFile2.

Local $arr = StringRegExp($sFile, "(\S+)\h+(\S+)()\h+(\S+)\h+(\S+)", 3)
; ----------------------------------------------^ This 3rd capture group reg exp pattern , "()", creates the
; equivalent of a new, blank 3rd column inserted into an equivalent 2D array.  This conceptural 2D array has
; its row elements structured sequentially, row after row in the created 1D array.
_ArrayDisplay($arr, "Note :- New, blank 3rd column")

; Insert data into equivalent 3rd column of imaginary 2D array but actually using the 1D array.
For $i = 0 To UBound($arr) - 1 Step $iCols ; $arr[$i] is equivalent of accessing the data in the 1st column, "Col 0" of each row of a 2D array.
    For $j = 0 To UBound($arr) - 1 Step $iCols ; "$j" For..Next loop is searching for a match loop.

        ; Find when the data in the 1st column of each row, "$arr[$i]", equals the data in the new 4th column, "$j+$iCols-2", of any row.
        If $arr[$i] == $arr[$j + $iCols - 2] Then
            $arr[$i + 2] = $arr[$j + $iCols - 1] ; Then make the new, blank 3rd column (of the $i For..Next loop) equal to
            ;                                      the new 5th column, or last column (of the $j For..Next loop).
            ExitLoop 1
        EndIf
    Next
Next

; Re-constitute the 1D array into text.
For $i = 0 To UBound($arr) - 1
    $sFile2 &= $arr[$i] & (Mod($i + 1, $iCols) ? " " : @CRLF)
Next

ConsoleWrite(StringTrimRight($sFile2, 2) & @LF) ;<========= "StringTrimRight" removes the trailing @CR and @LF characters.
_ArrayDisplay($arr)

#cs
    Result is:
    itemname1 61 35 itemname2 52
    itemname1 612 35 itemname2 52
    itemname2 613 52 itemname3 24
    itemname3 62 24 itemname3 24
    itemname3 631 24 itemname1 35
#ce
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...