Sign in to follow this  
Followers 0
rickybobby

Parsing array into columns

15 posts in this topic

I have a text file that has a list in the following format:

          Mon 01/27/2014  11:44:04.16 ComputerName Location 

          Mon 01/27/2014 11:44:04.16 ComputerName2 Location2

     so on and so forth....

I'm trying to parse it so each column is in its own column and own row; but all I can manage is getting each line in its own line, I can't figure out where to go from here...My code is below, any help would be appreciated! 

(The text is separated by a single space, and then a newline) 

$AccessLog = "C:\Users\student-eac\Desktop\autoit scripts\comparetext\accesslog.txt"

$AccessArray = 0 

_FileReadToArray ( $AccessLog, $AccessArray) 

_ArrayDisplay($AccessArray)

Share this post


Link to post
Share on other sites



rickybobby,

Welcome to the AutoIt forum. :)

You are nearly there. Create a new array and split the lines with StringSplit like this:

#include <Array.au3>

; Simulate reading the file into an array
Global $aLines[3] = [2, _
            "Mon 01/27/2014 11:44:04.16 ComputerName Location", _
            "Mon 01/27/2014 11:44:04.16 ComputerName2 Location2"]

; Create a new array
Global $aArray[$aLines[0]][4]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    $aArray[$i -1][0] = $aTmp[1] & " " & $aTmp[2]
    $aArray[$i -1][1] = $aTmp[3]
    $aArray[$i -1][2] = $aTmp[4]
    $aArray[$i -1][3] = $aTmp[5]
Next

_ArrayDisplay($aArray, "", Default, 8)
Please ask if you ahve any questions. :)

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

#5 ·  Posted (edited)

rickybobby,

Welcome to the AutoIt forum. :)

You are nearly there. Create a new array and split the lines with StringSplit like this:

#include <Array.au3>

; Simulate reading the file into an array
Global $aLines[3] = [2, _
            "Mon 01/27/2014 11:44:04.16 ComputerName Location", _
            "Mon 01/27/2014 11:44:04.16 ComputerName2 Location2"]

; Create a new array
Global $aArray[$aLines[0]][4]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    $aArray[$i -1][0] = $aTmp[1] & " " & $aTmp[2]
    $aArray[$i -1][1] = $aTmp[3]
    $aArray[$i -1][2] = $aTmp[4]
    $aArray[$i -1][3] = $aTmp[5]
Next

_ArrayDisplay($aArray, "", Default, 8)
Please ask if you ahve any questions. :)

M23

 

I think you misunderstood me, or I misspoke. I want everything in its own columns, day, time, computer name, etc... 

Also you have the strings I posted in the actual code, would I just replace that with the variable? (the actual file is over 2000 lines)

Thank you! 

 

**EDIT:

so I got this working: 

#include <File.au3>
#include <Array.au3>
$AccessLog = "C:\Users\student-eac\Desktop\autoit scripts\comparetext\accesslog.txt"

;$AccessArray = 0 
$aLines = 0

_FileReadToArray ( $AccessLog, $aLines) 

; Create a new array
Global $AccessArray[$aLines[0]][4]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    $AccessArray[$i -1][0] = $aTmp[1] & " " & $aTmp[2]
    $AccessArray[$i -1][1] = $aTmp[3]
    $AccessArray[$i -1][2] = $aTmp[4]
    $AccessArray[$i -1][3] = $aTmp[5]
Next

_ArrayDisplay($AccessArray, "", Default, 8)

But as I said I wanted 4 columns and X rows, I've tried modifying the code; but I keep getting errors...

Edited by rickybobby

Share this post


Link to post
Share on other sites

What errors are you getting? That's a bit vague if you're looking for help.


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

#7 ·  Posted (edited)

I think he doesn't want the day to be in the same column as the date. And he's probably getting runtime array bounds errors after messing with it.

Try this:

#include <File.au3>
#include <Array.au3>
$AccessLog = "C:\Users\.........\log.txt"

$aLines = 0

_FileReadToArray ( $AccessLog, $aLines)

; Create a new array
Global $AccessArray[$aLines[0]][5]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    $AccessArray[$i -1][0] = $aTmp[1]
    $AccessArray[$i -1][1] = $aTmp[2]
    $AccessArray[$i -1][2] = $aTmp[3]
    $AccessArray[$i -1][3] = $aTmp[4]
    $AccessArray[$i -1][4] = $aTmp[5]
Next

_ArrayDisplay($AccessArray)
Edited by Inverted

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I meant i want the data to look like this: 

            C1       C2        C3                  C4                   C5

R1 1)  Mon 1/27/14 11:46:13 ComputerName1 LocationX

R2 2)  Mon 1/27/14 11:47.16 ComputerName2 LocationX2

R#....

 

C# = Column #

R# = Row #

So only 5 columns and X # of rows. 

Edited by rickybobby

Share this post


Link to post
Share on other sites

rickybobby,

Then this should work: :)

#include <Array.au3>

; Simulate reading the file into an array
Global $aLines[3] = [2, _
            "Mon 01/27/2014 11:44:04.16 ComputerName Location", _
            "Mon 01/27/2014 11:44:04.16 ComputerName2 Location2"]

; Create a new array
Global $aArray[$aLines[0]][5]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    For $j = 1 To 5
        $aArray[$i -1][$j - 1] = $aTmp[$j]
    Next
Next

_ArrayDisplay($aArray, "", Default, 8)
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

#10 ·  Posted (edited)

rickybobby,

Then this should work: :)

#include <Array.au3>

; Simulate reading the file into an array
Global $aLines[3] = [2, _
            "Mon 01/27/2014 11:44:04.16 ComputerName Location", _
            "Mon 01/27/2014 11:44:04.16 ComputerName2 Location2"]

; Create a new array
Global $aArray[$aLines[0]][5]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    For $j = 1 To 5
        $aArray[$i -1][$j - 1] = $aTmp[$j]
    Next
Next

_ArrayDisplay($aArray, "", Default, 8)
M23

 

Nope still more X # of columns and 5 rows; also it cuts off the location (one of the rows is blank).

See above post for how I'm trying to format it. 

This is how it comes out:

PyERpls.png

 

Also my code: 

#include <File.au3>
#include <Array.au3>
$AccessLog = "C:\Users\accesslog.txt"

;$AccessArray = 0 
$aLines = 0

_FileReadToArray ( $AccessLog, $aLines) 

; Simulate reading the file into an array
            
; Create a new array
Global $aArray[$aLines[0]][5]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit($aLines[$i], " ")
    For $j = 1 To 5
        $aArray[$i -1][$j - 1] = $aTmp[$j]
    Next
Next

_ArrayDisplay($aArray, "", Default, 8)
Edited by rickybobby

Share this post


Link to post
Share on other sites

rickybobby,

Then your file is not in the format you described in the OP and which I have simulated in that script. :(

Could you please post it (or an extract) so I can see what exactly is the format and how we should split 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

#12 ·  Posted (edited)

rickybobby,

Then your file is not in the format you described in the OP and which I have simulated in that script. :(

Could you please post it (or an extract) so I can see what exactly is the format and how we should split it. :)

M23

 
Mon 01/27/2014  9:44:12.44 OC010104JXQ5YQ1 group1 
Mon 01/27/2014  9:44:13.53 EL04010500020 group2 
Mon 01/27/2014  9:44:17.86 OC010105JXQ70R1 group1 
Mon 01/27/2014  9:44:20.57 EL04010500021 group2 
 
thats an excerpt from the file, I want it to look exactly the same, but each day, date, time, compname, and group seperated. 
 
like so:
7I1irpE.png
Edited by rickybobby

Share this post


Link to post
Share on other sites

rickybobby,

Aha, you have a double space between the date and time sections! That is what caused the blank section. :)

Here is the file I used based on your post above: Test.txt

And here is the script I used on the file which gives me what you say you want:

#include <Array.au3>
#include <File.au3>
#include <StringConstants.au3>

$aLines = _FileReadToArray("Test.txt")

_ArrayDisplay($aLines, "", Default, 8)

; Create a new array
Global $aArray[$aLines[0]][5]

; Now loop through the array and fill the new array
For $i = 1 To $aLines[0]
    $aTmp = StringSplit(StringStripWS($aLines[$i], $STR_STRIPSPACES), " ")
    For $j = 1 To 5
        $aArray[$i -1][$j - 1] = $aTmp[$j]
    Next
Next

_ArrayDisplay($aArray, "", Default, 8)
Are we there now? :)

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

#14 ·  Posted (edited)

Glad to see I'm not the only one confused with the 2 versions of FileReadToArray :)

What about some regex ?

#include <Array.au3>

$aLines = FileReadToArray("Test.txt")
;_ArrayDisplay($aLines, "", Default, 8)

; Create a new array
Global $aArray[UBound($aLines)][5]

; Now loop through the array and fill the new array
For $i = 0 To UBound($aLines)-1
    $aTmp = StringRegExp($aLines[$i], '\S+', 3)
    For $j = 0 To UBound($aTmp)-1
        $aArray[$i][$j] = $aTmp[$j]
    Next
Next

_ArrayDisplay($aArray)
Edited by mikell

Share this post


Link to post
Share on other sites

Here are another couple of ways of converting a file into a 1-dimensional array, then, into a 2-dimensional array.

The number of columns must be known.

#include <Array.au3> ; For displaying arrays only.

Local $iNumCol = 5
Local $aLines = StringRegExp(FileRead("Test-2.txt"), '\S+', 3)
;_ArrayDisplay($aLines)

; Create a new array
Global $aArray[Int(UBound($aLines) / $iNumCol)][$iNumCol]

;#cs
For $i = 0 To UBound($aLines, 1) - 1
    $aArray[Floor($i / $iNumCol)][Mod($i, $iNumCol)] = $aLines[$i]
Next
;#ce

 ;Or

#cs
For $i = 0 To UBound($aArray, 1) - 1
    For $j = 0 To UBound($aArray, 2) - 1
        $aArray[$i][$j] = $aLines[($i * $iNumCol) + $j]
    Next
Next
#ce

_ArrayDisplay($aArray)

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