Page 2 of 2 <12
Topic Options
#182404 - 2007-11-09 05:19 PM Re: add fields from csv [Re: Darren_W]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Yes, I know that they start at 0. This script isn't looking at those lines, but rather those positions on each line.
Thanks.

Top
#182411 - 2007-11-10 12:09 AM Re: add fields from csv [Re: booey]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Do you want to make the sum of all the values on every line at position 66 and another of all the values on every line at position 71?
Top
#182412 - 2007-11-10 12:25 AM Re: add fields from csv [Re: Witto]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
You are reading the very first line, but you don't do anything with it.
I think you should also indent your code, so you see your mistakes like no EndIf.
I think it is good habit closing opened files:
Close(3)
I reformatted your code a bit and put two remarks in it.
Break ON
DIM $csv,$bal,$csv_array,$read,$controlnum,$line,$x
RedirectOutput("C:\temp\output.txt",1)

$csv="C:\temp\test.csv"
If Open(3,$csv) = 0
   
$read=ReadLine(3)
   
While @ERROR = 0
       
$read=ReadLine(3) ;This should be placed just before the loop
        $csv_array=Split($read,"|")
       
If UBound($csv_array)

           
;Add together first balances
            ? "Account:  " + ($csv_array[5]) + "   Balance:   " + ($csv_array[71])
           
$cintndc = Round($csv_array[66],2)
           
$balsndc = $balsndc + $cintndc
            ;$balsndc ?

            ;add together second balances
            $cintproc = Round($csv_array[71],2)
           
$balsproc = $balsproc + $cintproc
            ;$balsproc ?

        Else ;This Else is redundant
        EndIf
    Loop

    @CRLF

    ;add together first and second balances
    $total = $balsndc + $balsproc
    "Total balance is: " + $total ?


   
$read = Close(3)

   
RedirectOutput("")

Top
#182479 - 2007-11-12 09:11 AM Re: add fields from csv [Re: Witto]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
There are some unused declared variables and some undeclared used variables. I think if you want to declare variables, you should set the option explicit at top of your script.
What about:
;Script Options
If Not @LOGONMODE
    Break On
Else
    Break Off
EndIf
Dim $RC
$RC = SetOption("Explicit", "On")
$RC = SetOption("NoMacrosInStrings", "On")
$RC = SetOption("NoVarsInStrings", "On")
If @SCRIPTEXE = "KIX32.EXE"
    $RC = SetOption("WrapAtEOL", "On")
EndIf

;Declare variables
DIM
 
$csv,$csv_array,$read,$cintndc,$balsndc,$cintproc,$balsproc,$Total
DIM $Handle1,$ColumnAccount,$ColumnBalance1,$ColumnBalance2

;Initialize variables
$csv="C:\temp\test.csv"
$ColumnAccount=5
$ColumnBalance1=71
$ColumnBalance2=66

;Code
RedirectOutput("C:\temp\output.txt",1)

$Handle1 = FreeFileHandle()
If Open($Handle1,$csv) = 0
   
$read=ReadLine($Handle1)
   
While @ERROR = 0
       
        $csv_array=Split($read,"|")
       
If UBound($csv_array)

           
;Add together first balances
   
        ? "Account:  " + ($csv_array[$ColumnAccount]) + "   Balance:   " + ($csv_array[$ColumnBalance1])
           
$cintndc = Round($csv_array[$ColumnBalance2],2)
           
$balsndc = $balsndc + $cintndc
            ;$balsndc ?

            ;add together second balances
            $cintproc = Round($csv_array[$ColumnBalance1],2)
           
$balsproc = $balsproc + $cintproc
            $balsproc ?

        EndIf
        $read=ReadLine($Handle1)
   
Loop
    $RC = Close($Handle1)
   

    @CRLF
   
    ;add together first and second balances
    $total
 = $balsndc + $balsproc
    "Total balance is: " + $total?
EndIf

RedirectOutput("")

;Personal UDF Section

;UDF Section

Top
#182507 - 2007-11-12 03:58 PM Re: add fields from csv [Re: Witto]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Witto,

Thanks to your help, it's working now. Thank you very much.

So was the problem with the first line not being added caused by the wrong placement of the second " $read=ReadLine(3)" in my original script?

Thanks.

Top
#182515 - 2007-11-12 05:23 PM Re: add fields from csv [Re: booey]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
What you do is:
;Open file
;If no error occurs, the return value and the errorlevel are both 0
;Here you check the return value

If Open($Handle1,$csv) = 0
   
;Read the first line
    ;If there is no line to read, the errorlevel is different from 0
    $read=ReadLine($Handle1)
   
While @ERROR = 0
       
;Do stuff
       
       
;Read the next line and go to while
        ;If errorlevel is not 0, There was no line to read anymore
        ;In that case, continue after Loop
        $read=ReadLine($Handle1)
   
Loop
    ;Close file
    $RC = Close($Handle1)
EndIf

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1003 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.167 seconds in which 0.138 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org