# Selecting array slots surrounding criteria

## Recommended Posts

Hi Everyone

It's been a while since I last posted as I haven't hit any problems for a while but now I have, I am redeveloping parts of my data entry software and need to find certain sets within an array and get the array slots each side or inbetween what I am looking for and send those values to a new array.

Currently I have an array[20]=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

As the data gets added I change the zero to a one, after 20 entries I now examine the array for the following possible combinations

101 (here I would like the array slot of the zero returned)

0110 (again I would want the zeros)

Working example after 20 entries

0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0

My new array should look like

1,4,10,15,18

The first part above looks like

0,0,1,1,0,1

This has both combinations I am after, hope this makes sense to someone, I am struggling to understand how to do it properly.

Thanks to all for looking and hopefully helping.

##### Share on other sites

have been doing such things often. one way is to turn the array into a string and let stringinstr do it, or you turn it into a binary, which needs a bit more knowledge about bit operations.

E.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

You could loop through your array and search for the 1s and 0s and where they are in the array and use that to determine which 0s you need to keep track of.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

sorry, needed some time for a sample

.

```Global \$array[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
\$string=""
For \$i=0 To 19
\$string&=\$array[\$i]
Next
\$j=1
Do
\$x=StringInStr(\$string,"101",0,\$j)
If \$x>0 Then MsgBox(0,"",\$x)
\$y=StringInStr(\$string,"0110",0,\$j)
If \$y>0 Then MsgBox(0,\$y-1,\$y+2)
\$j+=1
Until \$x=0 And \$y=0```

.

i leave it up to you to put it in an array again

E.

Edit or this:

.

```Global \$array[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
\$string=""
For \$i=0 To 19
\$string&=\$array[\$i]
Next
For \$i=1 To StringLen(\$string)
\$x=StringInStr(StringMid(\$string,\$i,3),"101")
If \$x>0 Then MsgBox(0,"",\$i)
\$y=StringInStr(StringMid(\$string,\$i,4),"0110")
If \$y>0 Then MsgBox(0,\$i-1,\$i+2)
Next```

.

this one gets the sequence right.

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

```#include <Array.au3>

Global \$array[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
\$string=""
\$newstring=","
For \$i=0 To 19
\$string&=\$array[\$i]
Next
For \$i=1 To StringLen(\$string)
\$x=StringInStr(StringMid(\$string,\$i,3),"101")
If \$x>0 And StringInStr(\$newstring,","&\$i&",")=0 Then \$newstring&=\$i&",";MsgBox(0,"",\$i)
\$y=StringInStr(StringMid(\$string,\$i,4),"0110")
If \$y>0 Then
If StringInStr(\$newstring,","&\$i-1&",")=0 Then \$newstring&=\$i-1&","
If StringInStr(\$newstring,","&\$i+2&",")=0 Then \$newstring&=\$i+2&","
EndIf;MsgBox(0,\$i-1,\$i+2)
Next
\$newarray=StringSplit(StringTrimLeft(StringTrimRight(\$newstring,1),1),",")
_ArrayDisplay(\$newarray)```

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

Thanks for the speedy replies, Edano, I will study what you have suggested, many thanks for your help.

##### Share on other sites

Thanks for the speedy replies, Edano, I will study what you have suggested, many thanks for your help.

.

if you consider to use my solutions, it would be easier to use a (I/0) string rather than an array from the beginning.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

Hi again

I have the code working perfect but have one little problem, as \$newarray is created on the fly how do I empty it so it can be re used, currently it is just growing each time, hope that makes sense.

After I get to my 20 additions of data I look for the 101 or 0110 get my results with Edanos' script then I want to add the next 20 sets of data and do it all again

##### Share on other sites

```#include <Array.au3>

Global \$array[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
While 1
\$string=""
\$newstring=","
For \$i=0 To 19
\$string&=\$array[\$i]
Next
For \$i=1 To StringLen(\$string)
\$x=StringInStr(StringMid(\$string,\$i,3),"101")
If \$x>0 And StringInStr(\$newstring,","&\$i&",")=0 Then \$newstring&=\$i&",";MsgBox(0,"",\$i)
\$y=StringInStr(StringMid(\$string,\$i,4),"0110")
If \$y>0 Then
If StringInStr(\$newstring,","&\$i-1&",")=0 Then \$newstring&=\$i-1&","
If StringInStr(\$newstring,","&\$i+2&",")=0 Then \$newstring&=\$i+2&","
EndIf;MsgBox(0,\$i-1,\$i+2)
Next
\$newarray=StringSplit(StringTrimLeft(StringTrimRight(\$newstring,1),1),",")
_ArrayDisplay(\$newarray)
WEnd```

.

?

or use it in a function

.

```#include <Array.au3>

Global \$Garray[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
_ArrayDisplay(_Func(\$Garray))

Func _Func(\$array)
Local \$string=""
Local \$newstring=","
For \$i=0 To 19
\$string&=\$array[\$i]
Next
For \$i=1 To StringLen(\$string)
Local \$x=StringInStr(StringMid(\$string,\$i,3),"101")
If \$x>0 And StringInStr(\$newstring,","&\$i&",")=0 Then \$newstring&=\$i&",";MsgBox(0,"",\$i)
Local \$y=StringInStr(StringMid(\$string,\$i,4),"0110")
If \$y>0 Then
If StringInStr(\$newstring,","&\$i-1&",")=0 Then \$newstring&=\$i-1&","
If StringInStr(\$newstring,","&\$i+2&",")=0 Then \$newstring&=\$i+2&","
EndIf;MsgBox(0,\$i-1,\$i+2)
Next
Return StringSplit(StringTrimLeft(StringTrimRight(\$newstring,1),1),",")
EndFunc```
Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

Yes using that code, after my first set if data I do this

Global \$array[20]=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Then start adding the next set of data, I then end up with a different set of values within the array so the 101 or 0110's will be in different locations, using your code the \$newarray just keeps growing as it still holds the previous set of results.

I can't ReDim it as I don't know the size it will be for the next set of data, any ideas

##### Share on other sites

Yes using that code, after my first set if data I do this

Global \$array[20]=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Then start adding the next set of data, I then end up with a different set of values within the array so the 101 or 0110's will be in different locations, using your code the \$newarray just keeps growing as it still holds the previous set of results.

I can't ReDim it as I don't know the size it will be for the next set of data, any ideas

.

no, it cannot grow. it is newly created every time by stringsplit(). maybe you should post your code.

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

ah, maybe you forgot to reset \$newstring

\$newstring=","

that is the only explanation i can imagine

and \$string="" as well

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

##### Share on other sites

Thanks, I had forgotten to reset \$string, so that was getting longer each time, thanks for your help

##### Share on other sites

@Edano

The func does not run as posted.  Slightly optimized/corrected version.

```#include <Array.au3>

Global \$Garray[20]=[0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,1,1,0,0]
local \$aRET = _Func(\$Garray)
_ArrayDisplay(\$aRET)

Func _Func(\$array)
Local \$string=_arraytostring(\$array,'') ; eliminates for loop and length dependency
Local \$newstring="", \$x, \$y             ; does not need to be initialized with a comma, don't declare vars in a loop
For \$i=1 To StringLen(\$string)
\$x=StringInStr(StringMid(\$string,\$i,3),"101")
If \$x>0 And StringInStr(\$newstring,","&\$i&",")=0 Then \$newstring&=\$i&",";MsgBox(0,"",\$i)
\$y=StringInStr(StringMid(\$string,\$i,4),"0110")
If \$y>0 Then
If StringInStr(\$newstring,","&\$i-1&",")=0 Then \$newstring&=\$i-1&","
If StringInStr(\$newstring,","&\$i+2&",")=0 Then \$newstring&=\$i+2&","
EndIf;MsgBox(0,\$i-1,\$i+2)
Next
Return StringSplit(StringTrimRight(\$newstring,1),",",2)     ;   removed stringtrimleft and return with no count at offset 0
EndFunc```

FWIW - 1 based version

```#include <array.au3>

local \$tstr  = '00110100010100001100'
local \$aRET  = _ret_zero_pos(stringsplit(\$tstr,'',3))

_arraydisplay(\$aRET,'Zero Positions')

func _ret_zero_pos(\$array)

local \$ostr, \$idx = 0, \$str = _arraytostring(\$array,'')

while stringlen(\$str) > 2

switch True
case stringregexp(\$str,'0110')
stringregexp(\$str,'0110',1)
if @extended > 0 then
\$idx += @extended
\$ostr &= \$idx - 4 & ',' & \$idx - 1 & ','
\$str = stringtrimleft(\$str,@extended)
continuecase
endif
case stringregexp(\$str,'101')
stringregexp(\$str,'101',1)
if @extended > 0 then
\$idx += @extended
\$ostr &= \$idx - 2 & ','
\$str = stringtrimleft(\$str,@extended)
endif
case Else
exit
endswitch

wend

return stringsplit(stringtrimright(\$ostr,1),',',3)

endfunc```

@Phaser - I agree with Edano, unless there is some dependency we are unaware of I would have used strings also.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

## Create an account

Register a new account