Search a list: Difference between revisions

m (format)
Line 3,676:
]
].</lang>
the above example did not raise an exception; the following does (a handler has been added to proceed for more search words):
{{works with|Smalltalk/X}}
<lang smalltalk>| haystack |
haystack := 'Zig,Zag,Wally,Ronald,Bush,Krusty,Charlie,Bush,Bozo' subStrings: $,.
[
{ 'Washington' . 'Bush' . 'Ronald' } do: [:word |
|firstIdx lastIdx|
 
firstIdx := haystack
indexOf:word
ifAbsent:[
ProceedableError raiseRequestWith:word errorString:'not found'.
0
].
firstIdx = 0 ifFalse:[
(lastIdx := haystack lastIndexOf:word) = firstIdx
ifTrue:[ e'the first index of {word} is {firstIdx}' printCR ]
ifFalse:[ e'the last index of {word} is {lastIdx}' printCR ]]
]
] on:Error do:[:ex |
e'{ex description} exception raised for: {ex parameter}' printCR.
'but I don''t care and proceed...' printCR.
ex proceed.
]</lang>
{{out}}
<pre>not found exception raised for: Washington
but I don't care and proceed...
the last index of Bush is 8
the first index of Ronald is 4</pre>
 
=={{header|Standard ML}}==
Anonymous user