Jump to content

columns of text to an array


Recommended Posts

i am trying to convert a file's text to an array. i dont think they are tabs in between each column. as you can see there might be spaces in the first column. is there a way to do this nicely?

http://www.postimage.org/image.php?v=Pqrc8Yr

thanks in advance

Edited by gcue
Link to comment
Share on other sites

For each line $line of text you read off disk, a simple regexp will do it:

$line = "ACLIENT    1212 8 46 1053 11324 0:04:55.843 0:02:02.140 71:10:06:991"
$columns = StringRegExp($line, "([^\s]+)(?:\s+|$)", 3)
_ArrayDisplay($columns)

and find the data in the $columns array.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

thank you jchd - it alllmost works.. except when the first column's text has a space such as in the example provided

and here's the code i have

#include <file.au3>
#include <array.au3>

$file = "ap.txt"
$line_count = _FileCountLines($file)

for $x = 8 to $line_count
    $line = FileReadLine($file, $x)

    $columns = StringRegExp($line, "([^\s]+)(?:\s+|$)", 3)

    _ArrayDisplay($columns)
next
Edited by gcue
Link to comment
Share on other sites

ok so i figured the text in all the columns only resides in the first column.. all other columns are numbers

so im trying this:

#include <file.au3>
#include <array.au3>

$file = "ap.txt"

$line_count = _FileCountLines($file)

Dim $array[1][3]

For $x = 4 To $line_count
    $line = FileReadLine($file, $x)

    $columns = StringRegExp($line, "([^\s]+)(?:\s+|$)", 3)

;~ _ArrayDisplay($columns)

    ReDim $array[UBound($array) + 1][3]

    $2nd_column = $columns[1]

    If Not IsNumber($2nd_column) Then
        MsgBox(0, "", $2nd_column)
        $process_name = $columns[0] & " " & $columns[1]
    Else
        $process_name = $columns[0]
    EndIf

    $array[UBound($array) - 1][0] = $process_name
    $array[UBound($array) - 1][1] = $columns[2]
    $array[UBound($array) - 1][2] = $columns[5]
Next

_ArrayDisplay($array)

problem now is IsNumber isnt working the way i thought it would... i also tried IsInt

Edited by gcue
Link to comment
Share on other sites

Hi gcue,

here's another, rather long-winded, approach which allows for any number of spaces in the first column.

#include <array.au3>

$sLine = "System Kill   4 8 75 1792 260 0:00:00.000 0:05:45.718 0:00:00.000"
$sLine = StringStripWS($sLine,5);remove leading, trailing and double spaces
$p1 = StringInStr($sLine,' ',0,-8,StringLen($sline));find the last space before the start of the second column
$a1 = stringsplit(StringTrimLeft($sline,$p1),' ');get the other cols
$a1[0] = StringLeft($sLine, $p1 - 1);insert the first col
_ArrayDisplay($a1)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

As this is a column delimited file I would read it into an array by this method

#include <file.au3>
#include <array.au3>

Local $file = "ap.txt"
Local $aTemp = 0
_FileReadToArray($file,$aTemp)

Local $array[UBound($aTemp)-1][9]

For $iRow = 1 To UBound($aTemp) - 1
  $columns = StringRegExp($aTemp[$iRow], "^(.{13})(.{4})(.{4})(.{4})(.{5})(.{7})(.{13})(.{14})(.{15})$", 3)
    If @error Then ContinueLoop
  For $iCol = 0 To 8
    $array[$iRow-1][$iCol] = StringStripWS($columns[$iCol],3)  ;StringStripWS() is optional
    Next
Next

_ArrayDisplay($array)

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Sorry I was kidnapped by various things...

Yeah, if the contents can be about anything (at least in the headers and the first column, the best is to split a column boundaries and strip whitespaces. When there are no strict rules to rely on, regexps are not the tool to use.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here my solution:

#include <array.au3>
Local $line, $string, $aT1, $aT2, $s
$file = FileOpen("ap.txt")
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $s = StringStripWS($line, 7)
    $aT1 = StringRegExp($s, ".+([a-zA-Z]+\S)", 2)
    $aT2 = StringRegExp($s, "(?:\d).+", 1)
    $string &= $aT1[0] & "|" & StringReplace($aT2[0], " ", "|") & "|"
Wend
$array = StringSplit(StringMid($string, 1, StringLen($string) - 1), "|", 2)
_ArrayDisplay($array)

I hope it works!

BR,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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