SequenceableCollection.st
changeset 11878 dbc05293d160
parent 11875 79f71cb2270b
child 11879 e7d20a6e6719
--- a/SequenceableCollection.st	Mon Aug 24 21:28:48 2009 +0200
+++ b/SequenceableCollection.st	Thu Aug 27 13:35:28 2009 +0200
@@ -767,6 +767,27 @@
     "
 !
 
+indexOfNth:n occurrenceOf:what
+    "return the index of the nTh occurence of a value, or 0 if there are not that many"
+
+    |idx cnt|
+
+    n > self size ifTrue:[^ 0].
+    cnt := 0.
+    idx := 0.
+    [
+        idx := self indexOf:what startingAt:idx+1.
+        idx == 0 ifTrue:[^ 0].
+        cnt := cnt + 1.
+        cnt = n ifTrue:[ ^ idx].
+    ] loop.
+
+    "
+     #(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1  
+     #(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9  
+    "
+!
+
 keyAtEqualValue:value
     "return the index of a value.
      This is normally not used (use indexOf:), but makes the
@@ -7577,7 +7598,7 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.275 2009-08-24 10:49:15 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.276 2009-08-27 11:35:28 cg Exp $'
 ! !
 
 SequenceableCollection initialize!