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. Note that in text mode (upper/lowercase), BASIC keywords are only recognized in lowercase.
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 for i=0 to nc-1:
240 print chr$(14);:rem text mode
240 : if ci$(i,0) = "Dar Es Salaam" then print i:goto 260
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 :def p=valfnq(i) = ci$(i,1)0) = "Dar Es Salaam"
280 gosub 500
280 :290 if pi<50 then print ci$(i,0):" gotoNone 300found."
290 next i
300 forif i>=0 tothen nc-1print " Index="i"."
310 print: print "Test 2. population < 5M:"
310 : if left$(ci$(i,0),1)="A" then print ci$(i,1):goto 330
320 def fnq(i) = val(ci$(i,1)) < 5
320 next i
330 endgosub 500
340 if i<0 then print " None found."
340 data "Lagos", 21.0
350 dataif i>=0 then print "Cairo Name="ci$(i, 150)".2"
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
260500 for i=0 to nc-1
430 data "Casablanca", 3.98
510 : if fnq(i) then return
440 data ""</lang>
250520 next i
530 i=-1
540 return
550 :
340560 data "Lagos", 21.0
570 data "Cairo", 15.2
360580 data "Kinshasa-Brazzaville", 11.3
370590 data "Greater Johannesburg", 7.55
380600 data "Mogadishu", 5.85
390610 data "Khartoum-Omdurman", 4.98
400620 data "Dar Es Salaam", 4.7
410630 data "Alexandria", 4.58
420640 data "Abidjan", 4.4
430650 data "Casablanca", 3.98
440660 data ""</lang>
 
{{Out}}
<pre>ready.
run
 
6
Test 1. name='Dar Es Salaam':
Khartoum-Omdurman
Index= 6 .
4.58
 
Test 2. population < 5M:
Name=Khartoum-Omdurman.
 
Test 3. name like 'A%':
Population=4.58.
 
ready.</pre>
1,480

edits