Sign in to follow this  
Followers 0
nitekram

Array and finding consecutive numbers

18 posts in this topic

Can someone help me convert this code into autoit?

$x = 2;

$tarray = array(1,3,4,5,10,15,16,17,18,20,21,22,23,24);


foreach($tarray as $value) {

      if(($value - $previous ) < 2){

             $temp[] = $value;
            
            if(count($temp)>= $x) {
                                    
                  $return[] = $temp;
                  
                  $temp = array();
                  $temp[] = $value;
            
            }
      
      }else{
      
            $temp = array();
            $temp[] = $value;     
            
      }    
      
      $previous = $value;

}



print_r($return);

thanks


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites



how about something different

I want to be able to pull out from a set of numbers a total of 5 consectutive numbers i.e. numbers are 1,4,5,6,7,8,10

I want to return 4,5,6,7,8,

can someone help?


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Looks like you want to use arraytrim

Edit: also please allow 24 hours before bumping your post. You've been around here long enough to know that.

Edited by youknowwho4eva

Giggity

Share this post


Link to post
Share on other sites

Try something like :

const $tab[12]=[2,13,3,4,6,7,8,10,3,4,5,6]
$l=0
$m=1
$t1=$tab[0]
#length of serie - 1
$nb=2    
#return if not found
$id=-1  
for $i=1 to ubound($tab)-1
    $m=$m*abs($tab[$i]-$t1)
    if $m=1 then 
        If $l==$nb Then
            $id=$i-$nb
            ExitLoop
        EndIf
        $l=$l+1
    Else
        $m=1
        $l=0
    EndIf
        $t1=$tab[$i]
Next    
ConsoleWrite("The index begining the serie is "&$id)

Share this post


Link to post
Share on other sites

Try something like :

const $tab[12]=[2,13,3,4,6,7,8,10,3,4,5,6]
$l=0
$m=1
$t1=$tab[0]
#length of serie - 1
$nb=2    
#return if not found
$id=-1  
for $i=1 to ubound($tab)-1
    $m=$m*abs($tab[$i]-$t1)
    if $m=1 then 
        If $l==$nb Then
            $id=$i-$nb
            ExitLoop
        EndIf
        $l=$l+1
    Else
        $m=1
        $l=0
    EndIf
        $t1=$tab[$i]
Next    
ConsoleWrite("The index begining the serie is "&$id)
This code returns 9 - i need it to return the 5 numbers that are consecutive, or at least the first number in the series

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This code returns 9 - i need it to return the 5 numbers that are consecutive, or at least the first number in the series

Interesting exercise...

#include <Array.au3>

Global $tarray[14] = [1, 3, 4, 5, 10, 15, 16, 17, 18, 20, 21, 22, 23, 24]
Global $value, $previous = $tarray[0], $temp[1] = [0], $return[1] = [0]

For $n = 0 To UBound($tarray) - 1
    $value = $tarray[$n]
    If ($value - $previous) = 1 Then
        _ArrayAdd($temp, $value)
        If UBound($temp) > UBound($return) Then
            $return = $temp
        EndIf
    Else
        Dim $temp[2] = [1, $value]
    EndIf
    $previous = $value
Next
$return[0] = UBound($return) - 1
_ArrayDisplay($return, "$return")

:mellow:

Edit: I put the 24 vice 25 back at the end of the string of numbers provided by the OP. The code returns the longest consecutive string found (should be 20, 21, 22, 23, 24), and if there are multiple strings of the same length returns the first string of that length. I was testing that by changing 24 to 25 and forgot to put it back when posting (returns 15, 16, 17, 18).

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

#include <array.au3>
Global $tarray[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Local $a_ret = _my_array_func($tarray)
_ArrayDisplay($a_ret)

Func _my_array_func($a_array, $i_base = 0)
    Local $i_count = 0, $i_ub = UBound($a_array) - 1
    Local $a_temp[$i_ub + 1][5]
    For $i = $i_base To $i_ub
        If $i + 4 > $i_ub Then ExitLoop
        If $a_array[$i] + 1 = $a_array[$i + 1] And _
            $a_array[$i] + 2 = $a_array[$i + 2] And _
            $a_array[$i] + 3 = $a_array[$i + 3] And _
            $a_array[$i] + 4 = $a_array[$i + 4] Then
            $i_count += 1
            $a_temp[$i_count][0] = $a_array[$i]
            $a_temp[$i_count][1] = $a_array[$i + 1]
            $a_temp[$i_count][2] = $a_array[$i + 2]
            $a_temp[$i_count][3] = $a_array[$i + 3]
            $a_temp[$i_count][4] = $a_array[$i + 4]
        EndIf           
    Next
    If Not $i_count Then Return SetError(1, 0, 0)
    ReDim $a_temp[$i_count + 1][5]
    $a_temp[0][0] = $i_count
    Return $a_temp
EndFunc

Edit:

This one should be better, you can choose how many you want consecutive:

#include <array.au3>
Global $tarray[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Local $a_ret = _my_array_func($tarray)
_ArrayDisplay($a_ret)

Func _my_array_func($a_array, $i_consec = 5, $i_base = 0)
    Local $i_count = 0, $i_ub = UBound($a_array) - 1
    Local $a_temp[$i_ub + 1][$i_consec], $i_equal = 0
    For $i = $i_base To $i_ub
        $i_equal = 0
        If $i + $i_consec - 1 > $i_ub Then ExitLoop
        For $n = 1 To $i_consec - 1
            If $a_array[$i] + $n <> $a_array[$i + $n] Then ExitLoop
            $i_equal += 1
        Next
        If $i_equal = $i_consec - 1 Then
            $i_count += 1
            For $n = 0 To $i_consec - 1
                $a_temp[$i_count][$n] = $a_array[$i + $n]
            Next
        EndIf       
    Next
    If Not $i_count Then Return SetError(1, 0, 0)
    ReDim $a_temp[$i_count + 1][$i_consec]
    $a_temp[0][0] = $i_count
    Return $a_temp
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#include <array.au3>
Global $tarray[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Local $a_ret = _my_array_func($tarray)
_ArrayDisplay($a_ret)

Func _my_array_func($a_array, $i_base = 0)
    Local $i_count = 0, $i_ub = UBound($a_array) - 1
    Local $a_temp[$i_ub + 1][5]
    For $i = $i_base To $i_ub
        If $i + 4 > $i_ub Then ExitLoop
        If $a_array[$i] + 1 = $a_array[$i + 1] And _
            $a_array[$i] + 2 = $a_array[$i + 2] And _
            $a_array[$i] + 3 = $a_array[$i + 3] And _
            $a_array[$i] + 4 = $a_array[$i + 4] Then
            $i_count += 1
            $a_temp[$i_count][0] = $a_array[$i]
            $a_temp[$i_count][1] = $a_array[$i + 1]
            $a_temp[$i_count][2] = $a_array[$i + 2]
            $a_temp[$i_count][3] = $a_array[$i + 3]
            $a_temp[$i_count][4] = $a_array[$i + 4]
        EndIf           
    Next
    If Not $i_count Then Return SetError(1, 0, 0)
    ReDim $a_temp[$i_count + 1][5]
    $a_temp[0][0] = $i_count
    Return $a_temp
EndFunc

Edit:

This one should be better, you can choose how many you want consecutive:

#include <array.au3>
Global $tarray[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Local $a_ret = _my_array_func($tarray)
_ArrayDisplay($a_ret)

Func _my_array_func($a_array, $i_consec = 5, $i_base = 0)
    Local $i_count = 0, $i_ub = UBound($a_array) - 1
    Local $a_temp[$i_ub + 1][$i_consec], $i_equal = 0
    For $i = $i_base To $i_ub
        $i_equal = 0
        If $i + $i_consec - 1 > $i_ub Then ExitLoop
        For $n = 1 To $i_consec - 1
            If $a_array[$i] + $n <> $a_array[$i + $n] Then ExitLoop
            $i_equal += 1
        Next
        If $i_equal = $i_consec - 1 Then
            $i_count += 1
            For $n = 0 To $i_consec - 1
                $a_temp[$i_count][$n] = $a_array[$i + $n]
            Next
        EndIf       
    Next
    If Not $i_count Then Return SetError(1, 0, 0)
    ReDim $a_temp[$i_count + 1][$i_consec]
    $a_temp[0][0] = $i_count
    Return $a_temp
EndFunc
That's pretty cool. I like the 2D array for multiple matches. But I thought the OP's use of "$x = 2" indicated he wanted to consider all consecutive data points. Does the original C code in post #1 produce a 2D array?

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Smart code but rather inefficient.

You go through table $i_consec times . Once would be enough.

Once you have found two consecutive elements whose difference is not 1 it's useless to come back to $i+1. You can progress from the point you are in order to find a sequence. Hence my code. Perheaps efficiency is no longer a challenge..

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Smart code but rather inefficient.

You go through table $i_consec times . Once would be enough.

Once you have found two consecutive elements whose difference is not 1 it's useless to come back to $i+1. You can progress from the point you are in order to find a sequence. Hence my code. Perheaps efficiency is no longer a challenge..

Nope ... you're right.

Edit:

Actually ... Now that I look at it, I don't think you are right. The first $n For/Next loop is validating the consecutive numbers, the second simply puts them in the return array. Without doing it the way I have it .... You'd have to hard code it like I did in the first example.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@SmOkeN

What I meant is you have two nested for loops (for $i / for $n) but one would have been sufficient.

Nevertheless, one should go and look at the code produced to know about efficiency that's sure.

@nitekram

My code returns 9 but if you use $tab[$id] you would get the first terme of the serie

Regards

Edited by happyuser

Share this post


Link to post
Share on other sites

@SmOkeN

What I meant is you have two nested for loops (for $i / for $n) but one would have been sufficient.

Nevertheless, one should go and look at the code produced to know about efficiency that's sure.

@nitekram

My code returns 9 but if you use $tab[$id] you would get the first terme of the serie

Regards

I don't want to sound like a prick here, but... I'd like to see how you'd write that with just 1 loop and it still be dynamic.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

That's pretty cool. I like the 2D array for multiple matches. But I thought the OP's use of "$x = 2" indicated he wanted to consider all consecutive data points. Does the original C code in post #1 produce a 2D array?

:)

That's C code? Looks more like php to me. And I have no idea what he's doing with it personally.

Edit:

Looks more like he is using the 2 as a base ... I didn't really bother to look to close, I only read his reply about:

This code returns 9 - i need it to return the 5 numbers that are consecutive, or at least the first number in the series

Guess that threw me off. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

@SmOke_N

The code is some posts above. Here is a revised version.

const $tab[23]=[2,13,3,4,6,7,8,10,3,4,5,6,9,1,2,3,5,6,7,8,9,9,11]
$l=0
$m=1
$t1=$tab[0]
#length of earched serie 
$nb=5   
#return if not found
$id=-1  
for $i=1 to ubound($tab)-1
    $m=$m*abs($tab[$i]-$t1)
    if $m=1 then
        If $l==$nb-2 Then
            $id=$i-$nb+1
            ExitLoop
        EndIf
        $l=$l+1
    Else
        $m=1
        $l=0
    EndIf
        $t1=$tab[$i]
Next    
ConsoleWrite(@crlf&"The index begining the serie is "&$id)
if $id>=0 Then
    ConsoleWrite(@crlf&"The first element is "&$tab[$id]&@CRLF)
EndIf

After a closer look at pretended C code, I think that it does not do what it is supposed to. It should consider OK the following sequence :

(12,10,3,2,3) because differences between two consecutive elements are always less than 2!

Share this post


Link to post
Share on other sites

After a closer look at pretended C code, I think that it does not do what it is supposed to. It should consider OK the following sequence :

(12,10,3,2,3) because differences between two consecutive elements are always less than 2!

Perhaps he assumes _ArraySort(), or the function that generates the source array always adds incrementing values.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I don't want to sound like a prick here, but... I'd like to see how you'd write that with just 1 loop and it still be dynamic.

Like this perhaps?

#include <array.au3>
Global $array[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Global $results[1]

$ConsecutiveNumbers = 5 ;number of consecutive integers

$iCN = $ConsecutiveNumbers - 1
For $i = 0 to UBound($array) - $iCN - 2
    If ($array[$i + $iCN] - $array[$i]) = $iCN Then _ArrayAdd($results,_ArrayToString($array,",",$i,$i + $iCN))
Next
_ArrayDisplay($results)

:)


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites

Thanks for everyones help!


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

Like this perhaps?

#include <array.au3>
Global $array[15] = [1, 3, 4, 5, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25]
Global $results[1]

$ConsecutiveNumbers = 5 ;number of consecutive integers

$iCN = $ConsecutiveNumbers - 1
For $i = 0 to UBound($array) - $iCN - 2
    If ($array[$i + $iCN] - $array[$i]) = $iCN Then _ArrayAdd($results,_ArrayToString($array,",",$i,$i + $iCN))
Next
_ArrayDisplay($results)

:)

That's not 1 loop. Both _ArrayAdd() and _ArrayToString() are looping there (and ReDim often).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
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
Sign in to follow this  
Followers 0