Search a list of records: Difference between revisions

m (syntax highlighting fixup automation)
 
(6 intermediate revisions by 5 users not shown)
Line 1,335:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,355:
};
var index := dataset.selectBy::(r => r.Name).toArray().indexOfElement("Dar Es Salaam");
console.printLine(index);
var name := dataset.filterBy::(c => c.Population < 5.0r).toArray().FirstMember.Name;
console.printLine(name);
var namePopulation := dataset.filterBy::(c => c.Name.startingWith("A")).toArray().FirstMember.Population;
console.printLine(namePopulation)
}</syntaxhighlight>
Line 2,606:
Khartoum-Omdurman
4.58
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
program Search_list_records;
{$mode ObjFPC}{$H+}
 
type
TCity = record
name: string;
population: real;
end;
 
const
Cities: array of TCity = (
(name: 'Lagos'; population: 21.0),
(name: 'Cairo'; population: 15.2),
(name: 'Kinshasa-Brazzaville'; population: 11.3),
(name: 'Greater Johannesburg'; population: 7.55),
(name: 'Mogadishu'; population: 5.85),
(name: 'Khartoum-Omdurman'; population: 4.98),
(name: 'Dar Es Salaam'; population: 4.7),
(name: 'Alexandria'; population: 4.58),
(name: 'Abidjan'; population: 4.4),
(name: 'Casablanca'; population: 3.98)
);
 
function FindCityIndex(const CityName: string): Integer;
var
i: Integer;
begin
Result := -1;
for i := 0 to High(Cities) do
if Cities[i].name = CityName then
Exit(i);
end;
 
function FindCityName(const pop: real): string;
var
City: TCity;
begin
Result := 'not found';
for City in Cities do
if City.population < pop then
Exit(City.name);
end;
 
function FindCityPopulation(const Start: Char): Real;
var
City: TCity;
begin
Result := -1;
for City in Cities do
if City.name[1] = Start then
Exit(City.population);
end;
 
begin
writeln('index: ', FindCityIndex('Dar Es Salaam'));
writeln('name: ', FindCityName(5.0));
writeln('population: ', FindCityPopulation('A'):4:2);
end.
 
</syntaxhighlight>
{{out}}
<pre>
index: 6
name: Khartoum-Omdurman
population: 4.58
</pre>
 
=={{header|Perl}}==
Line 2,708 ⟶ 2,777:
</pre>
Note that Phix subscripts are 1-based, hence the output of 7 not 6.
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
 
(
( "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 )
)
 
len for >ps
( tps 1 ) sget "Dar Es Salaam" == if ps> 1 - ? exitfor else cps endif
endfor
 
len for
get 2 get 5 < if 1 get ? drop exitfor else drop endif
endfor
 
len for >ps
( tps 1 1 ) sget 'A' == if ( ps> 2 ) sget ? exitfor else cps endif
endfor</syntaxhighlight>
{{out}}
<pre>6
Khartoum-Omdurman
4.58
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
Line 3,515 ⟶ 3,618:
Khartoum-Omdurman
4.58</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
const cities = {"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}
 
fn main() {
mut count := 0
mut result :=""
for city, population in cities {
count++
if city == "Dar Es Salaam" {
if !result.contains("Index") {result += "Index of '${city}': ${count - 1}\n"}
}
if population < 5 {
if !result.contains("million") {result += "First city with less than 5 million: ${city}\n"}
}
if city[0].ascii_str() == "A" {
if !result.contains("letter") {result += "First population that starts with letter 'A': ${population}\n"}
}
}
println(result.all_before_last("\n"))
}
</syntaxhighlight>
 
{{out}}
<pre>
First city with less than 5 million: Khartoum-Omdurman
Index of 'Dar Es Salaam': 6
First population that starts with letter 'A': 4.58
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Tuple
 
var Element = Tuple.create("Element", ["record", "index"])
45

edits