Item Delimiters?

Discuss all aspects of programming here. From 8-bit through to modern architectures.
User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Item Delimiters?

Postby richmond62 » Mon Jun 19, 2017 7:02 am

In another programming language one can have string variables containing ITEM DELIMITERS like this:

set item delimiter to ","
put "HAPPY,SAD,INDIFFERENT" into D$
put item 2 of D$ into X$

(X$ will contain "SAD")

this is, obviously, rather more useful than LEFT$, RIGHT$ and MID$ in BASIC.

Is there a way to work with delimiters in BASIC?

User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Re: Item Delimiters?

Postby richmond62 » Mon Jun 19, 2017 7:21 am

This:

Code: Select all

10 A$="HAPPY,SAD,INDIFFERENT"
20 PRINT INSTR(A$,",")

Will only pick up the first comma; this is a right pox
as you would have to set a different character as
an item delimiter between each item; which would
get very poxy indeed if one had a list of, say, 25 items.

User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Re: Item Delimiters?

Postby richmond62 » Mon Jun 19, 2017 7:44 am

Wow: don't you just love those people who answer their own questions!!!

Code: Select all

10 A$="HAPPY,SAD,INDIFFERENT"
20 ID1= INSTR(A$,",")
30 B$=LEFT$(A$,(ID1-1))
40 PRINT B$
50 A2$=MID$(A$,(ID1+1))
60 ID2= INSTR(A2$,",")
70 B2$=LEFT$(A2$,(ID2-1))
80 PRINT B2$
90 A3$=MID$(A2$,(ID2+1))
100 ID3= INSTR(A3$,",")
110 B3$=LEFT$(A3$,(ID3-1))
PRINT B3$

Screen Shot 2017-06-19 at 10.43.59 am.png
(6.48 KiB) Not downloaded yet

CLUNKY!

User avatar
danielj
Posts: 5350
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester

Re: Item Delimiters?

Postby danielj » Mon Jun 19, 2017 7:48 am

Ah, but then you wrap that up as a function and you're away. :)

User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Re: Item Delimiters?

Postby richmond62 » Mon Jun 19, 2017 7:56 am

wrap that up as a function


"Bonehead Barker" (my Maths teacher in 1976)
never got to functions (mainly because he ran away from the
horrible boarding school where I was just as soon as he found a better job
- sensible chap).

And as I have just started looking at BASIC again for the first time
in about 30 years, frankly, when it comes to functions, I haven't a clue. :D

User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Re: Item Delimiters?

Postby richmond62 » Mon Jun 19, 2017 8:06 am


User avatar
Rich Talbot-Watkins
Posts: 1121
Joined: Thu Jan 13, 2005 5:20 pm
Location: Palma, Mallorca

Re: Item Delimiters?

Postby Rich Talbot-Watkins » Mon Jun 19, 2017 8:18 am

INSTR can take a third parameter specifying where to start from.

Try this:

Code: Select all

  10 A$="Hello,goodbye,whatever"
  20 start%=1
  30 REPEAT
  40 end%=INSTR(A$,",",start%)
  50 PRINT MID$(A$,start%,end%-start%)
  60 start%=end%+1
  70 UNTIL end%=0


This relies on the apparently undocumented feature that a negative third parameter to MID$ returns the rest of the string. Makes the code nice and neat though!

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Item Delimiters?

Postby Elminster » Mon Jun 19, 2017 8:07 pm

And as I have just started looking at BASIC again for the first time
in about 30 years, frankly, when it comes to functions, I haven't a clue.


Taking Rich's code you could do someting likebelow. Unfortunately BASIC2/4 you cannot pass an array as a Parameter, unless you cheat with indirection operators. If you were using BASIC V, e.g. Adv 6502 Basic or RISC OS you could also tunr the array into a parameteras well.

Code: Select all

10 DIM GLOBAL_ARRAY$(30)
   20 A$="Hello.goodbye.whatever"
   30 PROCprint__array(FNstrip(A$,"."))
   40 A$="HAPPY,SAD,INDIFFERENT"
   50 PROCprint__array(FNstrip(A$,","))
   60 END
   70 :
   80 DEF FNstrip(string$,delimiter$)
   90    start%=1
  100    counter%=0
  110    REPEAT
  120      end%=INSTR(string$,delimiter$,start%)
  130      GLOBAL_ARRAY$(counter%)=MID$(string$,start%,end%-start%)
  140       start%=end%+1
  150       counter%=counter%+1
  160    UNTIL end%=0
  170 =counter%-1
  175 :
  180 DEF PROCprint__array(elements%)
  190   IF elements% THEN FOR iterate%=0 TO elements%
  200         PRINT GLOBAL_ARRAY$(iterate%)
  210   NEXT iterate%
  220 ENDPROC

User avatar
tricky
Posts: 1918
Joined: Tue Jun 21, 2011 8:25 am
Contact:

Re: Item Delimiters?

Postby tricky » Mon Jun 19, 2017 8:54 pm

I was sure JGH would have had something, but I couldn't find it http://mdfs.net/System/Library/BLib/

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Item Delimiters?

Postby Elminster » Mon Jun 19, 2017 9:28 pm

tricky wrote:I was sure JGH would have had something, but I couldn't find it http://mdfs.net/System/Library/BLib/


Always the first place to look. I have nicked local errors, font load, command line parameters from there recently. I dont remember seeing anythign about string delimiters. I just did above to work it through my head :)

User avatar
Elminster
Posts: 1632
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK

Re: Item Delimiters?

Postby Elminster » Tue Jun 20, 2017 12:05 am

Elminster wrote:
tricky wrote:I was sure JGH would have had something, but I couldn't find it http://mdfs.net/System/Library/BLib/


Always the first place to look. I have nicked local errors, font load, command line parameters from there recently. I dont remember seeing anythign about string delimiters. I just did above to work it through my head :)


Command line of course the is a space delimited list.

User avatar
jgharston
Posts: 2762
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield

Re: Item Delimiters?

Postby jgharston » Tue Jun 20, 2017 12:06 am

Rich Talbot-Watkins wrote:This relies on the apparently undocumented feature that a negative third parameter to MID$ returns the rest of the string. Makes the code nice and neat though!

or just used the documentation functionality of not using the third parameter: PRINT MID$(A$,start%)

tricky wrote:I was sure JGH would have had something, but I couldn't find it http://mdfs.net/System/Library/BLib/

Command Line Parsing does it with space-separated items:
http://mdfs.net/System/Library/BLib/Docs/CmdLine.txt
http://mdfs.net/System/Library/BLib/CmdLine

A$="hello there stardot world"
REPEAT
PRINT FNcl("",0)
UNTIL A$=""


The STRING library supplied with BBC BASIC for Windows has a FNsplit() function that will split up a string by a supplied delimiter, however it is dependant on the internal layout of the heap in BBFW.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
jgharston
Posts: 2762
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield

Re: Item Delimiters?

Postby jgharston » Tue Jun 20, 2017 12:10 am

And you can always do something like:

A$="HELP:LOAD:SAVE:INFO:SPOOL:":REM or any other delimiter
REPEAT
A%=INSTR(A$,":"):REM or any other delimiter
PRINT LEFT$(A$,A%-1)
A$=MID$(A$,A%+1)
UNTIL A$="" OR A%=0


which is the sort of code I use for holding a list of commands or help strings, see the Filer programs.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

User avatar
richmond62
Posts: 170
Joined: Sun Apr 16, 2017 3:15 pm

Re: Item Delimiters?

Postby richmond62 » Sat Jun 24, 2017 10:52 am

This was as about as far as I could get with 8-9 year olds:

Screen Shot 2017-06-24 at 1.51.37 pm.png


Return to “programming”

Who is online

Users browsing this forum: No registered users and 2 guests