Jump to content

Recommended Posts

Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted (edited)

  On 6/18/2010 at 10:16 AM, 'Melba23 said:

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])

  On 6/18/2010 at 10:24 AM, 'Zibit said:

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
  • Moderators
Posted

amakrkr,

  Quote

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:

  Quote

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:

  Reveal hidden contents

 

Posted

  On 6/18/2010 at 11:37 AM, 'Melba23 said:

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:

  • Moderators
Posted

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:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...