Search a list of records: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Added task description and zkl example)
Line 1: Line 1:
{{draft task}}
{{draft task|Array search}}
Given a vector type (such as list or array), which may be homogeneous or heterogeneous, search for something. Demonstrate by searching for at least two types of things (which may require multiple examples).
=={{header|Perl}}==
=={{header|Perl}}==


Line 9: Line 10:


<lang PHP>echo in_array('needle', ['hay', 'stack']) ? 'Found' : 'Not found';</lang>
<lang PHP>echo in_array('needle', ['hay', 'stack']) ? 'Found' : 'Not found';</lang>

=={{header|zkl}}==
<lang zkl>list:=List("one",2,3.4,2);
list.find(2).println(); //-->1
list.find("one").println(); //-->0
list.index(2,3).println(); //-->3 same as find but exception if not found
// start searching at third position</lang>

Revision as of 06:47, 10 October 2015

Search a list of records is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a vector type (such as list or array), which may be homogeneous or heterogeneous, search for something. Demonstrate by searching for at least two types of things (which may require multiple examples).

Perl

<lang perl>if(grep $_ eq $needle, @haystack) { print 'Found'; }</lang>

PHP

<lang PHP>echo in_array('needle', ['hay', 'stack']) ? 'Found' : 'Not found';</lang>

zkl

<lang zkl>list:=List("one",2,3.4,2); list.find(2).println(); //-->1 list.find("one").println(); //-->0 list.index(2,3).println(); //-->3 same as find but exception if not found

                            // start searching at third position</lang>