Jump to content
Sign in to follow this  
mentosan

working with arrays

Recommended Posts

mentosan

Hello people !

I'm having this file.txt :

CODE
111 22

3333 44444

and I'm using this code to create separate strings :

dim $setfile = "file.txt"
dim $array

_FileReadToArray ($setfile, $array)
$array = StringSplit ($setfile, " ,@CRLF") 

MsgBox (0,"Display",$array[1],4) ;for testing purposes
MsgBox (0,"Display",$array[2],4) ;for testing purposes
MsgBox (0,"Display",$array[3],4) ;for testing purposes
MsgBox (0,"Display",$array[4],4) ;for testing purposes

I want result to be :

111

22

3333

44444

Want am I doing wrong ?

Thank you for your reply !

Share this post


Link to post
Share on other sites
PsaltyDS

Hello people !

I'm having this file.txt :

111 22
3333 44444

and I'm using this code to create separate strings :

dim $setfile = "file.txt"
dim $array

_FileReadToArray ($setfile, $array)
$array = StringSplit ($setfile, " ,@CRLF") 

MsgBox (0,"Display",$array[1],4);for testing purposes
MsgBox (0,"Display",$array[2],4);for testing purposes
MsgBox (0,"Display",$array[3],4);for testing purposes
MsgBox (0,"Display",$array[4],4);for testing purposes

I want result to be :

111

22

3333

44444

Want am I doing wrong ?

Thank you for your reply !

The variable $setfile contains the file name, not the data from the file, so all you are splitting is "file.txt".

There is no point is using _FileReadToArray() is you will have multiple entries on each line, either.

Read the whole file into a single string with FileRead(), and pass that to StringSplit().

In addition, there is no comma separator for delimiters, so to split on space and newline use " @LF" (space and @LF).

Use StringStripWS() on each entry in the array to remove any stray @CR characters.

We'll talk RegExp another day...

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
mentosan

$setfile = FileRead ("file.txt")
dim $array

$array = StringSplit ($setfile, " @LF")

$array[2] = StringStripWS ($array & Chr(10) & Chr(13), 8)


MsgBox (0,"Display",$array[2],4)

The problem is on array index no.2 the result will be "22<LF>3333". I want only "22".

You mean if I would use RegEx then the line will be like :

$array[2] = StringRegExp($setfile,"\s\d+",3)

$array[3] = StringRegExp($setfile,"\r\d+",3)

Thx

Edited by mentosan

Share this post


Link to post
Share on other sites
PsaltyDS

$setfile = FileRead ("file.txt")
dim $array

$array = StringSplit ($setfile, " @LF")

$array[2] = StringStripWS ($array & Chr(10) & Chr(13), 8)


MsgBox (0,"Display",$array[2],4)

The problem is on array index no.2 the result will be "22<LF>3333". I want only "22".

You mean if I would use RegEx then the line will be like :

$array[2] = StringRegExp($setfile,"\s\d+",3)

$array[3] = StringRegExp($setfile,"\r\d+",3)

Thx

RegExp is better, if you're already familiar with that syntax, run with it. But I was talking about something like this:
#include <Array.au3>; For _ArrayDisplay() only

; $sData = FileRead("C:\YourDir\YourFile.txt")
$sData = "111 22" & @CRLF & "3333 44444"
$avSplit = StringSplit($sData, " " & @LF, 0)
_ArrayDisplay($avSplit, "Before")
For $n = 1 To $avSplit[0]
    $avSplit[$n] = StringStripWS($avSplit[$n], 2)
Next
_ArrayDisplay($avSplit, "After")

StringStripWS() takes off the stray @CR left over by splitting on @LF. If you use " " & @CRLF to begin with, you will wind up with empty elements that have to be removed.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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.