Search a list of records: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 505: | Line 505: | ||
Khartoum-Omdurman |
Khartoum-Omdurman |
||
4.580000 |
4.580000 |
||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
namespace RosettaSearchListofRecords |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
var dataset = new List<Dictionary<string, object>>() { |
|||
new Dictionary<string, object> {{ "name" , "Lagos"}, {"population", 21.0 }}, |
|||
new Dictionary<string, object> {{ "name" , "Cairo"}, {"population", 15.2 }}, |
|||
new Dictionary<string, object> {{ "name" , "Kinshasa-Brazzaville"}, {"population", 11.3 }}, |
|||
new Dictionary<string, object> {{ "name" , "Greater Johannesburg"}, {"population", 7.55 }}, |
|||
new Dictionary<string, object> {{ "name" , "Mogadishu"}, {"population", 5.85 }}, |
|||
new Dictionary<string, object> {{ "name" , "Khartoum-Omdurman"}, {"population", 4.98 }}, |
|||
new Dictionary<string, object> {{ "name" , "Dar Es Salaam"}, {"population", 4.7 }}, |
|||
new Dictionary<string, object> {{ "name" , "Alexandria"}, {"population", 4.58 }}, |
|||
new Dictionary<string, object> {{ "name" , "Abidjan"}, {"population", 4.4 }}, |
|||
new Dictionary<string, object> {{ "name" , "Casablanca"}, {"population", 3.98 }} |
|||
}; |
|||
// Find the (zero-based) index of the first city in the list whose name is "Dar Es Salaam" |
|||
var index = dataset.FindIndex(x => ((string)x["name"]) == "Dar Es Salaam"); |
|||
Console.WriteLine(index); |
|||
// Find the name of the first city in this list whose population is less than 5 million |
|||
var name = (string)dataset.Find(x => (double)x["population"] < 5.0)["name"]; |
|||
Console.WriteLine(name); |
|||
// Find the population of the first city in this list whose name starts with the letter "A" |
|||
var aNamePopulation = (double)dataset.Find(x => ((string)x["name"]).StartsWith("A"))["population"]; |
|||
Console.WriteLine(aNamePopulation); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
6 |
|||
Khartoum-Omdurman |
|||
4.58 |
|||
</pre> |
</pre> |
||
Line 555: | Line 599: | ||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre> |
|||
6 |
|||
Khartoum-Omdurman |
|||
4.58 |
|||
</pre> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
namespace RosettaSearchListofRecords |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
var dataset = new List<Dictionary<string, object>>() { |
|||
new Dictionary<string, object> {{ "name" , "Lagos"}, {"population", 21.0 }}, |
|||
new Dictionary<string, object> {{ "name" , "Cairo"}, {"population", 15.2 }}, |
|||
new Dictionary<string, object> {{ "name" , "Kinshasa-Brazzaville"}, {"population", 11.3 }}, |
|||
new Dictionary<string, object> {{ "name" , "Greater Johannesburg"}, {"population", 7.55 }}, |
|||
new Dictionary<string, object> {{ "name" , "Mogadishu"}, {"population", 5.85 }}, |
|||
new Dictionary<string, object> {{ "name" , "Khartoum-Omdurman"}, {"population", 4.98 }}, |
|||
new Dictionary<string, object> {{ "name" , "Dar Es Salaam"}, {"population", 4.7 }}, |
|||
new Dictionary<string, object> {{ "name" , "Alexandria"}, {"population", 4.58 }}, |
|||
new Dictionary<string, object> {{ "name" , "Abidjan"}, {"population", 4.4 }}, |
|||
new Dictionary<string, object> {{ "name" , "Casablanca"}, {"population", 3.98 }} |
|||
}; |
|||
// Find the (zero-based) index of the first city in the list whose name is "Dar Es Salaam" |
|||
var index = dataset.FindIndex(x => ((string)x["name"]) == "Dar Es Salaam"); |
|||
Console.WriteLine(index); |
|||
// Find the name of the first city in this list whose population is less than 5 million |
|||
var name = (string)dataset.Find(x => (double)x["population"] < 5.0)["name"]; |
|||
Console.WriteLine(name); |
|||
// Find the population of the first city in this list whose name starts with the letter "A" |
|||
var aNamePopulation = (double)dataset.Find(x => ((string)x["name"]).StartsWith("A"))["population"]; |
|||
Console.WriteLine(aNamePopulation); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 680: | Line 680: | ||
(city-index "alexandra") → #f |
(city-index "alexandra") → #f |
||
</lang> |
</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
Line 1,209: | Line 1,210: | ||
"Khartoum-Omdurman" |
"Khartoum-Omdurman" |
||
4.58</pre> |
4.58</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,790: | Line 1,789: | ||
Khartoum-Omdurman |
Khartoum-Omdurman |
||
4.58 |
4.58 |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 1,833: | Line 1,831: | ||
$index1 = first_index { $_->{name} eq 'Dar Es Salaam' } @cities;</lang> |
$index1 = first_index { $_->{name} eq 'Dar Es Salaam' } @cities;</lang> |
||
=={{header|Perl 6}}== |
|||
The built-in method <tt>.first</tt> fulfills the requirements of this task.<br> |
|||
It takes any [https://docs.perl6.org/language/operators#infix_~~ smart-matcher] as a predicate. The <tt>:k</tt> adverb makes it return the key (i.e. numerical index) instead of the value of the element. |
|||
{{Works with|Rakudo|2016.08}} |
|||
<lang perl6>my @cities = |
|||
{ 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 }, |
|||
; |
|||
say @cities.first(*<name> eq 'Dar Es Salaam', :k); |
|||
say @cities.first(*<population> < 5).<name>; |
|||
say @cities.first(*<name>.match: /^A/).<population>;</lang> |
|||
{{out}} |
|||
<pre> |
|||
6 |
|||
Khartoum-Omdurman |
|||
4.58 |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,149: | Line 2,117: | ||
6 |
6 |
||
"Khartoum-Omdurman" |
"Khartoum-Omdurman" |
||
4.58 |
|||
</pre> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
The built-in method <tt>.first</tt> fulfills the requirements of this task.<br> |
|||
It takes any [https://docs.perl6.org/language/operators#infix_~~ smart-matcher] as a predicate. The <tt>:k</tt> adverb makes it return the key (i.e. numerical index) instead of the value of the element. |
|||
{{Works with|Rakudo|2016.08}} |
|||
<lang perl6>my @cities = |
|||
{ 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 }, |
|||
; |
|||
say @cities.first(*<name> eq 'Dar Es Salaam', :k); |
|||
say @cities.first(*<population> < 5).<name>; |
|||
say @cities.first(*<name>.match: /^A/).<population>;</lang> |
|||
{{out}} |
|||
<pre> |
|||
6 |
|||
Khartoum-Omdurman |
|||
4.58 |
4.58 |
||
</pre> |
</pre> |