Jump to content

loop with csv file - advice needed on EOF check


Davie
 Share

Recommended Posts

Hi Guys ,

I only started playing with AutoIT yesterday and Im rether enjoying myself.

 

I'm a little unsure how to handle this loop or change in a for next to stop at the end of file.

Just now the csv has one line but it loops endlessly on the single entry in the csv.

so...

#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <file.au3>
#include <array.au3>

;get the csv
$Line=1
$path="F:\logins.csv"

;readthe csv
    $String=FileReadLine($path,$Line)
;if theres a problem
    If $String="" Then MsgBox($MB_SYSTEMMODAL, "Fail", "Cant get anything from the file", 2)

;start the read using col 2,3,4 of the row.Output to messagebox to validate

While 1
$Line+=1
    $Snam=StringSplit($String, ",")
        MsgBox(0,"","Server : " & $Snam[1] & @CRLF & "User : " & $Snam[3] & @CRLF & "Pass : " & $Snam[4])

;now go do some other things with Snam1 Snam2 Snam4

WEnd

 

Im not clever enough yet to manipulate a for/next or some kind of better eof check...

 

Any help appreciated , and thanks in advance...

 

Davie.

Link to comment
Share on other sites

Feast your eyes on this: 

I would recommend reading it into a array first (the last option above). Reading individual lines is not very efficient.

 

Edit: For your reference, the error in your code is that you increment the $Line count, but you don't read in a new string within your loop.

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

  • Developers

You are reading just line 1 of the CSV and I don't see anywhere in the code that you read subsequent lines from the file.

There are UDF's available to read CSV files into an array which probably will make your life much easier...  just search for those.

Jos
 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Guys , could you maybe help here quickly:

 

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

Local $aInput
$file = "F:\logins.csv"

_FileReadToArray($file, $aInput, Default, ",")
 _ArrayDisplay($aInput)

For $i = 1 to UBound($aInput) -1

   MsgBox (0,'',$aInput[1][$i])
   MsgBox (0,'',$aInput[2][$i])
   MsgBox (0,'',$aInput[3][$i])

Next

Message boxes are giving Col 1 row 1  , col 1 , row 2 Col 1 row 3.

I would dance the dance of joy if it was returning

Row 1 col 1 , row 1 col 2 , row 1 col 3 

next

Row 2 col 1 , row 2 col 2 , row 2 col 3 

next

etc

 

AM I far off it?

 

Link to comment
Share on other sites

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

Local $aInput
$file = "F:\logins.csv"

_FileReadToArray($file, $aInput, Default, ",")
_ArrayDisplay($aInput)

For $i = 1 to UBound($aInput) -1
   MsgBox (0,'',$aInput[$i][0])
   MsgBox (0,'',$aInput[$i][1])
   MsgBox (0,'',$aInput[$i][2])
   MsgBox (0,'',$aInput[$i][3])
Next

Better?

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

Can you see me happy dancing SlackerAl!! Its sheer happy feet :D

then I released I didn't understand the basic structure and I kinda tripped.

Moving $I - can you spare a sec to explain whats being asked for in your example compared to mine?

Thanks so much for helping. Works like a dream.

 

Link to comment
Share on other sites

Imagine

Your file:
Apple,,,green
Banana,yellow,,
Minion,yellow,blue,,

    
Your array
    
        [Col 0] [Col 1] [Col 2] [Col 3]
[Row 1] Apple   null    null    green
[Row 2] Banana  yellow  null    null
[Row 3] Minion  yellow  blue    null

You can see col indexes and row indexes, any two indexes give you a position, e.g. 2,1 would be yellow, like plotting x, y on a map. (except row index first)

Row 0 stores some info about the array by default, so forget it for now.

So by putting $i first I lock the row we are looping to one $i value and I have hand coded in each column index (you would make this a second loop if you were keen).

Edited by SlackerAl
minor grammar

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

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