Search a list: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 75:
}
</lang>
 
=={{header|Ada}}==
<lang ada>with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 524 ⟶ 525:
First index for Zag: 1
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++}}==
Line 786 ⟶ 805:
Sherlock not found
</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}}==
Line 1,014 ⟶ 1,015:
println(find("Ronald")) # prints 3
println(find("McDonald")) # will throw</lang>
 
=={{header|Elena}}==
ELENA 5.0 :
Line 1,978 ⟶ 1,980:
[ 1 6 ]
]</pre>
 
=={{header|Lasso}}==
Lasso arrays have a findindex method which returns all matching indexes. [http://lassoguide.com/operations/containers.html?#array]
Line 2,078 ⟶ 2,081:
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>
 
 
=={{header|Lua}}==
Line 2,131 ⟶ 2,133:
CheckIt
</lang>
 
 
=={{header|Maple}}==
Line 2,198 ⟶ 2,199:
??? Error using ==> searchCollection at 11
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}}==
Line 2,244 ⟶ 2,224:
(%i37) catch(findneedle("Zag", haystack, 'l));
(%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}}==
Line 2,405 ⟶ 2,406:
else:
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}}==
Line 2,433 ⟶ 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>
 
Line 2,602 ⟶ 2,603:
7 Bush
</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}}==
Line 3,050 ⟶ 2,998:
find.needle(haystack3) # 3
find.needle(haystack3, needle="needles", ret=TRUE) # 3 5</lang>
 
 
=={{header|Racket}}==
Line 3,080 ⟶ 3,027:
'(#f 7)
</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}}==
Line 3,838 ⟶ 3,839:
End Sub</lang>{{out}}<pre>Washington not found in haystack.
Bush is at position 5. And last position is 8.</pre>
 
=={{header|VBScript}}==
Shamelessly derived from the BASIC version.
10,333

edits