Jump to content

stringsplit


Recommended Posts

#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,

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

- 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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

  • Moderators

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.

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