Search a list of records: Difference between revisions

Content added Content deleted
(Added Arturo implementation)
Line 477: Line 477:
return {result1, result2, result3} --> {6, "Khartoum-Omdurman", 4.58}
return {result1, result2, result3} --> {6, "Khartoum-Omdurman", 4.58}
end run</lang>
end run</lang>

=={{header|Arturo}}==

<lang rebol>; Rosetta Code task -- Search a list of records
define :city [name population][]

records: map [
["Lagos" 21.0]
["Cairo" 15.2]
["Kinshasa-Brazzaville" 11.3]
["Greater Johannesburg" 7.55]
["Mogadishu" 5.85]
["Khartoum-Omdurman" 4.98]
["Dar Es Salaam" 4.7]
["Alexandria" 4.58]
["Abidjan" 4.4]
["Casablanca" 3.98]
] => [to :city]

find: function [block f][
loop.with: 'i block 'elt [
if f elt -> return @[elt i]
]
return false
]

; Print the index of the first city named Dar Es Salaam.
print last find records $[c][equal? c\name "Dar Es Salaam"]

; Print the name of the first city with under 5 million people.
print get first find records $[c][less? c\population 5] 'name

; Print the population of the first city starting with 'A'.
print get first find records $[c][equal? first c\name `A`] 'population

</lang>

{{out}}

<pre>6
Khartoum-Omdurman
4.58</pre>


=={{header|C}}==
=={{header|C}}==