I'm having issues reading a CSV file to then add containing numbers together.
$billsRawData1 = FileReadLine(@AppDataDir & "\testDir\test.csv", 2)
$billsArray1 = StringSplit($billsRawData1, ",")
$billsRawData2 = FileReadLine(@AppDataDir & "\testDir\test.csv", 3)
$billsArray2 = StringSplit($billsRawData2, ",")
$billsRawData3 = FileReadLine(@AppDataDir & "\testDir\test.csv", 4)
$billsArray3 = StringSplit($billsRawData3, ",")
$billsRawData4 = FileReadLine(@AppDataDir & "\testDir\test.csv", 5)
$billsArray4 = StringSplit($billsRawData4, ",")
$billsRawData5 = FileReadLine(@AppDataDir & "\testDir\test.csv", 6)
$billsArray5 = StringSplit($billsRawData5, ",")
$billsRawData6 = FileReadLine(@AppDataDir & "\testDir\test.csv", 7)
$billsArray6 = StringSplit($billsRawData6, ",")
$billsRawData7 = FileReadLine(@AppDataDir & "\testDir\test.csv", 8)
$billsArray7 = StringSplit($billsRawData7, ",")
$billsRawData8 = FileReadLine(@AppDataDir & "\testDir\test.csv", 9)
$billsArray8 = StringSplit($billsRawData8, ",")
$billsRawData9 = FileReadLine(@AppDataDir & "\testDir\test.csv", 10)
$billsArray9 = StringSplit($billsRawData9, ",")
$billsRawData10 = FileReadLine(@AppDataDir & "\testDir\test.csv", 11)
$billsArray10 = StringSplit($billsRawData10, ",")
$billsRawData11 = FileReadLine(@AppDataDir & "\testDir\test.csv", 12)
$billsArray11 = StringSplit($billsRawData11, ",")
$billsRawData12 = FileReadLine(@AppDataDir & "\testDir\test.csv", 13)
$billsArray12 = StringSplit($billsRawData12, ",")
$billsRawData13 = FileReadLine(@AppDataDir & "\testDir\test.csv", 14)
$billsArray13 = StringSplit($billsRawData13, ",")
$billsRawData14 = FileReadLine(@AppDataDir & "\testDir\test.csv", 15)
$billsArray14 = StringSplit($billsRawData14, ",")
$billsRawData15 = FileReadLine(@AppDataDir & "\testDir\test.csv", 16)
$billsArray15 = StringSplit($billsRawData15, ",")
$billsRawData16 = FileReadLine(@AppDataDir & "\testDir\test.csv", 17)
$billsArray16 = StringSplit($billsRawData16, ",")
$billsRawData17 = FileReadLine(@AppDataDir & "\testDir\test.csv", 18)
$billsArray17 = StringSplit($billsRawData17, ",")
$billsRawData18 = FileReadLine(@AppDataDir & "\testDir\test.csv", 19)
$billsArray18 = StringSplit($billsRawData18, ",")
$billsRawData19 = FileReadLine(@AppDataDir & "\testDir\test.csv", 20)
$billsArray19 = StringSplit($billsRawData19, ",")
$billsRawData20 = FileReadLine(@AppDataDir & "\testDir\test.csv", 21)
$billsArray20 = StringSplit($billsRawData20, ",")
$total = $billsArray1[3] + $billsArray2[3]; + $billsArray3[3] + $billsArray4[3] + $billsArray5[3] + $billsArray6[3] + $billsArray7[3] + $billsArray8[3] + $billsArray9[3] + $billsArray10[3] + $billsArray11[3] + $billsArray12[3] + $billsArray13[3] + $billsArray14[3] + $billsArray15[3]; + $billsArray16[3] + $billsArray17[3] + $billsArray18[3] + $billsArray19[3] + $billsArray20[3]
MsgBox(0, "", $total)
,,,
10/04/2015, Internet,$40 , Monthly
10/07/2015, Gas,$80 , Monthly
10/01/2015, Cable,$60 , Monthly
10/27/2015, Storage,$50 , Monthly
10/30/2015,School,$150 , Monthly
10/18/2015,Rent,$750 ,Monthly
test.csv contains variable amount of rows. I'm trying to add the value in the third column. I read each line into a variable and split that into an array ($billsArray1[3]
returns $40
). I then add these variables together, but it returns 0
for $total
.
1) Why is $total
returning 0
instead of the total?
2) How to create a loop to assign the variables (so I don't need to create new variables for each line, as I don't know how many lines there will be)? Note* it always skips the first line so it starts at line 2.
3) How to create another loop to add the data in the third column together?
I wouldn't create a separate array. I'd just loop over the file, grab the 3rd entry, strip everything that isn't a number (like $
) and then add that to the total...
$file = FileOpen(@AppDataDir & "\testDir\test.csv", 0)
$total = 0
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
;Get the 3rd "token" and strip everything that is not a number.
$bill = StringRegExpReplace(StringSplit($line, ',')[3], "[^\d]", "")
;Add current bill to total.
$total = $total + $bill
WEnd
FileClose($file)
MsgBox(0, "Total", $total)