Jump to content
Sign in to follow this  
diikee

stringsplit

Recommended Posts

diikee

#include <file.au3>
$file = FileOpen("c:\misc\e.csv", 0)
$sFile = @ScriptDir & "\Test1.txt"

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
Dim $aRecords
If Not _FileReadToArray("c:\misc\e.csv",$aRecords) Then
   MsgBox(4096,"Error", " Error reading  error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    $rec = StringSplit($aRecords, ",")
    _FileWriteFromArray($sFile, $rec, 1)
Next
FileClose($file)

I am reading a CSV file to an array but I want to split the entries in the file and print them one at a time.

instead of me,you,them,ours...

i want

me,

you,

them,

ours,

Share this post


Link to post
Share on other sites
Paulie

Stringsplit()

I don't understand your question? If you have a comma delimiter string and want to seperate it by the commas, the Stringsplit is the function for you

#Include <Array.au3>
$String = "me,you,them,ours"
$Result = StringSplit($string, ",")

_ArrayDisplay($Result)

Share this post


Link to post
Share on other sites
diikee

- save the string [csv] to a file instead of hard coding it.

- open the file

- read the file

- stringsplit [ ","]

- write the split values to a file or to console

as a test.

Edited by diikee

Share this post


Link to post
Share on other sites
herewasplato

_FileReadToArray only splits on CR, LF, or CRLF - not commas

try this code

#include <Array.au3>

$file = FileOpen("e.csv", 0)
$sFile = FileOpen("Test1.txt", 2)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$aRecords = StringSplit(FileRead("e.csv"), ",")

For $x = 1 To $aRecords[0]
    FileWriteLine($sFile, $aRecords[$x])
Next
FileClose($file)
FileClose($sFile)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
SmOke_N

_FileReadToArray only splits on CR, LF, or CRLF - not commas

try this code

#include <Array.au3>

$file = FileOpen("e.csv", 0)
$sFile = FileOpen("Test1.txt", 2)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$aRecords = StringSplit(FileRead("e.csv"), ",")

For $x = 1 To $aRecords[0]
    FileWriteLine($sFile, $aRecords[$x])
Next
FileClose($file)
FileClose($sFile)
FileWrite("Test.txt", StringReplace(FileRead("e.csv"), ",", "," & @CRLF))

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
herewasplato

FileWrite("Test.txt", StringReplace(FileRead("e.csv"), ",", "," & @CRLF))

minimalist :-) :-) :-) :-) :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
diikee

woowwwwwwwwwwwwwwww ---that was easy.

How comes other methods above couldn't work????

Share this post


Link to post
Share on other sites
SmOke_N

woowwwwwwwwwwwwwwww ---that was easy.

How comes other methods above couldn't work????

What are you referring to that worked and didn't work?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
diikee

FileWrite("Test.txt", StringReplace(FileRead("e.csv"), ",", "," & @CRLF))

vs

#include <Array.au3>

$file = FileOpen("e.csv", 0)
$sFile = FileOpen("Test1.txt", 2)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$aRecords = StringSplit(FileRead("e.csv"), ",")

For $x = 1 To $aRecords[0]
    FileWriteLine($sFile, $aRecords[$x])
Next
FileClose($file)
FileClose($sFile)

Share this post


Link to post
Share on other sites
SmOke_N

FileWrite("Test.txt", StringReplace(FileRead("e.csv"), ",", "," & @CRLF))

vs

#include <Array.au3>

$file = FileOpen("e.csv", 0)
$sFile = FileOpen("Test1.txt", 2)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$aRecords = StringSplit(FileRead("e.csv"), ",")

For $x = 1 To $aRecords[0]
    FileWriteLine($sFile, $aRecords[$x])
Next
FileClose($file)
FileClose($sFile)
Seems he didn't add the comma back in like I did.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
herewasplato

...i want

me,

you,

them,

ours,

There you go SmOke_N - givin' the OP precisely what is requested :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.