How to resolve the algorithm Search a list of records step by step in the REXX programming language
How to resolve the algorithm Search a list of records step by step in the REXX programming language
Table of Contents
Problem Statement
Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.
If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list of records step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program (when using criteria) locates values (indices) from an associate array. */
$="Lagos=21, 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"
@.= '(city not found)'; city.= "(no city)" /*city search results for not found.*/
/* [↓] construct associate arrays. */
do #=0 while $\=''; parse var $ c '=' p "," $; c=space(c); parse var c a 2; @.c=#
city.#=c; pop.#=p; pop.c=#; if @.a==@. then @.a=c; /*assign city, pop, indices.*/
end /*#*/ /* [↑] city array starts at 0 index*/
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ task 1: show the INDEX of a city.*/
town= 'Dar Es Salaam' /*the name of a city for the search.*/
say 'The city of ' town " has an index of: " @.town /*show (zero─based) index of a city.*/
say /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ task 2: show 1st city whose pop<5 M*/
many=5 /*size of a city's pop in millions. */
do k=0 for # until pop.k
say '1st city that has a population less than ' many " million is: " city.k
say /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ task 3: show 1st city with A* name.*/
c1= 'A' /*1st character of a city for search*/
say '1st city that starts with the letter' c1 "is: " @.c1 /*stick a fork in it, all done*/
You may also check:How to resolve the algorithm Date format step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Sidef programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Clojure programming language
You may also check:How to resolve the algorithm Reflection/List methods step by step in the Perl programming language