# count positions in comma delimited string of numbers

## Recommended Posts

Hi all, I'm trying to find a way to COUNT the position between x2 given numbers of a comma delimited string of numbers

Lets say I got

\$string1="5,12,45,67,2,4,50,10,23"

I want to input, lets say 5 then the number 67 and the program to output the number 3 (which means 5 is 3 numbers away from number 5 & commas excluded)

Respectively if i input as starting number the number 2 and then the number 23 I want the program to output 4 which means 23 is 4 numbers away on the right from number 2.

Seems i cant do something like that with the string functions like StringMid or the other functions that get/count position because i want it to start counting AFTER the first number that is input by the user till it reaches the second number that is also output by the user irrespective where the x2 numbers are located within the string.

Only think i thought is to find the string length then substract the positions of the other 2 numbers ? but how do we do this ?

One other thing, is it possible also with autoit for someone to input number 12 and the poisition number he wishes and autoit to output him the number in that position ? for example the user to enter number 12 and "distance 5" and the program to give him the number 50 which is 5 numbers away from 12 ?

cheers

Edited by asiawatcher

##### Share on other sites

There are actually only two numbers between 5 and 67

```#include <String.au3>

Global \$sString = "5,12,45,67,2,4,50,10,23"
Global \$sString2 = "Hello AutoIt forum! How's everyone doing today?"

ConsoleWrite("Numbers between 5 and 67: " & CountBetween(\$sString, 5, 67) & @LF)
ConsoleWrite("Words between the words 'Hello' and 'everyone': " & CountBetween(\$sString2, "Hello", "everyone", " ") & @LF)

Func CountBetween(Const \$sText, Const \$sFirst, Const \$sSecond, Const \$sDelim = ",")
; Get the starting position of the first number
Local \$iStart = StartingPosition(\$sText, \$sFirst, \$sDelim)
; Error getting the first number, set error to 1
If (@Error) Then Return SetError(1, 0, "")
Local \$iEnd = StartingPosition(\$sText, \$sSecond, \$sDelim)
; Error getting the second number, set error to 2
If (@Error) Then Return SetError(2, 0, "")
; Do a RegExpReplace because it will store the number of times it replaces the delimiter in the @Extended macro
StringRegExpReplace(StringMid(\$sText, \$iStart, Abs(\$iStart - \$iEnd)), \$sDelim, "")
; Return the count
; Subtract one to account for the extra delimiter
; There's only 1 number between 5 and 45, but there are two commas (one on each side)
Return @Extended - 1
EndFunc

Func StartingPosition(Const \$sText, Const \$sSearch, Const \$sDelim)
; Need the length of the delimiter
Local \$iDelimLength = StringLen(\$sDelim)
; Split the string, use the entire delimiter and don't have the count in the [0] element
Local \$aSplit = StringSplit(\$sText, \$sDelim, \$STR_ENTIRESPLIT + \$STR_NOCOUNT)
Local \$iPosition = 1

For \$i = 0 to UBound(\$aSplit) - 1
; If this is the search, return the starting position of it
If (\$aSplit[\$i] = \$sSearch) Then Return \$iPosition
; Increment position by the length of the string + the length of the delimiter
\$iPosition += StringLen(\$aSplit[\$i]) + \$iDelimLength
Next
; Couldn't find it
Return SetError(1, 0, 0)
EndFunc```

As for the other thing, just split the string like I did in StartingPosition, find the first value, then add position you want to the index

```Local \$aSplit = StringSplit(\$sText, \$sDelim, \$STR_ENTIRESPLIT + \$STR_NOCOUNT)
For \$i = 0 to UBound(\$aSplit) - 1
If (\$aSplit[\$i] = Value to search for) Then Return \$aSplit[\$i + How many positions ahead?]
Next```

##### Share on other sites

Thanks the first example works the second always returns -1 what am i doing wrong ?

#include <String.au3>

Global \$sString = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26,0"

Global \$sValuee = InputBox("Testing", "Enter first number", "", " M2")
Global \$sValueee = InputBox("Testing", "distance?", "", " M2")

msgbox (0,"hmmm","The number starting from "& \$sValuee & " and distance " &\$sValueee& " is " & CountBetween(\$sString, \$sValuee, \$sValueee))

Func CountBetween(Const \$sText, Const \$sFirst, Const \$sSecond, Const \$sDelim = ",")
; Get the starting position of the first number
Local \$iStart = StartingPosition(\$sText, \$sFirst, \$sDelim)
; Error getting the first number, set error to 1
If (@Error) Then Return SetError(1, 0, "")
Local \$iEnd = StartingPosition(\$sText, \$sSecond, \$sDelim)
; Error getting the second number, set error to 2
If (@Error) Then Return SetError(2, 0, "")
; Do a RegExpReplace because it will store the number of times it replaces the delimiter in the @Extended macro
StringRegExpReplace(StringMid(\$sText, \$iStart, Abs(\$iStart - \$iEnd)), \$sDelim, "")
; Return the count
; Subtract one to account for the extra delimiter
; There's only 1 number between 5 and 45, but there are two commas (one on each side)
Return @Extended - 1
EndFunc

Func StartingPosition(Const \$sText, Const \$sSearch, Const \$sDelim)
; Need the length of the delimiter
Local \$iDelimLength = StringLen(\$sDelim)
; Split the string, use the entire delimiter and don't have the count in the [0] element
Local \$aSplit = StringSplit(\$sText, \$sDelim, \$STR_ENTIRESPLIT + \$STR_NOCOUNT)
Local \$iPosition = 1

For \$i = 0 to UBound(\$aSplit) - 1
If (\$aSplit[\$i] = \$sValuee) Then Return \$aSplit[\$i + \$sValueee]
Next

; Couldn't find it
Return SetError(1, 0, 0)
EndFunc

Edited by asiawatcher

##### Share on other sites

You're getting -1 because you're still doing the StringRegExpReplace and returning the Extended -1. Make a separate function that returns the number in the offset index

##### Share on other sites

Another example :

```Local \$sString = "5,12,45,67,2,4,50,10,23"
ConsoleWrite( _CountBetween(\$sString,  5, 67) )

Func _CountBetween(\$sInput, \$sFirst, \$sSecond)
Local \$sShotString = StringRegExpReplace(\$sInput, ".*?(?:^|,)" & \$sFirst & "(?=,|\$)|," & \$sSecond & "(?=,|\$)\K.+", "")
If @extended <= 1 Then Return SetError(1, 0, 0)
StringRegExpReplace(\$sShotString, "\d+", "")
Return @extended
EndFunc```

edited, thanks JC

Edited by jguinch

##### Share on other sites

I suspect a typo here:

`Local \$sShotString = StringRegExpReplace(\$sInput, ".*?(?:^|,)" & \$sFirst & "(?=,|\$)|," & \$sSecond & "(?=,|\$)\K.+", "")`

The pattern could also be a bit simpler ;-)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

A bit simpler

```Local \$sString = "8,5,12,45,67,2,4,50,10,23"
msgbox( 0,"", _CountBetween2(\$sString,  5, 4) )

Func _CountBetween2(\$sInput, \$sFirst, \$sSecond)
Local \$sShotString = StringRegExpReplace(\$sInput, ".*\b" & \$sFirst & "\b(.*?)\b" & \$sSecond & "\b.*", "\$1")
If not @extended Then Return SetError(1, 0, 0)
StringReplace(\$sShotString, ",", "")
Return @extended
EndFunc```

Edited by mikell

##### Share on other sites

A little tweak to a bit simpler and another example.

```Local \$sString = "8,5,12,45,67,2,4,50,10,23"
Local \$sString2 = "Hello AutoIt forum! How's everyone doing today?"

MsgBox(0, "", _CountBetween2(\$sString, 8, 45))
MsgBox(0, "", _CountBetween2(\$sString2, "Hello", "everyone", " "))

Func _CountBetween2(\$sInput, \$sFirst, \$sSecond, \$Delimiter = ",")
Local \$sShotString = StringRegExpReplace(\$sInput, ".*\b" & \$sFirst & "\b(.*?)\b" & \$sSecond & "\b.*", "\$1")
If Not @extended Then Return SetError(1, 0, 0)
StringReplace(\$sShotString, \$Delimiter, "")
Return @extended
EndFunc   ;==>_CountBetween2```

##### Share on other sites

2 functions _CountBetween and _CountNr useful for the OP purpose, but a bit more generalized

Search function _CountBetween will accept the 2 numbers in any order, it will return the distance between them and additional infos in @extent

second function _CountNr will return the element located at the wanted offset starting from the passed item

```Local \$sString = "8,5,12,45,67,2,4,50,10,23,hello"

_ShowResult(_CountBetween(\$sString, 5, 8), @error, @extended)
_ShowResult(_CountNr(\$sString, 50, -1), @error, @extended)
_ShowResult(_CountNr(\$sString, 2, 5), @error, @extended)

Func _CountBetween(\$sInput, \$sFirst, \$sSecond, \$sDelim = ",")
; returns the distance between first and second element
; @extent gives you additional infos:
; 0 = First element is on the left.
; 1 = First element is on the right
; 2 = First and second item are same
;
; If an error occurs @error is set to 1
; and @extend as following:

\$sInput = \$sDelim & \$sInput & \$sDelim
Local \$iExtended = 0, \$aFound[2] = [StringInStr(\$sInput, \$sDelim & \$sFirst & \$sDelim), StringInStr(\$sInput, \$sDelim & \$sSecond & \$sDelim)]
If Not (\$aFound[0] And \$aFound[1]) Then Return SetError(1, 1 * Not (\$aFound[0]) + 2 * Not (\$aFound[1]), 0) ; none or only one found
Local \$0 = Number(\$aFound[0] >= \$aFound[1]), \$1 = Number(\$aFound[0] <= \$aFound[1]) ; arrange indexes of found elements
StringReplace(StringMid(\$sInput, \$aFound[\$0], \$aFound[\$1] - \$aFound[\$0] + 1), \$sDelim, \$sDelim) ; count distance of elements
Return SetError(0, \$0 + 1 * (\$aFound[0] = \$aFound[1]), @extended - 1)
EndFunc   ;==>_CountBetween

Func _CountNr(\$sInput, \$sItem, \$iNr, \$sDelim = ",")
; returns the number located at distance \$iNr from passed item
; \$iNr can be positive or negative
; @extent 3 all ok or 4 if offset is 0
;
; if an error occurs @error is set to 1
; and @extend is 1 or 4
; 4 = searc is out of bounds

\$aInput = StringSplit(\$sInput, \$sDelim, 2) ; \$sInput to array
For \$i = 0 To UBound(\$aInput) - 1 ; search \$sFirst
If \$aInput[\$i] = \$sItem Then
Local \$iNDX = \$i + \$iNr
If \$iNDX < 0 Or \$iNDX > UBound(\$aInput) - 1 Then Return SetError(1, 4, "")
Return SetError(0, 3 + 1 * (\$iNDX = \$i), \$aInput[\$iNDX]) ; @extent 3 all ok or 4 if offset is 0
EndIf
Next
Return SetError(1, 1, 0)
EndFunc   ;==>_CountNr

Func _ShowResult(\$result, \$error, \$extended) ; just to show results
Local Static \$aInfos[5] = ["First element is on the left", "First element is on the right", "First and second are same", "ok", "Distance is 0 returned same number"]
If \$error Then
\$sMsg = "Error: " & \$aErrors[\$extended]
Else
\$sMsg = "Result is " & \$result & @CRLF & \$aInfos[\$extended]
EndIf
MsgBox("", "err." & \$error & " ext." & \$extended, \$sMsg)
EndFunc   ;==>_ShowResult```

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

What i might ask might sound a bit strange

So i have a string like this

Global \$sString1 = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26"

Then i got another string like this

Global \$sString2 ="0,10,36,4,25,22,1,35,9,5,28,3,29,3,20,25,18,31,8,4,11"

How can I take groups of 2 in string2 like 36,4 then 25,22 etc and count the "DISTANCE" of each group of 2 from the FIRST NUMBER ZERO and from 10 -the first 2 numbers of string 2- (and from each)  where distances are in string1 and also display a message when distances of the group of both numbers are less than 3 characters away from zero, from 10 or from both 0 and 10  (commas are excluded)

what i have now is this which i have to modify it to fit my purpose (Thanks InunoTaishou)

Global \$split = StringSplit(\$string2, ",")

For \$i = 1 To \$split[0] ; Loop through the array returned by StringSplit to display the individual values.

\$var = CountBetween(\$sString, \$split[1], \$split[\$i])+1   ;; \$split[1] is zero

;;still unfinished

next

Func CountBetween(Const \$sText, Const \$sFirst, Const \$sSecond, Const \$sDelim = ",")
; Get the starting position of the first number
Local \$iStart = StartingPosition(\$sText, \$sFirst, \$sDelim)
; Error getting the first number, set error to 1
If (@Error) Then Return SetError(1, 0, "")
Local \$iEnd = StartingPosition(\$sText, \$sSecond, \$sDelim)
; Error getting the second number, set error to 2
If (@Error) Then Return SetError(2, 0, "")
; Do a RegExpReplace because it will store the number of times it replaces the delimiter in the @Extended macro
StringRegExpReplace(StringMid(\$sText, \$iStart, Abs(\$iStart - \$iEnd)), \$sDelim, "")
; Return the count
; Subtract one to account for the extra delimiter
; There's only 1 number between 5 and 45, but there are two commas (one on each side)
Return @Extended - 1
EndFunc

Func StartingPosition(Const \$sText, Const \$sSearch, Const \$sDelim)
; Need the length of the delimiter
Local \$iDelimLength = StringLen(\$sDelim)
; Split the string, use the entire delimiter and don't have the count in the [0] element
Local \$aSplit = StringSplit(\$sText, \$sDelim, \$STR_ENTIRESPLIT + \$STR_NOCOUNT)
Local \$iPosition = 1

For \$i = 0 to UBound(\$aSplit) - 1
; If this is the search, return the starting position of it
If (\$aSplit[\$i] = \$sSearch) Then Return \$iPosition
; Increment position by the length of the string + the length of the delimiter
\$iPosition += StringLen(\$aSplit[\$i]) + \$iDelimLength
Next
; Couldn't find it
Return SetError(1, 0, 0)
EndFunc

Edited by asiawatcher

##### Share on other sites

The very first part is easy:

```Local \$sString1 = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26"
Local \$sString2 ="0,10,36,4,25,22,1,35,9,5,28,3,29,3,20,25,18,31,8,4,11"    ; an odd number of values, last 11 is orphan
Local \$aDistances = StringSplit(\$sString1, ',', 2)
Local \$aCouples = StringRegExp(\$sString2, "(\d+,\d+)", 3)
_ArrayDisplay(\$aCouples)

For \$vv In \$aCouples
; do something clear as dry mud... ?
Next```

but your attempt at specifying what to do next is a complete failure for the mere human I am.
May I ask what is the rationale for such a processing? Which kind of data are you playing with?

Also PLEASE, use code boxes (the <> icon) to host your code.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Hi thanks for your answer, I can see you have created x2 arrays for the data but how can i do the processing ?

I want to use those data for statistics purposes, in this example i used numbers from a roulette wheel

in string2 you have the first group of 0,10 which are opposite from each other (the first group of x2 numbers will always be opposite to each other by choice). I want the second group of x2 to be both numbers of group close to 0, both numbers of group close to 10, or each number close to both 0 and 10 (or close to the straight line that connects 0 and 10 if you see a european roulette wheel)  and when that happens display a message with the group that meets this criteria otherwise ignore it. Close to zero 10 or both means the max 5 numbers away.

How do you count the distances  of how "far" (how many numbers away) 36,4 is for example from 0,10 you will say ? you get those from string1

In this example (0,10,4,36) 4 is close to 0 and 36 to 10 so its ok, if numbers were 0,10,3,15 both 3 and 15 are close to zero (but far from 10 but we dont care as they are both close to one number of the first group) so again its a good group, the same goes if both numbers of second group were close to 10 but far from zero which is opposite BUT if we have 0,10,30,34 its not ok because 30 is close to 10 but 34 is far both from 10 and zero

Does this make any sence ? This logic can be used in many other environments for data analysis and statistics. Roulette wheel was an example as it works in similar way with what i wanna do. (and its nothing to do with bots or automate tasks just number crunching)

cheers !

##### Share on other sites
3 hours ago, asiawatcher said:

in string2 you have the first group of 0,10 which are opposite from each other

Certainly not opposite. Since a roulette wheel has 37 slots, the "opposite" of 0 is exactly between 10 and 5. Here I'm refereing to the European roulette shown here. Adapting to a different distribution doesn't change that, as long as slots are in the [0, 36] range.

This will list minimal distance from zero for a few samples:

```Local \$aSequence = [0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26]
Local \$aSamples = [34, 17, 5, 2, 30, 14, 0, 9]

Local \$iDistance
For \$i In \$aSamples
\$iDistance = _ArraySearch(\$aSequence, \$i)
\$iMinDistance = _Min(\$iDistance, UBound(\$aSequence) - \$iDistance)
ConsoleWrite(\$i & " is " & \$iMinDistance & " away (" & (\$iDistance > UBound(\$aSequence) / 2 ? "back" : "for") & "wards) from 0" & @LF)
Next```

Now I don't get what you say about subsequent "groups".

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

see my post below

Edited by asiawatcher

##### Share on other sites

see my post below

Edited by asiawatcher

##### Share on other sites

EVERYTHING SOLVED THANKS FOR YOUR HELP EVERYONE !

Only thing left really is that some distances are not counted correctly

check my code

```#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Tidy_Stop_OnError=n
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/rsln /mo
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <FileConstants.au3>
; *** End added by AutoIt3Wrapper ***
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <string.au3>
#include <FTPEx.au3>
#include <File.au3>
#include <Misc.au3>
#include <ClipBoard.au3>
#include <date.au3>
#include <ProgressConstants.au3>
#include <IE.au3>
#Include <Constants.au3>
#Include <INET.au3>
#include <SendMessage.au3>
#include <File.au3>
#include <String.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include "Misc.au3"
#include "date.au3"
#include <Array.au3>
#include <Math.au3>

FileDelete("one.txt")

Global \$sString = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26"

Global \$input = InputBox("ΑΡΙΘΜΟΙ ΡΟΥΛΕΤΑΣ", "ΒΑΛΤΕ ΤΟΥΣ ΑΡΙΘΜΟΥΣ ΧΩΡΙΣΜΕΝΟΥΣ ΜΕ ΚΟΜΜΑ", "0,6,36,4,25,22,1,35,9,5,28,3,29,3,20,25,18,31,8,4,11", "")
Global \$split = StringSplit(\$input, ",")
Global \$firstnum= StringLeft(\$input, 1) ; last number

For \$i = 1 To \$split[0] ; Loop through the array returned by StringSplit to display the individual values.

\$var = CountBetween(\$sString, \$split[1], \$split[\$i])+1
;;\$varr = CountBetween(\$sString, \$split[1], \$split[\$i+1])+1

if \$var<6 and \$var>0  then ;;if closer than 6 pockets 0--> number

\$hFileOpen1 = FileOpen("one.txt", \$FO_APPEND)
FileWrite(\$hFileOpen1, \$split[\$i]&",")
FileClose(\$hFileOpen1)

elseif \$var>31 then ;;now check from the opposite side number -->0

\$hFileOpen1 = FileOpen("one.txt", \$FO_APPEND)
FileWrite(\$hFileOpen1, \$split[\$i]&",")
FileClose(\$hFileOpen1)

elseif \$var<24 and \$var>12 then

\$hFileOpen1 = FileOpen("one.txt", \$FO_APPEND)
FileWrite(\$hFileOpen1, \$split[\$i]&",")
FileClose(\$hFileOpen1)

else

\$hFileOpen1 = FileOpen("one.txt", \$FO_APPEND)
FileWrite(\$hFileOpen1, "X,")
FileClose(\$hFileOpen1)

endif

Next

FileClose(\$ffo22)

\$trimmed = StringTrimLeft(\$ffr22, 2)
\$trimmed2 = StringTrimRight(\$trimmed, 1)

Example()

Func Example()
Local \$aDays = StringSplit(\$trimmed2, ",") ; Split the string of days using the delimiter "," and the default flag value.
#cs
The array returned will contain the following values:
...
#ce

For \$i = 1 To \$aDays[0] Step 2; Loop through the array returned by StringSplit to display the individual values.

MsgBox(\$MB_SYSTEMMODAL, "", "ΑΠΟΣΤΑΣΗ ΠΟΥ ΘΕΛΟΥΜΕ ΑΠΟ "&\$firstnum&" (+ "&\$final2&" ΤΣΕΠΕΣ)")

;;      \$bet1= StringLeft(\$sString, \$final2*2) ; Retrieve 5 characters from the left of the string.

;;      MsgBox(\$MB_SYSTEMMODAL, "ΠΟΝΤΑΡΙΣΜΑ ΣΕ", \$bet1)

endif
endif

Next
EndFunc   ;==>Example

FileDelete("one.txt")

Func CountBetween(Const \$sText, Const \$sFirst, Const \$sSecond, Const \$sDelim = ",")
; Get the starting position of the first number
Local \$iStart = StartingPosition(\$sText, \$sFirst, \$sDelim)
; Error getting the first number, set error to 1
If (@Error) Then Return SetError(1, 0, "")
Local \$iEnd = StartingPosition(\$sText, \$sSecond, \$sDelim)
; Error getting the second number, set error to 2
If (@Error) Then Return SetError(2, 0, "")
; Do a RegExpReplace because it will store the number of times it replaces the delimiter in the @Extended macro
StringRegExpReplace(StringMid(\$sText, \$iStart, Abs(\$iStart - \$iEnd)), \$sDelim, "")
; Return the count
; Subtract one to account for the extra delimiter
; There's only 1 number between 5 and 45, but there are two commas (one on each side)
Return @Extended - 1
EndFunc

Func StartingPosition(Const \$sText, Const \$sSearch, Const \$sDelim)
; Need the length of the delimiter
Local \$iDelimLength = StringLen(\$sDelim)
; Split the string, use the entire delimiter and don't have the count in the [0] element
Local \$aSplit = StringSplit(\$sText, \$sDelim, \$STR_ENTIRESPLIT + \$STR_NOCOUNT)
Local \$iPosition = 1

For \$i = 0 to UBound(\$aSplit) - 1
; If this is the search, return the starting position of it
If (\$aSplit[\$i] = \$sSearch) Then Return \$iPosition
; Increment position by the length of the string + the length of the delimiter
\$iPosition += StringLen(\$aSplit[\$i]) + \$iDelimLength
Next
; Couldn't find it
Return SetError(1, 0, 0)
EndFunc```

if you run it first it will suggest that the group 5,28 is the correct one but the distance between them is wrong they are NOT 4 pockets away like the software says they are 13 !!

On the other hand the 4,11 group gives the correct "distance" from 11 to 4  (if you count it ccw but i want it to count clockwise) ( those x2 numbers are 10 pockets away)

The error i think is due to the first string with the whole list of numbers iot styarts from zero ends in 26 and it doesn't do word wrap when searching and reaching the end (the 26) it should somehow when it searches from 35 to 32 for example to count in this direction ==>  and count 3,26 THEN when it reaches the end word wrap to zero and then 32 and count this distance.

In a few words to always count clockwise how do we do this ? After that im done

I want to search in this string

Global \$sString = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26"

always cw and word-wrap while searching till i reach the second number im searching for. from 12 to 32 is  5 pockets away but as it works now it will say 3 pockets as it will count till the 26 and stop, it wont go back to beginning

cheers

Edited by asiawatcher

##### Share on other sites
4 hours ago, asiawatcher said:

I want to search in this string

Global \$sString = "0,32,15,19,4,21,2,25,17,34,6,27,13,36,11,30,8,23,10,5,24,16,33,1,20,14,31,9,22,18,29,7,28,12,35,3,26"

always cw and word-wrap while searching till i reach the second number im searching for. from 12 to 32 is  5 pockets away but as it works now it will say 3 pockets as it will count till the 26 and stop, it wont go back to beginning

That part is trivial:

```Local \$aSequence = [0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26]
Local \$aSamples = [34, 17, 5, 2, 30, 14, 0, 9]

Local \$iIndex1, \$iIndex2, \$iDistance
For \$i = 0 To (Floor(UBound(\$aSamples) / 2) * 2) - 1 Step 2     ; deal with odd number of samples
\$iIndex1 = _ArraySearch(\$aSequence, \$aSamples[\$i])          ; find the index of the first number
\$iIndex2 = _ArraySearch(\$aSequence, \$aSamples[\$i + 1])      ; find the index of the second number
\$iDistance = \$iIndex2 - \$iIndex1
If \$iDistance < 0 Then \$iDistance += UBound(\$aSequence)
ConsoleWrite(\$aSamples[\$i + 1] & " is " & \$iDistance & " away (clockwise) from " & \$aSamples[\$i] & @LF)
Next```

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Thanks, while trying to make aSamples variable dynamic like user should input the numbers via inputbox the script doesnt display anything why ?

Global \$aSamples = InputBox("ΑΡΙΘΜΟΙ ΡΟΥΛΕΤΑΣ", "ΒΑΛΤΕ ΤΟΥΣ ΑΡΙΘΜΟΥΣ ΧΩΡΙΣΜΕΝΟΥΣ ΜΕ ΚΟΜΜΑ", "0,6,36,4,25,22,1,35,9,5,28,3,29,3,20,25,18,31,8,4,11", "")

same if i try to enter the numbers in as file and read the file load it on Global \$aSamples then proceed why ?

cheers

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...