Simplest editor for a String?

Discussions related to database technologies, file handling, directories and storage
Baldilocks
Posts: 8
Joined: Sat 30 Nov 2019, 10:04

Re: Simplest editor for a String?

Post by Baldilocks »

Many thanks Ken Down. Your post on the 1st showed me that I was making a mountain out of a molehill.
I realised that I didn't really need the mouse and that the reality was simpler than I had thought. Here's my solution:
(I have to avoid commas because the ultimate fate of the data is to export to a CSV file - removing the commas is easier than leaving them in.)

Code: Select all

   10 S$="ABCDEFG123456789"
   20 
   30 PROC_EditLine(S$)
   40 GOTO30
   50 END
   60 
   70 DEF PROC_EditLine(RETURN S$)
   80 LOCAL L$, R$ , G
   90 R$ = S$
  100 REPEAT
  110   PRINT TAB(0,15);L$;:COLOUR 9:PRINT "|";:COLOUR 0:PRINTR$;" " ;
  120   PRINT"Press a key "
  130   G = GET
  140   CASE G OF
  150     WHEN 8     : L$ = LEFT$(L$,LEN(L$)-1)
  160     WHEN 135 : R$ = MID$(R$,2)
  170     WHEN 136 : R$ = RIGHT$(L$,1) + R$ :  L$= LEFT$(L$,LEN(L$)-1)
  180     WHEN 137 : L$ = L$ + LEFT$(R$,1)  :  R$ = MID$(R$,2)
  190   ENDCASE
  200   IF G = 44 THEN 220 : REM Skip comma
  210   IF G > 31 AND G < 127 THEN L$= L$ + CHR$(G)
  220 UNTIL G = 13
  230 S$ = L$ + R$
  240 ENDPROC
Problem solved! Thanks everyone.

Baldi
David Williams

Re: Simplest editor for a String?

Post by David Williams »

Don't let Richard see that GOTO statement! :D
Baldilocks
Posts: 8
Joined: Sat 30 Nov 2019, 10:04

Re: Simplest editor for a String?

Post by Baldilocks »

What G**O? I used "IF THEN <Line number>" then used a blob of Tippex on my screen to hide it!
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Simplest editor for a String?

Post by KenDown »

Glad to have been of help.

Why on earth use a GOTO? What's wrong with

Code: Select all

REPEAT
PROC_EditLine(S$)
UNTIL FALSE
Likewise down at line 200

Code: Select all

IF G = 44 ELSE IF G > 31 AND G<127 THEN L$ = L$ + CHR$(G)
Once you have done that, you can get rid of the line numbers, which will improve readability - which in turn makes debugging easier.

You can further improve readability by following the convention that keywords like IF or REPEAT are in upper case but variable names are all in lower case. Thus

Code: Select all

IF g=44 ELSE IF g>31 AND g<127 l$+=CHR$(g)
In my opinion, readability is further improved (and execution speeded up slightly) by using integers unless you need fractions. So the final attempt at the line would be

Code: Select all

IF g%=44 ELSE IF g%>31ANDg%<127 l$+=CHR$g%
However the IF...ELSE business is really redundant. How about this:

Code: Select all

CASE g% OF
  WHEN 8:
  WHEN44:
  WHEN135:
  WHEN136:
  WHEN137:
OTHERWISE
  IF g%>31ANDg%<127 l$+=CHR$g%
ENDCASE
Baldilocks
Posts: 8
Joined: Sat 30 Nov 2019, 10:04

Re: Simplest editor for a String?

Post by Baldilocks »

Thanks again Richard, for your insight and comments.
I've had a look through the larger programs that I have written and it looks like I've never used a CASE before, so I have made some progress.
I feel like, compared with many on here, I am extremely amateurish - and this is my only defence. I don't program often, and most of that is in assembler called from C for arduino, but I hate C so use BBCBASIC when I can.

The first GOTO was simply a lashup of bits extracted from the main program to illustrate the PROC itself, the PROC is called from several places without the GOTO loop.

The second one is more complex.
140 CASE G OF
150 WHEN 8 : L$ = LEFT$(L$,LEN(L$)-1)
160 WHEN 135 : R$ = MID$(R$,2)
170 WHEN 136 : R$ = RIGHT$(L$,1) + R$ : L$= LEFT$(L$,LEN(L$)-1)
180 WHEN 137 : L$ = L$ + LEFT$(R$,1) : R$ = MID$(R$,2)
190 ENDCASE
200 IF G = 44 THEN 220 : REM Skip comma
210 IF G > 31 AND G < 127 THEN L$= L$ + CHR$(G)

And you suggest:

CASE g% OF
WHEN 8:
WHEN44:
WHEN135:
WHEN136:
WHEN137:
OTHERWISE
IF g%>31ANDg%<127 l$+=CHR$g%
ENDCASE

I''m not aware of ever having seen += before this thread. I saw it in KenDown's post and searched it in the BBCBASIC help but that wants 3 characters minimum for a search.
The reason for my GOTO in line 200 was to ensure that character 44 was not acted upon, but you've put it up in the list of WHENs, and I'm not sure why.

On the subject of readability - I have one working eye with a cataract and only one typing hand. I have a large monitor and purpose-made specs for seeing the screen and the keyboard. I still find it difficult to see the difference between $ and % (depending a lot on the fonts involved) and the first thing I did when I saw your IF g% line above was to explode it to: IF g% > 31 AND g% < 127 l$ += CHR$ g%
Then I had to work out why my capital L dollar had turned into IS . - lower l and different font dollar!

My point is that readability is, at least in part, quite literally in the eye of the beholder!

Thank you for the insight and the feedback.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Simplest editor for a String?

Post by KenDown »

Er - the comments about GOTO and CASE were mine, not Richard's. I am flattered that you should think it was his post, but he possibly is less so!

+= is a shorthand way of incrementing a value. So you can have a%=a%+4, but a%+=4 is shorter and, I believe, faster for the computer to interpret. What can be done with numbers can also be done with strings, so a$=a$+"ABC" can be written as a$+="ABC". Unfortunately, it only works for adding onto the end of a string. If you want to add to the beginning you still have to use a$="ABC"+a$

When working with numbers you can have += or -= or *= or /= but obviously these have no meaning when dealing with strings.

A WHEN statement does not have to be followed by an instruction, hence:

Code: Select all

CASE g% OF
  WHEN8: a$ = LEFT$(a$, n%-1) + MID$(a$, n%+1)
  WHEN 44    
  WHEN 136: x% += 1
  WHEN 137: x% -= 1
OTHERWISE
  IF etc
ENDCASE
You can see how the other numbers do something - move the cursor or delete a character, after the OTHERWISE you have the insert character routine, but after WHEN 44 there is nothing. The program simply notes that g% was equal to 44 and goes on to the next g%.

Note that WHEN 44 is marginally faster than IF g% = 44 as you can see by running this little program:

Code: Select all

      TIME=0
      g%=32
      FORi%=0TO9000000
        CASEg%OF
          WHEN44
        ENDCASE
      NEXT
      PRINTTIME

      TIME=0
      FORi%=0TO9000000
        CASEg%OF
        ENDCASE
        IFg%=44
      NEXT
      PRINTTIME
Incidentally, if you are new to CASE there is a delightful use for it:

Code: Select all

CASE TRUE OF
  WHEN a% = 3
  WHEN x% = 7 AND c% < 23
OTHERWISE
ENDCASE
This allows you to test for multiple variables. The first example above only considers the variable g%, but in this second example you can test for a%, x%, c% or any other variables. So long as the expression after the WHEN evaluates to TRUE, the condition is executed. The only thing to bear in mind is that the first expression to evaluate to TRUE is executed and then the program jumps to ENDCASE, so if you have multiple conditions the second one to evaluate to TRUE will not even be considered. You have to order the WHENs carefully!

I'm sorry to hear of your sight problems and have tried to put in all the spaces that I normally leave out.
DDRM

Re: Simplest editor for a String?

Post by DDRM »

Hi folks,

At Richard's suggestion, I've added code tags to some of these posts, where it might be useful to the reader to copy it, and/or to make it easier to read. If you don't like it, feel free to take them out again, and/or to ask me not to do it!

In case anyone doesn't know about them, if you enclose a block of code inside tags like this: "{code] here is some code [/code}" (note my "mistaken" use of curly brackets, to stop it treating it as a real tag! - you should use square brackets), it will be put in a box which allows easy selection/copying, like this:

Code: Select all

 here is some code 
.

If you are posting more than a couple of lines for illustrative purpose, it's probably good to present it that way.

Best wishes,

D