Jump to content

Array search ROW not Column?


Recommended Posts

How do I go about searching all columns in one row of a 2d array? For example I want to see if the number 3 is in $array[1], and I only want to search $array[1][0] through $array[1][4].

This is part of the array I am dealing with. It is a large array (9504 elements with 7 sub elements(the last one is currently empty)) that lists all the combinations of the numbers 1 through 12 in a set of five, plus all the combinations of those sets with an extra number between 1 and 12.

[0]|1|2|3|4|5|1|

[1]|1|2|3|4|5|2|

[2]|1|2|3|4|5|3|

[3]|1|2|3|4|5|4|

[4]|1|2|3|4|5|5|

[5]|1|2|3|4|5|6|

[6]|1|2|3|4|5|7|

[7]|1|2|3|4|5|8|

[8]|1|2|3|4|5|9|

[9]|1|2|3|4|5|10|

[10]|1|2|3|4|5|11|

[11]|1|2|3|4|5|12|

[12]|1|2|3|4|6|1|

[13]|1|2|3|4|6|2|

[14]|1|2|3|4|6|3|

[15]|1|2|3|4|6|4|

[16]|1|2|3|4|6|5|

[17]|1|2|3|4|6|6|

[18]|1|2|3|4|6|7|

[19]|1|2|3|4|6|8|

[20]|1|2|3|4|6|9|

[21]|1|2|3|4|6|10|

[22]|1|2|3|4|6|11|

[23]|1|2|3|4|6|12|

I have tried array search but I either get an error code 6 (no match) or it says the array variable has incorrect number of subscripts or something.

I have tried it these two ways: ($complete is the array)

$value = 6
$result = _ArraySearch($complete[1], $value, 0, 4)
$err = @error
ConsoleWrite($result&" --- "&$err)

This results in an error about the array variable having incorrect number of subscripts.

$value = 6
   $result = _ArraySearch($complete, $value, 0, 4)
   $err = @error
   ConsoleWrite($result&"   ---   "&$err)

This results in error code 6, no match. I assumed this wouldn't work because it seems to imply searching the whole array (but then still it SHOULD find something).

I thought the first one would work, but it doesn't seem to.

While writing this script I have run into the "array variable has incorrect number of subscripts" dozens of times in different places. I havn't used autoit in awhile, I dont remember having to declare the size of an array, or getting this error ever! Has something changed?

Side note: Part of my script requires that I do not know how big the array is going to be, and I need to know exactly how many rows there are when the function is done using the array, and not include any empty rows. How do I do this? ReDim every time? What if I need to redim 100 million times (likely)? There has got to be a better way.

Link to comment
Share on other sites

  • Moderators

How do I go about searching all columns in one row of a 2d array? For example I want to see if the number 3 is in $array[1], and I only want to search $array[1][0] through $array[1][4].

If you know the number of columns, how about:

For $i = 1 to 4
   Msgbox(0, "", $complete[1][$i])
Next

...or am I misunderstanding your intent?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

If you know the number of columns, how about:

For $i = 1 to 4
Msgbox(0, "", $complete[1][$i])
Next

...or am I misunderstanding your intent?

Your code does not search for anything it just displays what is in each column.

I need to search a ROW and output a result as to weather it was found in one of the columns of that row. Yes I could visually check each column, but that would require 47,520 message boxes (in this situation, millions in others) and defeats the purpose of this program.

I do not want to search with my eyes, I want to program to do the searching and output a result to a variable, and continue on with the rest of the script.

There are 9504 rows, each containing 7 cells. The first 5 cells have a number between one and twelve. The sixth cell also has a number between one and twelve but refers to something different. The 7th cell is an empty space waiting to be filled with search results. The program is going to pick 3 random numbers between 1 and 12, and then it will check the first 5 columns of each row to see if all of those 3 numbers are present in that row. If it fails it will move down to the next row and search for the same 3 numbers. When it finds all 3 numbers it will then search the 6th column of that row to find a separate randomly chosen number between one and 12, if the search is successful it will add 1 to the 7th column of that row and then move down to the next row and start over. When it is done searching the last row it will pick a new set of random numbers, 3 normal and 1 extra between one and 12. After which it will start over at the top row and repeat. It will do this millions of times. Then when it is finished it will sort the array by the 7th column.

Link to comment
Share on other sites

Skybotgaming,

$result = _ArraySearch($complete[1], $value, 0, 4)
this errors because $complete[1] is not an array, rather, it is an element of an array.

This

$result = _ArraySearch($complete, $value, 0, 4)
is probably not working because you are restricting the search to the 1ST 5 rows of the array (untested).

Is this

This results in error code 6, no match. I assumed this wouldn't work because it seems to imply searching the whole array (but then still it SHOULD find something).

not what you wanted to do?

kylomas

edit: additional question

This

How do I go about searching all columns in one row of a 2d array? For example I want to see if the number 3 is in $array[1], and I only want to search $array[1][0] through $array[1][4].

is conradictory. Do you want to search all columns or a subset of columns? Edited by 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

Link to comment
Share on other sites

Skybotgaming,

$result = _ArraySearch($complete[1], $value, 0, 4)
this errors because $complete[1] is not an array, rather, it is an element of an array.

This

$result = _ArraySearch($complete, $value, 0, 4)
is probably not working because you are restricting the search to the 1ST 5 rows of the array (untested).

Is this not what you wanted to do?

kylomas

edit: additional question

This is conradictory. Do you want to search all columns or a subset of columns?

actually its not contradictory. A column is a space in a row. By saying I want to search all columns in a row I am saying I want to search every space (element) in that row. I think it sounds contradictory to you because you are assuming a column must span across multiple rows. A column is one single space in a row... for example this simple spreadsheet:

1-2-3-4-5

If I was to search for the number 3 in "all columns in one row" I would find it in the third column of the only row.

In anycase I figured out how to do what I wanted with a series of if/elseif statements. Not as clean as I would like it to be but this is what I got (ignore the junk boxes).

#include <Array.au3>
#include <RandUnique.au3>
Global $picks[12]=[01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12]
Global $extraball[12]=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Global $fullwheel = _ArrayCombinations($Picks, 5, ",")
Global $splitted[792][7]

_ArrayDelete($fullwheel, 0)
;_ArrayDisplay($fullwheel, "iSet = " & 5)
for $i = 0 to UBound($splitted) - 1
if $i > 788 Then
;ConsoleWrite(" $I equals: " & $i & " COMBO NUMBER " & $i+1)
endif
$temp = StringSplit($fullwheel[$i], ",")
$splitted[$i][0] = $temp[1]
$splitted[$i][1] = $temp[2]
$splitted[$i][2] = $temp[3]
$splitted[$i][3] = $temp[4]
$splitted[$i][4] = $temp[5]

next

_arraydisplay($splitted,"splitted")
Global $complete[99999][7]
$k = 0
$j = 0
$i = 0
do
if $k = 11 then
; ConsoleWrite("COMBO " & $j+1 & " TOTAL " & $i+1 & " POWER " & $k+1 & " --- ")
endif
$complete[$i][0] = $splitted[$j][0]
$complete[$i][1] = $splitted[$j][1]
$complete[$i][2] = $splitted[$j][2]
$complete[$i][3] = $splitted[$j][3]
$complete[$i][4] = $splitted[$j][4]
$complete[$i][5] = $extraball[$k]
If $k >= 11 Then
$j=$j+1
$k=0
Else
$k=$k+1
EndIf
$i = $i+1
until $j = UBound($splitted)
_ArrayDisplay($complete, "Complete Wheel")
$matched = 0
$draw = _RandomUnique(3, 1, 12, 1)
$powerball = _RandomUnique(1,1,12,1)
_arraydelete($draw,0)
_arraydelete($powerball,0)
_arraydisplay($draw)
_arraydisplay($powerball)
for $i = 0 to UBound($complete)-1
if $complete[$i][0] = $draw[0] Then
$first_draw = 1
ElseIf $complete[$i][1] = $draw[0] then
$first_draw = 1
elseif $complete[$i][2] = $draw[0] then
$first_draw = 1
elseif $complete[$i][3] = $draw[0] then
$first_draw = 1
elseif $complete[$i][4] = $draw[0] then
$first_draw = 1
Else
;MsgBox(0,"","No Match for "&$draw[0]&" in first draw.")
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
EndIf

if $first_draw = 1 Then
;MsgBox(0,"","Found Match for "&$draw[0]&" in first draw.")
if $complete[$i][0] = $draw[1] Then
$second_draw = 1
ElseIf $complete[$i][1] = $draw[1] then
$second_draw = 1
elseif $complete[$i][2] = $draw[1] then
$second_draw = 1
elseif $complete[$i][3] = $draw[1] then
$second_draw = 1
elseif $complete[$i][4] = $draw[1] then
$second_draw = 1
Else
;MsgBox(0,"","No Match for "&$draw[1]&" in second draw.")
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
EndIf

Else
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
EndIf

if $first_draw = 1 and $second_draw = 1 Then
;MsgBox(0,"","Found Match for "&$draw[1]&" in second draw.")
if $complete[$i][0] = $draw[2] Then
$third_draw = 1
ElseIf $complete[$i][1] = $draw[2] then
$third_draw = 1
elseif $complete[$i][2] = $draw[2] then
$third_draw = 1
elseif $complete[$i][3] = $draw[2] then
$third_draw = 1
elseif $complete[$i][4] = $draw[2] then
$third_draw = 1
Else
;MsgBox(0,"","No Match for "&$draw[2]&" in third draw.")
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
;Next
EndIf

Else
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
;Next
EndIf

if $first_draw = 1 and $second_draw = 1 and $third_draw = 1 Then
; MsgBox(0,"","Found Match for "&$draw[2]&" in third draw.")
if $complete[$i][5] = $powerball[0] Then
$power_draw = 1
; ConsoleWrite(" Success combo number "&$i+1)
$matched = $matched + 1
$complete[$i][6] = $complete[$i][6] + 1
Else
;MsgBox(0,"","No Match for "&$powerball[0]&" in powerball.")
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
EndIf

Else
$first_draw = 0
$second_draw = 0
$third_draw = 0
$power_draw = 0
;Next
EndIf
Next
;_arraydisplay($complete)
ConsoleWrite(" ------------------- TOTAL MATCHED : "&$matched&" ------- ")

There is still a ton of junk in there, this is just a brute force/rough draft for my project.

In case your wondering, this is for creating and analyzing lottery wheels.

Amazing fact: If you bought 9504 lottery tickets composing all the combinations of 12 regular numbers and 12 powerball numbers, and if when the drawing occured they drew 3 of the 12 regular numbers you picked plus one of the power ball numbers you picked, you would only get 36 tickets with a match3+powerball. If you where playing megamillions you would "win" $5,400 but you would actually be out $4,104 -------- NOTE this is only including tickets that matched 3+1, you would still have many tickets that would match 2+1 and 1+1, I just havent put it in the script yet, but I assume you would be ahead.

Edited by SkybotGaming
Link to comment
Share on other sites

skybotgaming,

Not to be arbitrary, but, (isn't there alway a butt?) $complete has seven columns, you are searching the 1st four, hence the contradiction.

A cursory glance at your code suggests to me that you may want to review/learn about case constructs. They are in the Help file. Also, some example code for searching arrays.

#include <array.au3>

local $array[10000][7], $ret, $num = 1

; generate array of consecutive numbers from 1 to 70000
; 7 numbers per row

for $1 = 0 to ubound($array,1) - 1
    for $2 = 0 to ubound($array,2) - 1
        $array[$1][$2] = $num
        $num += 1
    Next
Next

; find 10 random numbers and display what row they were found in
; The entire array is searched

for $1 = 1 to 10
    $st = timerinit()
    $num = random(1,90000,1)
    $ret = _arraysearch($array,$num)
    if $ret > 0 then
        consolewrite('  ' & stringformat('%06i%-20s%010i%-15s%.3f',$num,' found at offset = ',$ret,' loop time = ',round(timerdiff($st)/1000,3)) & @lf)
    Else
        consolewrite('  ' & stringformat('%06i%-20s%010i%-15s%.3f',$num,' not found',$ret,' loop time = ',round(timerdiff($st)/1000,3)) & @lf)
    endif
next

; find 10 random numbers in the 1st 4 columns of an array

local $found = false

for $1 = 1 to 10
    $found = False
    $st = timerinit()
    $num = random(1,90000,1)
    for $2 = 0 to ubound($array)- 1
        for $3 = 0 to 3                 ; search 1st 4 cols only
            if $array[$2][$3] = $num then
                consolewrite('! ' & stringformat('%06i%-20s%10s%-15s%.3f',$num,' found at offset = ', '[' & $2 & ',' & $1 & ']',' loop time = ',round(timerdiff($st)/1000,3)) & @lf)
                $found = true
                ExitLoop 2
            EndIf
        Next
    Next
    if not $found then consolewrite('! ' & stringformat('%06i%-20s%10s%-15s%.3f',$num,' not found ',' ',' loop time = ',round(timerdiff($st)/1000,3)) & @lf)
next

The last peice of crapware that I saw for lottery analysis was called "Lottolyzer", f.y.i.!

Good Luck,

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...