Author |
Topic: finding values in strings (Read 554 times) |
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
finding values in strings
« Thread started on: Mar 7th, 2015, 03:14am » |
|
This post goes with the one before it, but I wanted to separate it for a reason.
This question is the "easy one" mired within the last post, and any kind soul who might want to help might not see this question that was tagged onto the last "Strings" post.
( As I am now stuck-in-the-mud in trying to make sense of downloading a file, I cannot progress in some of the less mind-numbing coding. I have been on the Wiki site, but it's more than I can process at present.)
But I have a task that has just stumped me. I know that it is not difficult to solve, but I just can't do it.
Can someone help me to find the values between the commas in a csv string? I have tried all sorts of combinations of fore next loops, left$, right$ instr, and so, but I'm just stumped.
For example, take this line from a file "10,3,4,5,7,8,12" How can I get these characters between the commas off of this string and transfer them to string variables?
a$(1) = "10" a$(2) = "3" etc?
Thanks so much for any help. Charles
|
« Last Edit: Mar 7th, 2015, 03:22am by CharlesB » |
Logged
|
|
|
|
Ian Kings
New Member
member is offline


Gender: 
Posts: 3
|
 |
Re: finding values in strings
« Reply #1 on: Mar 7th, 2015, 07:34am » |
|
Hi Charles,
Have you tried Richard's split function?
I'm sure it was available via the old Yahoo groups forum in the files section, or it might even be already installed as part of BB4W installation as a library. Code:
REM. Routine to emulate the SPLIT function available in some BASICs
REM. Written by Richard Russell, 13-Mar-2009
REM. Call as follows: num% = FNsplit(str$, delim$, array$())
REM. Where str$ is the string to be split
REM. delim$ is the delimiter at which it should be split
REM. array$() is an array to receive the parts
REM. The returned value is the number of array elements written
DEF FNsplit(A$, d$, a$())
LOCAL C%, I%, Q%, R%
REPEAT
R% = 0
REPEAT
C% = INSTR(A$, d$, R%+1)
Q% = INSTR(A$, """", R%+1)
IF Q% IF C% > Q% THEN
R% = INSTR(A$, """", Q%+1)
IF R%=0 ERROR 100, "Mismatched quotes"
ENDIF
UNTIL (Q% = 0) OR (C% <= Q%)
IF C% = 0 THEN C% = LEN(A$)+1
a$(I%) = LEFT$(A$, C%-1)
A$ = MID$(A$, C%+LEN(d$))
I% += 1
UNTIL A$=""
= I%
Ian
|
|
Logged
|
|
|
|
KenDown
Full Member
member is offline


Posts: 181
|
 |
Re: finding values in strings
« Reply #3 on: Mar 7th, 2015, 3:52pm » |
|
I'm sure the split function is very useful as a general-purpose function which will take any delimiter. However for Charles' question the solution is extremely simple:
a$="10,3,4,5,7,8,12" REPEAT i%=INSTR(a$,","):REM Look for the next comma IFi%>0THEN PRINTLEFT$(a$,i%-1):REM Print the first portion of the string a$=MID$(a$,i%+1):REM Discard the first portion of the string ENDIF UNTILi%=0 PRINTa$
Needless to say he could do anything he likes with the string. For example, instead of PRINTing it he could extract the value with a%(index%)=VALa$ (or, if he wanted to be pedantic, a%(index%)=VALLEFT$(a$,i%-1)
|
|
Logged
|
|
|
|
CharlesB
New Member
member is offline


Gender: 
Posts: 46
|
 |
Re: finding values in strings
« Reply #4 on: Mar 7th, 2015, 4:17pm » |
|
O Ken, thank you so much! This will get me by for now until I can better understand functions! I knew the answer had to be simple, but after 10 hours trying I just could not find it.!!
on Mar 7th, 2015, 3:52pm, KenDown wrote:I'm sure the split function is very useful as a general-purpose function which will take any delimiter. However for Charles' question the solution is extremely simple:
a$="10,3,4,5,7,8,12" REPEAT i%=INSTR(a$,","):REM Look for the next comma IFi%>0THEN PRINTLEFT$(a$,i%-1):REM Print the first portion of the string a$=MID$(a$,i%+1):REM Discard the first portion of the string ENDIF UNTILi%=0 PRINTa$
Needless to say he could do anything he likes with the string. For example, instead of PRINTing it he could extract the value with a%(index%)=VALa$ (or, if he wanted to be pedantic, a%(index%)=VALLEFT$(a$,i%-1) |
|
|
|
Logged
|
|
|
|
|