Sign in to follow this  
Followers 0
computergroove

Question about an array

8 posts in this topic

I have a csv file that I changed to an ini file. Here are a few sample lines:

1,John,Smith,John.Smith@gmail.com,password

2,Jane,Fonda,Jane.Fonda@gmail.com,password

etc

I want to seperate line 1 into:

$Number = 1

$FirstName = John

$LastName = Smith

$Email = John.Smith@gmail.com

$Password = password

Then I want to use the above data and then replace all the above data with the data in the next line in the ini file. What I've pieced together so far (corrrect me if im wrong) is the following:

While 1
Local $file = FileOpen ("names.ini", 0) ;Open the names.ini file into memory
Local $LineNo = 1 ;Line Being read into memory
Local $Array[5] ;sets array memory to 6 places

    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open names.ini")
        Exit
    EndIf ;Checks if file opened

$Array = FileReadLine($file, $LineNo) ;Sets $Array to be the line in Names.ini

I am not sure how to take the data before the first "," and set it to $Number and the second part of the string to $FirstName etc. Help


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



Look at StringRegExp, StringRegExpReplace, StringMid, StringLeft, StringSplit, StringRight and some other funtions.

In the 1st one you will propably find your answer but if ur lines have some standars look at the other functions also.

I will maybe come tomorrow with a better answer if i have the time


I feel nothing.It feels great.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I don't understand why you are using an ini file, but you should use StringSplit with a comma delimiter if you want to split each line into an array of values. Also the following line

Local $Array[5] ;allocates five elements

Not 6 places.

Edited by czardas

Share this post


Link to post
Share on other sites

Simplest, unless the data file is huge, is probably use _FileReadToArray() to read the data file in one shot. Then use StringSplit() separating by the comma, to create an array of data items for each line. Or process each line in a loop so you can reuse the same array after reformulating the contents and writing it out or whatever.

Share this post


Link to post
Share on other sites

I don't understand why you are using an ini file, but you should use StringSplit with a comma delimiter if you want to split each line into an array of values. Also the following line

Local $Array[5] ;allocates five elements

Not 6 places.

Then how can I set the data to a variable name? ie $number = $array[1] ?

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You should also consider using a 2D array as I have done There are several csv parsers in example scripts, I rercommend you do a search.

$array[1] is a variable name.

Edited by czardas

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Ultimately you should probably use a two dimensional array. Here is a bit of code wich uses some basic methods you should also familiarize yourself with. Look at the help file examples for each function. The code is just an illustration. I haven't tested it, but it should be fine. Any issues running or understanding it, please tell us about it.

#include <Array.au3>
#include <File.au3>

Global $aRecords ; Inside a function you would use Local instead of Global.
_FileReadToArray("names.ini", $aRecords)

_ArrayDisplay($aRecords, "All records") ; Display not required.

Global $aTemp ; Declare a variable to hold each set of records.

; Loop through each entry and overwrite $aTemp.
For $i = 1 To $aRecords[0]
    ; Split each line
    $aTemp = StringSplit($aRecords, ",")
    _ArrayDisplay($aTemp, "After splitting each record") ; Display not required.
Next

; Let's loop through the final $aTemp array
Global $var
For $i = 1 To $aTemp[0]
    ; Accessing an array's elements
    $var = $aTemp[$i] ; assigning a second variable equal to the first.
    MsgBox(0, "", $var)
Next
Edited by czardas

Share this post


Link to post
Share on other sites

This is what I have so far and It works and is pretty much what I was looking for:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=Empire Island Auto Marble Sell City 1 and City 2.exe
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Maximize hMailServer - goto accounts and general

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{HOME}", "Terminate")
Global $file = FileOpen ("names.ini", 0) ;Open the names.ini file into memory
Global $LineNo = $LNo ;$LineNo is the line number being read in the names.ini
Global $String[5] ;data for the array coming from the names.ini file

While 1
$String = FilereadLine($file, $LineNo)
Local $Split = StringSplit($String, ",")
Local $Number = $Split[1] ;Number
Local $FirstName = $Split[2] ;Fisrt
Local $LastName = $Split[3] ;Last
Local $FirstLast = $Split[4] ;First.Last
Local $Email = $Split[5] ;Email
Local $Password = $Split[6] ;Password

MsgBox(0," ", $Number & $FirstName & $LastName & $FirstLast & $Email & $Password)

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

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  
Followers 0