w4wxa
New Member
Posts: 13
|
Post by w4wxa on Dec 26, 2018 22:05:43 GMT
My cell phone provider allows access to a historycsv file with usage data in it.
I access the variable I want using word$.
The variable comes back like x$ = "51"
When I try to use x = val(x$) it comes back 0.
How do you convert a string variable in a csv file to a numeric variable?
Thanks, Tom
|
|
|
Post by B+ on Dec 27, 2018 1:40:09 GMT
Could you show the JB code you are using to access the CSV file? If x$ = "51" then the val(x$) should = 51.
|
|
|
Post by Rod on Dec 27, 2018 8:21:04 GMT
Just guessing but you probably need to strip the quotes from the csv data “51” is not the same as 51 and is why just BASIC is returning 0. Any string that starts with a non numeric character returns 0 in the val() function.
|
|
w4wxa
New Member
Posts: 13
|
Post by w4wxa on Dec 27, 2018 14:27:59 GMT
Here is my test program
open "history.csv" for input as #in line input #in, a$ time$ = word$(a$,3,",") time = val(time$) print time close #in
end
Here is a file called history.csv
"10/28/2018 1:07:34 PM","Out","51","850-877-0716","0"
|
|
w4wxa
New Member
Posts: 13
|
Post by w4wxa on Dec 27, 2018 14:36:29 GMT
Rod is right. If history.csv looks like this:
10/28/2018 1:07:34 PM,Out,51,850-877-0716,0
numeric variable shows correctly
|
|
|
Post by B+ on Dec 27, 2018 15:35:57 GMT
Yeah, it is easier to see what is going on with some code.
I have to say, I wasn't expecting this problem with Double Quotes either, so thanks for heads up on csv file usage. Here is a function to extract string enclosed by Double Quotes and a way to extract the time from this csv file line:
open "history.csv" for input as #in line input #in, a$ close #in
print "Whole line from csv file:" print a$ print "OK" print tm$ = word$(a$,3,",") print "Third comma delimited word$ is ";tm$ print "OK" print t = val(tm$) print "The VAL of that 3rd word is ";t print "Dang, fooled me (B+) too!" print print "OK fix this with a function:" t2 = val(removeDblQuotes$(tm$)) print "Applied RemoveDblQuotes$ function then VAL on 3rd word is ";t2 print "OK, fixed!" print 'get the time from this file line sp = instr(a$, " ") comma = instr(a$, ",") print "The time found after 1st space and before first comma is *";mid$(a$, sp + 1, comma - sp - 2);"*"
' OK this function might be handy if all string values in csv file are enclosed with Double Quotes function removeDblQuotes$(s$) if left$(s$, 1) = chr$(34) then s$ = mid$(s$, 2) if right$(s$, 1) = chr$(34) then s$ = mid$(s$, 1, len(s$) - 1) removeDblQuotes$ = s$ end function
|
|
carlg
New Member
Posts: 14
|
Post by carlg on Dec 27, 2018 16:30:35 GMT
Rod is right. If history.csv looks like this: 10/28/2018 1:07:34 PM,Out,51,850-877-0716,0 numeric variable shows correctly If you know you have a quoted number, you can simply skip the first quote, and then val should work okay, like so: q$ = chr$(34) fifteen$ = q$+"15"+q$ print fifteen$ print val(fifteen$) 'does not work print val(mid$(fifteen$,2)) 'does work
|
|
|
Post by Rod on Dec 27, 2018 19:47:47 GMT
Try using “”,””as the delimiter. Most CSV files wrap everything in quotes. Originally it was only supposed to be fields that needed it but it became simpler to wrap everything.
|
|
w4wxa
New Member
Posts: 13
|
Post by w4wxa on Dec 27, 2018 22:00:51 GMT
That didn't work the delimiter has to be chr$(34);",";chr$(34) Works fine then
|
|