Search a list: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 75: Line 75:
}
}
</lang>
</lang>

=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
<lang ada>with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 524: Line 525:
First index for Zag: 1
First index for Zag: 1
Last index for Zag: 9</pre>
Last index for Zag: 9</pre>

=={{header|C sharp|C#}}==

<lang csharp>using System;
using System.Collections.Generic;

class Program {
static void Main(string[] args) {
List<string> haystack = new List<string>() { "Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo" };

foreach (string needle in new string[] { "Washington", "Bush" }) {
int index = haystack.IndexOf(needle);
if (index < 0) Console.WriteLine("{0} is not in haystack",needle);
else Console.WriteLine("{0} {1}",index,needle);
}
}
}</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 786: Line 805:
Sherlock not found
Sherlock not found
</pre>
</pre>

=={{header|C sharp|C#}}==

<lang csharp>using System;
using System.Collections.Generic;

class Program {
static void Main(string[] args) {
List<string> haystack = new List<string>() { "Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo" };

foreach (string needle in new string[] { "Washington", "Bush" }) {
int index = haystack.IndexOf(needle);
if (index < 0) Console.WriteLine("{0} is not in haystack",needle);
else Console.WriteLine("{0} {1}",index,needle);
}
}
}</lang>


=={{header|Ceylon}}==
=={{header|Ceylon}}==
Line 1,014: Line 1,015:
println(find("Ronald")) # prints 3
println(find("Ronald")) # prints 3
println(find("McDonald")) # will throw</lang>
println(find("McDonald")) # will throw</lang>

=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0 :
ELENA 5.0 :
Line 1,978: Line 1,980:
[ 1 6 ]
[ 1 6 ]
]</pre>
]</pre>

=={{header|Lasso}}==
=={{header|Lasso}}==
Lasso arrays have a findindex method which returns all matching indexes. [http://lassoguide.com/operations/containers.html?#array]
Lasso arrays have a findindex method which returns all matching indexes. [http://lassoguide.com/operations/containers.html?#array]
Line 2,078: Line 2,081:
showindex "dog [My dog has fleas] ; dog found at position 2 in My dog has fleas
showindex "dog [My dog has fleas] ; dog found at position 2 in My dog has fleas
showindex "cat [My dog has fleas] ; cat not found in My dog has fleas</lang>
showindex "cat [My dog has fleas] ; cat not found in My dog has fleas</lang>



=={{header|Lua}}==
=={{header|Lua}}==
Line 2,131: Line 2,133:
CheckIt
CheckIt
</lang>
</lang>



=={{header|Maple}}==
=={{header|Maple}}==
Line 2,198: Line 2,199:
??? Error using ==> searchCollection at 11
??? Error using ==> searchCollection at 11
The string 'g' does not exist in this collection of strings.</lang>
The string 'g' does not exist in this collection of strings.</lang>

=={{header|MAXScript}}==
<lang maxscript>haystack=#("Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo")

for needle in #("Washington","Bush") do
(
index = findItem haystack needle
if index == 0 then
(
format "% is not in haystack\n" needle
)
else
(
format "% %\n" index needle
)
)</lang>

{{out}}
<lang maxscript>Washington is not in haystack
5 Bush</lang>


=={{header|Maxima}}==
=={{header|Maxima}}==
Line 2,244: Line 2,224:
(%i37) catch(findneedle("Zag", haystack, 'l));
(%i37) catch(findneedle("Zag", haystack, 'l));
(%o37) 7</pre>
(%o37) 7</pre>

=={{header|MAXScript}}==
<lang maxscript>haystack=#("Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo")

for needle in #("Washington","Bush") do
(
index = findItem haystack needle
if index == 0 then
(
format "% is not in haystack\n" needle
)
else
(
format "% %\n" index needle
)
)</lang>

{{out}}
<lang maxscript>Washington is not in haystack
5 Bush</lang>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
Line 2,405: Line 2,406:
else:
else:
raise newException(ValueError, needle & " not in haystack")</lang>
raise newException(ValueError, needle & " not in haystack")</lang>

=={{header|Objective-C}}==
{{works with|Objective-C|2.0+}}
<lang objc>NSArray *haystack = @[@"Zig",@"Zag",@"Wally",@"Ronald",@"Bush",@"Krusty",@"Charlie",@"Bush",@"Bozo"];
for (id needle in @[@"Washington",@"Bush"]) {
int index = [haystack indexOfObject:needle];
if (index == NSNotFound)
NSLog(@"%@ is not in haystack", needle);
else
NSLog(@"%i %@", index, needle);
}</lang>


=={{header|Objeck}}==
=={{header|Objeck}}==
Line 2,433: Line 2,423:
};
};
}
}
}</lang>

=={{header|Objective-C}}==
{{works with|Objective-C|2.0+}}
<lang objc>NSArray *haystack = @[@"Zig",@"Zag",@"Wally",@"Ronald",@"Bush",@"Krusty",@"Charlie",@"Bush",@"Bozo"];
for (id needle in @[@"Washington",@"Bush"]) {
int index = [haystack indexOfObject:needle];
if (index == NSNotFound)
NSLog(@"%@ is not in haystack", needle);
else
NSLog(@"%i %@", index, needle);
}</lang>
}</lang>


Line 2,602: Line 2,603:
7 Bush
7 Bush
</pre>
</pre>

=={{header|Perl 6}}==

{{works with|Rakudo Star|2016.07}}
<lang perl6>my @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;
for <Washington Bush> -> $needle {
say "$needle -- { @haystack.first($needle, :k) // 'not in haystack' }";
}</lang>

{{out}}
<pre>
Washington -- not in haystack
Bush -- 4
</pre>

<br>
Or, including the "extra credit" task:
{{works with|Rakudo Star|2016.07}}
<lang perl6>my Str @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;

for <Washingston Bush> -> $needle {
my $first = @haystack.first($needle, :k);

if defined $first {
my $last = @haystack.first($needle, :k, :end);
say "$needle -- first at $first, last at $last";
}
else {
say "$needle -- not in haystack";
}
}</lang>

{{out}}
<pre>
Washingston -- not in haystack
Bush -- first at 4, last at 7
</pre>

The built-in method <code>.first</code> takes a [https://docs.perl6.org/language/operators#infix_~~ smart-matcher], and returns the first matching list element.<br>
The <code>:k</code> adverb tells it to return the key (a.k.a. list index) instead of the value of the matching element.<br>
The <code>:end</code> adverb tells it to start searching from the end of the list.<br>

<br>
If you plan to do many searches on the same large list, you might want to build a search hash first for efficient look-up:
<lang perl6>my @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;

my %index;
%index{.value} //= .key for @haystack.pairs;

for <Washington Bush> -> $needle {
say "$needle -- { %index{$needle} // 'not in haystack' }";
}</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 3,050: Line 2,998:
find.needle(haystack3) # 3
find.needle(haystack3) # 3
find.needle(haystack3, needle="needles", ret=TRUE) # 3 5</lang>
find.needle(haystack3, needle="needles", ret=TRUE) # 3 5</lang>



=={{header|Racket}}==
=={{header|Racket}}==
Line 3,080: Line 3,027:
'(#f 7)
'(#f 7)
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)

{{works with|Rakudo Star|2016.07}}
<lang perl6>my @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;
for <Washington Bush> -> $needle {
say "$needle -- { @haystack.first($needle, :k) // 'not in haystack' }";
}</lang>

{{out}}
<pre>
Washington -- not in haystack
Bush -- 4
</pre>

<br>
Or, including the "extra credit" task:
{{works with|Rakudo Star|2016.07}}
<lang perl6>my Str @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;

for <Washingston Bush> -> $needle {
my $first = @haystack.first($needle, :k);

if defined $first {
my $last = @haystack.first($needle, :k, :end);
say "$needle -- first at $first, last at $last";
}
else {
say "$needle -- not in haystack";
}
}</lang>

{{out}}
<pre>
Washingston -- not in haystack
Bush -- first at 4, last at 7
</pre>

The built-in method <code>.first</code> takes a [https://docs.perl6.org/language/operators#infix_~~ smart-matcher], and returns the first matching list element.<br>
The <code>:k</code> adverb tells it to return the key (a.k.a. list index) instead of the value of the matching element.<br>
The <code>:end</code> adverb tells it to start searching from the end of the list.<br>

<br>
If you plan to do many searches on the same large list, you might want to build a search hash first for efficient look-up:
<lang perl6>my @haystack = <Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo>;

my %index;
%index{.value} //= .key for @haystack.pairs;

for <Washington Bush> -> $needle {
say "$needle -- { %index{$needle} // 'not in haystack' }";
}</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==
Line 3,838: Line 3,839:
End Sub</lang>{{out}}<pre>Washington not found in haystack.
End Sub</lang>{{out}}<pre>Washington not found in haystack.
Bush is at position 5. And last position is 8.</pre>
Bush is at position 5. And last position is 8.</pre>

=={{header|VBScript}}==
=={{header|VBScript}}==
Shamelessly derived from the BASIC version.
Shamelessly derived from the BASIC version.