BBC BASIC for Windows
« Associative Arrays »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:33pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Associative Arrays  (Read 440 times)
g3nrw
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 74
xx Associative Arrays
« Thread started on: Nov 16th, 2013, 10:16am »

I want to set up a string array CAT$(9999), with each element containing a two-character code followed by a free-text description. For example:
~~~~~~~~~~~~~~~~
CAT$(14)="EX 035 CW Clipping 4mS"
CAT$(15)="EX 035 CW Clipping 6mS"
CAT$(35)="MD Mode USB"
~~~~~~~~~~~~~~~~

Is there a quick BB4W way to find the free text for an element once the two-character code is known, without doing a (perhaps lengthy) search for a match from the start of the array?

I thought of setting up the entries in strict alphabetical order of 2-character code, then doing a binary chop, but is there a quicker way?

Ian



User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Associative Arrays
« Reply #1 on: Nov 16th, 2013, 1:09pm »

on Nov 16th, 2013, 10:16am, g3nrw wrote:
I thought of setting up the entries in strict alphabetical order of 2-character code, then doing a binary chop, but is there a quicker way?

There's unlikely to be a quicker way, since SORTLIB is very fast (it's a machine-code shell sort) and the binary chop is very efficient. So if speed is your main concern that's the way I would recommend. Do remember to use the latest version of SORTLIB and specify ASCII sorting, so you can use regular comparison operators in the binary chop:

http://bb4w.wikispaces.com/Searching+ordered+lists

As far as alternative methods are concerned, you could prefix each element with a unique character and use INSTR:

Code:
      DIM CAT$(9999)
      CAT$() = "*"

      CAT$(14) = "*EX 035 CW Clipping 4mS"
      CAT$(15) = "*EX 035 CW Clipping 6mS"
      CAT$(35) = "*MD Mode USB"

      all$ = SUM(CAT$())
      REPEAT
        INPUT "Enter two-letter code: "code$
        I% = INSTR(all$, "*"+code$)
        IF I% THEN
          J% = INSTR(all$, "*", I%+1)
          PRINT "Free text is " MID$(all$, I%+4, J%-I%-4)
        ELSE
          PRINT "Not found"
        ENDIF
      UNTIL FALSE 

Of course you'd need to use BB4W version 6 if the total length of all the array elements exceeds 65535 characters.

Richard.
User IP Logged

g3nrw
Junior Member
ImageImage


member is offline

Avatar




PM


Posts: 74
xx Re: Associative Arrays
« Reply #2 on: Nov 18th, 2013, 7:14pm »

on Nov 16th, 2013, 1:09pm, Richard Russell wrote:
There's unlikely to be a quicker way,
[snip]
Richard.


Thanks Richard. Food for thought

Ian
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls