Jump to content

What's wrong with my StringSplit?


 Share

Recommended Posts

G'day everyone

I have a file named enname.txt in the same directory as the script, and that file contains five lines of text containing one or two words per line. The line breaks are @CRLFs. I'm trying to populate an array with those lines, but I keep getting the "no delimiters found" error.

$enname1 = FileOpen (@ScriptDir & "\enname.txt", 0)
$enname2 = StringSplit ($enname1, @CRLF, 1)
MsgBox (0, "", $enname2[0], 10)
Exit

And the message box says "1", which means "no delimiters found". I've tried @CRLF in quotes, and I've tried setting the delimiter size to 1 instead of 0, but no luck. I've also tried other delimiters (and I tried putting everything in enname.txt on a single line too).

Any ideas?

Thanks

Samuel

Edited by leuce
Link to comment
Share on other sites

I'm trying to populate an array with those lines, but I keep getting the "no delimiters found" error.

Then, use _FileReadToArray()

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Then, use _FileReadToArray()

Thanks... so... what does "ByRef" mean (in the helpfile in the _FileReadToArray() entry)? I can't get the array to populate with:

#include <file.au3>
Dim $enname2
_FileReadToArray(@ScriptDir & "\enname.txt", $enname2)
MsgBox (0, "", $enname2[0], 10)
Exit

Thanks

Samuel

Link to comment
Share on other sites

Try this (change "yourfile.txt")

Dim $file = "yourfile.txt"

$fileread = FileRead($file)
$filesplit = StringSplit($fileread, @CRLF)

MsgBox(0, "", "your array has " & $filesplit[0] & " entries")

For $i = 1 To $filesplit[0] Step 2 ;skip the @crlf entries
    MsgBox(0, "", "$filesplit[" & $i & "] is " & '"' & $filesplit[$i] & '"')
Next
Link to comment
Share on other sites

Try this (change "yourfile.txt")

Thanks... there were some bugs in your script, but I could figure them out easily, and it helps.

Dim $file = @ScriptDir & "\enname.txt"

$fileread = FileRead($file, FileGetSize($file))
$filesplit = StringSplit($fileread, @CRLF, 1)

MsgBox(0, "", "your array has " & $filesplit[0] & " entries")

For $i = 1 To $filesplit[0] 
    MsgBox(0, "", "$filesplit[" & $i & "] is " & '"' & $filesplit[$i] & '"')
Next

However, since I'm only going to use this script once ever, and since I know how many lines the file has, I might also use FileReadLine with each loop (later the script) instead of populating an array.

Thanks everyone!

Link to comment
Share on other sites

There weren't any bugs on my script for my test... maybe for your use. :whistle:

FileGetSize($file) doesn't make sense. Default is read the whole file, and FileGetSize returns the file size.

@ScriptDir & "\enname.txt" isn't necessary, since the script will check the same folder by default.

The loop was just to show you where your lines were in the array.

Edited by xcal
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...