Search a list of records: Difference between revisions
→{{header|Commodore BASIC}}: Use query model based on pseudo-higher-order programming.
(→{{header|Commodore BASIC}}: Add implementation.) |
(→{{header|Commodore BASIC}}: Use query model based on pseudo-higher-order programming.) |
||
Line 803:
=={{header|BASIC}}==
==={{header|Commodore BASIC}}===
C= BASIC has no associative data structure, so this just uses a two-dimensional array. It is written to accept a dynamically-sized list of cities and populations, with an empty-string sentinel indicating the end of the list.
It also uses a form of pseudo-higher-order programming: the search function uses the query function `FNQ`, which is redefined by the caller to match the desired criteria for each test.
Note that in text mode (upper/lowercase), BASIC keywords are only recognized in lowercase.
<lang gwbasic>100 nc=0
110 read n$
Line 817 ⟶ 820:
210 : next j
220 next i
230
240 print chr$(14);:rem text mode
250 print: print "Test 1. name='Dar Es Salaam':"
250 next i▼
260 rem search uses query function fnq
260 for i=0 to nc-1▼
270
280 gosub 500
300
310 print: print "Test 2. population < 5M:"
320 def fnq(i) = val(ci$(i,1)) < 5
330
340 if i<0 then print " None found."
340 data "Lagos", 21.0▼
350
360 print: print "Test 3. name like 'A%':"
360 data "Kinshasa-Brazzaville", 11.3▼
370 def fnq(i) = left$(ci$(i,0),1)="A"
370 data "Greater Johannesburg", 7.55▼
380 gosub 500
380 data "Mogadishu", 5.85▼
390 if i<0 then print " None found."
390 data "Khartoum-Omdurman", 4.98▼
400 if i>=0 then print " Population="ci$(i,1)"."
400 data "Dar Es Salaam", 4.7▼
410 end
410 data "Alexandria", 4.58▼
420 :
420 data "Abidjan", 4.4▼
430 data "Casablanca", 3.98▼
510 : if fnq(i) then return
440 data ""</lang>▼
530 i=-1
540 return
550 :
570 data "Cairo", 15.2
{{Out}}
<pre>ready.
run
Test 1. name='Dar Es Salaam':
Khartoum-Omdurman▼
Index= 6 .
Test 2. population < 5M:
▲ Name=Khartoum-Omdurman.
Test 3. name like 'A%':
Population=4.58.
ready.</pre>
|