What is POS(X) for?

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

What is POS(X) for?

Post by Jeff-20 »

Can anyone give me an example of using POS(x) in BASIC? I've never found a reason to use it, and it just occured to me that I've yet to see a BASIC program LISTing use it.
High Scores, Links, and Jeff's Basic Games page.
User avatar
srowe
Vic 20 Scientist
Posts: 1340
Joined: Mon Jun 16, 2014 3:19 pm

Re: What is POS(X) for?

Post by srowe »

I don't think I've ever used it myself before either. The only sort of use I can imaging would be to prevent text wrapping at the end of a line, like this

Code: Select all

10 READA$:IFA$="*"THENEND
20 P=POS(0)
30 IF LEN(A$)+P>22THENPRINT
40 PRINTA$" ";
50 GOTO10
100 DATATHIS,IS,A,SENTENCE,WITH,SOME,WORDS,OF,VARIOUS,LENGTHS
110 DATATHEY,SHOULD,BE,PRINTED,WITHOUT,WRAPPING
200 DATA*
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: What is POS(X) for?

Post by Mike »

POS(X) retrieves the horizontal position of the cursor in the current logical output line (as result of all characters output, TAB()- and SPC()-directives, and "," and ";" formatting specifiers) in the same fashion as PEEK() gets you back what you have POKEd, or the @(X,Y)-function in MINIGRAFIK tells you which points have been set with the "@" pixel plot or line draw command.

In all those cases the user program could - in principle! - keep track of the expected result by itself, but language support does the job already. With POS(X) a fairly comparable result comes from building a pending output string beforehand and using LEN() to determine its length. This is what I do in my DATA MAKER program or in the message routine of my REMAP tool, to quote:

Code: Select all

63996 INPUT"START";T:INPUT"END";E:INPUT"LINE NUMBER";L:L$=MID$(STR$(L),2)+" FORT="+MID$(STR$(T),2)+"TO"+MID$(STR$(E),2)+":READA:POKET,A:NEXT":GOTO63998
63997 L$=MID$(STR$(L),2)+" DATA ":FORT=STOE:N$=C$+MID$(STR$(PEEK(T)),2):C$=",":IFLEN(L$+N$)<88THENL$=L$+N$:NEXT
63998 PRINT"{CLR}"L$:POKE631,19:POKE632,13:POKE633,13:POKE198,3:IFT<=ETHENPRINT"L="L+1"{LEFT}:S="T"{LEFT}:E="E"{LEFT}:GOTO63997":END
63999 FORT=1TO4:PRINT63995+T:POKE633+T,13:NEXT:PRINT"LIST":POKE198,7
... in line 63997, see "LEN(L$+N$)<88". A little more complex, from REMAP:

Code: Select all

[...]
32 P$=S$(I)+" IS "+C$(C-8)+" AND CANNOT BE MAPPED TO "+S$(M(I))+".":GOTO34
33 P$="FOREGROUND MUST BE MULTI-COLOUR, AND IDENTICAL ACROSS WHOLE PICTURE."
34 @RETURN:X=1
35 Y=X+22:IFY>LEN(P$)THENPRINTP$:END
36 Y=Y-1:IFMID$(P$,Y,1)<>" "THENON-(Y>X)GOTO36:X=X+22:GOTO35
37 P$=LEFT$(P$,Y-1)+CHR$(13)+MID$(P$,Y+1):X=Y+1:GOTO35
[...]
... where X and Y "run" along P$ (thus already "know" where in the output string we are) and MID$(P$,Y,1) scans for a blank that ought be replaced by a carriage return for a word wrap. It is not (easily) known beforehand, where the line breaks should be, srowe's and my two examples show how to handle this (with or without POS(X)).

Using an array instead of PEEK/POKE of course does not account for any side effects (like VIC register writes or changing values in OS workspace).

With the "@"-example of MG, doing that task manually would be extremely impractical: it would require a big extra array and re-doing the algorithm of the line draw command by hand, which would defeat the purpose of a fast line draw within the BASIC extension!
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: What is POS(X) for?

Post by Jeff-20 »

I see. I guess there's a purpose. I just never found use for it. Thanks for explaining!
High Scores, Links, and Jeff's Basic Games page.
Post Reply