PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Determining numbers used in an ASCII sequence.  (Read 178 times)

steven.rajski

  • Member
  • **
  • Posts: 10
    • View Profile
Determining numbers used in an ASCII sequence.
« on: June 23, 2022, 11:29:06 AM »
Trying to find the number of slots used between a starting and ending alphanumeric number, i.e. the number of slots used between A000ZZ and A0010F.  Positions 4 & 6 can be 0-Z, position 5 A-Z (with the exclusion of :;<=>?@)
)

Ken Sproul

  • Silver Member
  • ***
  • Posts: 46
    • View Profile
Re: Determining numbers used in an ASCII sequence.
« Reply #1 on: June 23, 2022, 02:52:10 PM »
Steven,

One way to accomplish this is:

1. Create a sort file where the key is the alphanumeric number.
          direct "numbers",6

2. Write all of the possible numbers to the file.
          write ("numbers",key=number$)

3. Compute the inclusive number of slots by subtracting the index of both values and adding 1.
          slots=ind("numbers",key="A0010F")-ind("numbers",key="A000ZZ")+1


Loren Doornek

  • Gold Member
  • ****
  • Posts: 55
    • View Profile
Re: Determining numbers used in an ASCII sequence.
« Reply #2 on: June 23, 2022, 03:24:25 PM »
That's an interesting challenge :-) 

I wrote a function to do this by converting the input values to a decimal number, then just subtracting the two values. You may need to tweak it depending on whether you need to include the starting/ending values.  You can test it by passing in hex values, then comparing to an actual hex value. 

Here's a couple examples of running it, and the code is further below.

Test two simple Hex values:
RUN
Value 1:0A
Value 2:0F
Type for each position: H
Difference is: 5

Test two numeric values:
RUN
Value 1:123
Value 2:456
Type for each position: NNN
Difference is: 333

Test two hex values, and validate result:
RUN
Value 1:01A1
Value 2:01F1
Type for each position: HHHH
Difference is: 80
PRINT DEC($0001A1$)
 417
PRINT DEC($0001F1$)
 497
PRNT 497-417
 80

Testing using your values:
RUN
Value 1:A000ZZ
Value 2:A001FF
Type for each position: ANNPAP
Difference is: 196

Here's the code:

Code: [Select]
0010 ! Trying to find the number of slots used between a starting and ending alphanumeric number, i.e. the number of slots used between A000ZZ and A0010F.  Positions 4 & 6 can be 0-Z, position 5 A-Z (with the exclusion of :;<=>?@)
0020 BEGIN
0030 LET type$="HHHHHH" ! Specify allowed characters for each slot.  H=Hex,N=Numbers,A=Alphanumeric,Blank=whatever default is setup in the function
0040 LET type$="ANNPAP",val1$="A000ZZ",val2$="A001FF" ! Defaults from example provided
0050 !
0060 INPUT EDIT "Value 1:",val1$
0070 INPUT EDIT "Value 2:",val2$
0080 INPUT EDIT "Type for each position: ",type$; LET type$=UCS(type$); IF POS("HNAP"^type$) THEN PRINT "Invalid Types"; GOTO *SAME
0090 !
0100 LET val1$=UCS(val1$),val2$=UCS(val2$)
0110 IF NUL(val1$) OR NUL(val2$) THEN END
0120 !
0130 LET ret1$=FNval$(val1$,type$)
0140 LET ret2$=FNval$(val2$,type$)
0150 !
0160 IF POS("ERROR"=ret1$+ret2$) THEN PRINT "Invalid Values Entered" ELSE PRINT "Difference is: "+STR(NUM(ret2$)-NUM(ret1$))
0170 END
0180 !
0200 ! ^100
0210 DEF FNval$(LOCAL val$, LOCAL type$)
0220 ! Define available characters
0230 LOCAL ret$,slots,alpha$,numer$,hex$,alphanumer$,i,val,deftype$
0240 LET alpha$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
0250 LET numer$="0123456789"
0260 LET alphanumer$=numer$+alpha$
0270 LET hex$="0123456789ABCDEF"
0280 !
0290 ! Define maximum length of input value
0300 LET slots=6 ! Max length of input value
0310 IF LEN(val$)>slots THEN LET ret$="ERROR"; GOTO 0640
0320 !
0330 ! Define available characters for each position
0340 LET deftype$="H"; IF NOT(NUL(type$)) THEN IF POS(MID(type$,1,1)="HNAP") THEN LET deftype$=MID(type$,1,1) ! Set the default type to whatever the first specified type is, or Hex by default
0350 LOCAL DIM avlchar$[1:slots]
0360 LET avlchar${ALL}=TBL(POS(MID(type$,1,1)="HNAP"),hex$,hex$,numer$,alpha$,alphanumer$) ! Set the default allowed characters for each slot. Default is the allowed characters from the first specified column, or hex if nothing specified
0370 !
0380 ! Set the allowed characters for each slot
0390 LET type$=PAD(type$,slots,0,deftype$) ! Pad type$ to correct length using default type
0400 FOR i=1 TO slots
0410 LET avlchar$[i]=TBL(POS(MID(type$,i,1)="HNAP"),$$,hex$,numer$,alpha$,alphanumer$)
0420 NEXT i
0430 !
0440 ! Determine how much 1 unit in each slot is worth
0450 LOCAL DIM slotval[1:slots]
0460 LET slotval[slots]=1
0470 FOR i=slots-1 TO 1 STEP -1
0480 LET slotval[i]=slotval[i+1]*LEN(avlchar$[i+1])
0490 NEXT i
0500 !
0510 ! Pad the input string, and check for invalid characters
0520 LET val$=PAD(MID(val$,1,slots),slots,0,"0") ! Pad the initial value to the correct length by prefixing with zeroes
0530 FOR i=1 TO LEN(val$)
0540 IF NOT(POS(MID(val$,i,1)=avlchar$[i])) THEN LET ret$="ERROR"
0550 NEXT i
0560 IF NOT(NUL(ret$)) THEN GOTO 0640
0570 !
0580 ! Calculate the value
0590 FOR i=1 TO LEN(val$)
0600 LET val+=(POS(MID(val$,i,1)=avlchar$[i])-1)*slotval[i]
0610 NEXT i
0620 LET ret$=STR(val)
0630 !
0640 RETURN ret$
0650 END DEF

Mike King

  • Administrator
  • Diamond Member
  • *****
  • Posts: 3626
  • Mike King
    • View Profile
    • PVX Plus Technologies
Re: Determining numbers used in an ASCII sequence.
« Reply #3 on: June 23, 2022, 03:59:59 PM »
My question is what exactly are you looking for?

You asked how to find "the number of slots used" -- that is different than the number of potential values between the two values you provided.  How is one to know if a slot is "Used" -- is this a file and you want to know how many records exists between the two keys?

If so then just get the difference in the RNO values for these two records as in:

OPEN (1) "TheFile"
READ (1,KEY="A000ZZ")
RNO_LOW = RNO(1)
READ (1,KEY="A0010F")
RNO_HI=RNO(1)

The number of USED slots between these two value would be the difference between the two RNO's less one.
« Last Edit: June 23, 2022, 04:53:15 PM by Mike King »