Jump to content

Splitting a string from TXT file


Recommended Posts

Hello all,

i have posted quite some questions regarding AutoIt and TXT files past week.

And my script evolved from a simple Write to TXT file to a script which (hopefully) will insert information from THX to oracle database.

Below my post you will find the code but first i want to ask for your help. I got few problems i need to solve so lets start.

The problem i have is related to my TXT file. I am stuck at how to read lines from my file and seperate that line to a segments.

Example:

files.dat@21.12.2009@21:33 -> $part1 = files.dat , $part2 = 21.12.2009, $part3 = 21:33

I wanted to use that info later on, on my INSERT statment to oracle database.

I have tried with StringSplit:

for $i = 1 to ubound($list) -1

    $read = FileReadLine($file)
    $split = StringSplit($read,'@')


    FileWriteLine($file_open,$split & @CRLF)

Next

...but all i get is emptyness :mellow:.

Link to comment
Share on other sites

  • Moderators

amakrkr,

Read the Help file for StringSplit - "Returns an array". :mellow:

So you need to read the individual elements of the array like this:

$sData = "files.dat@21.12.2009@21:33"

$aArray = StringSplit($sData, "@")

MsgBox(0, "Split", $sData & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $aArray[1] & @CRLF & $aArray[2] & @CRLF & $aArray[3])

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

amakrkr,

Read the Help file for StringSplit - "Returns an array". :mellow:

So you need to read the individual elements of the array like this:

$sData = "files.dat@21.12.2009@21:33"

$aArray = StringSplit($sData, "@")

MsgBox(0, "Split", $sData & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $aArray[1] & @CRLF & $aArray[2] & @CRLF & $aArray[3])

M23

Thank you for your fast replay.

Problem i get when i try your code is "Arry variable has incorrect number of subscripts or subscript dimension range exceeded".

$File = "c:\Filenames.dat"

$data = FileReadLine($File)

$aArray = StringSplit($data , "@")

MsgBox(0, "Split", $data  & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $aArray[1] & @CRLF & $aArray[2] & @CRLF & $aArray[3])

if you want to paste all of it then

$i = 0
$split = $split = StringSplit($read,'@')
do
filewrite("file.txt", $split[$i])
until $i = $split[0]

Hello and thx for replay.

When i tested this i got only "11111111" in my file. It looks like this is endless loop for some reason.

I dont know if i am missing something but i thought StringSplit will return a string as a resoult or am i wrong?

Edited by amakrkr
Link to comment
Share on other sites

  • Moderators

amakrkr,

I dont know if i am missing something but i thought StringSplit will return a string as a resoult or am i wrong?

Did you read what i wrote above? :mellow:

Read the Help file for StringSplit - "Returns an array".

As for the error: Each time you use StringSplit on a line in your file, you will get as many parts as there are section separated by the delimiter:

"files.dat@21.12.2009@21:33" will give you 3 parts

"Fred" will give you just the one as there is no delimiter present.

The number of parts is held in the [0] element of the array, so you can only read that many elements - if you go over that number you get an error.

So I imagine you are trying to split a line without a delimiter (@). Just check how many elements you have:

$data = "files.dat@21.12.2009@21:33"

$aArray = StringSplit($data , "@")

$sSplits = ""
For $i = 1 To $aArray[0]
    $sSplits &= $aArray[$i] & @CRLF
Next

MsgBox(0, "Split", $data  & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $sSplits)

$data = "Fred"

$aArray = StringSplit($data , "@")

$sSplits = ""
For $i = 1 To $aArray[0]
    $sSplits &= $aArray[$i] & @CRLF
Next

MsgBox(0, "Split", $data  & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $sSplits)

If you are unsure about arrays, there is a very good tutorial in the Wiki. :party:

Is it clear now? :P

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

amakrkr,

Did you read what i wrote above? :mellow:

As for the error: Each time you use StringSplit on a line in your file, you will get as many parts as there are section separated by the delimiter:

"files.dat@21.12.2009@21:33" will give you 3 parts

"Fred" will give you just the one as there is no delimiter present.

The number of parts is held in the [0] element of the array, so you can only read that many elements - if you go over that number you get an error.

So I imagine you are trying to split a line without a delimiter (@). Just check how many elements you have:

$data = "files.dat@21.12.2009@21:33"

$aArray = StringSplit($data , "@")

$sSplits = ""
For $i = 1 To $aArray[0]
    $sSplits &= $aArray[$i] & @CRLF
Next

MsgBox(0, "Split", $data  & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $sSplits)

$data = "Fred"

$aArray = StringSplit($data , "@")

$sSplits = ""
For $i = 1 To $aArray[0]
    $sSplits &= $aArray[$i] & @CRLF
Next

MsgBox(0, "Split", $data  & @CRLF & @CRLF &  "has been split into:" & @CRLF & @CRLF & $sSplits)

If you are unsure about arrays, there is a very good tutorial in the Wiki. :party:

Is it clear now? :P

M23

Tnank you!

Now its all clear.

Thanks again! :party:

Link to comment
Share on other sites

  • Moderators

amakrkr,

When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. :mellow:

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