Search a list of records: Difference between revisions

→‎{{header|Perl 6}}: Added implementation
(→‎{{header|zkl}}: added searches with multiple results)
(→‎{{header|Perl 6}}: Added implementation)
Line 281:
print 'Found';
}</lang>
 
=={{header|Perl 6}}==
There are several search operations that may be used. It mostly depends on whether you want to find actual values or pointers, and/or all possible values or a single value matching your criteria. The most appropriate for the given test data/operations are shown here.
 
<lang perl6>use JSON::Tiny;
 
my $cities = from-json('
[{"name":"Lagos", "population":21}, {"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}]
');
 
# Find the indicies of the cities named 'Dar Es Salaam'.
say grep-index( *<name> eq 'Dar Es Salaam', @$cities ); # (6)
 
 
# Find the name of the first city with a population less
# than 5 when sorted by population, largest to smallest.
say ($cities.sort( -*.<population> ).first: *.<population> < 5)<name>; # Khartoum-Omdurman
 
 
# Find all of the city names that contain an 'm'
say join ', ', sort grep( {$_<name>.lc ~~ /'m'/}, @$cities )»<name>; # Dar Es Salaam, Khartoum-Omdurman, Mogadishu</lang>
 
=={{header|PHP}}==
10,327

edits