Sign in to follow this  
Followers 0
sbt

Array function algo

2 posts in this topic

Hi,

This is not that easy. So what I want to do is the following, i have an array from the first value i have to substract 1, the result should goe

ones in a new string and ones in a variable ($c). Then the script should check if the next value in the array is in sequential order. If it is

it should add 1 to $c; if the next value is not in sequential order it should substract $c the value and add to the new string separated by ",", and so on. Example:

$Array 3 4 5 6 9 10 12

$c 2 +1 +1 +1 +1

$newstring 2 , 4 (9 - 5), 6(12 -6)

The result shoult be $newstring = '2,4,6'. I tried to modify some code from others here but i failed. I'm always getting errors.

#include <Array.au3>
$Array = "3,4,5,6,9"
$Array = StringSplit($Array, ",")
_ArrayDisplay($Array) 

Global $c = $Array[1]-1  ; set $c to $Array[1]-1

For $i = 2 To $Array[0]                 
    If $Array[$i + 1] - $Array[$i] <> 1 Then  ; if the array is in sequential order adding 1 to $c 
         $c = $c + 1
        Else 
                 $newstring &= $Array[1]-1 & "," & $Array[$i]-$c  ; if not add to string subtracting $c 
        EndIf
Next
Return $string
_ArrayDisplay($string)

Thx in advance

sbt

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

There's a number of errors in your script that make it not run at all or not well coded, and at least one clear logic error (I think).

I don't follow your algorithm exactly from your explanation, especially this part: "if the next value is not in sequential order it should substract $c the value"

But if you use the following modification of your code, at least it runs more or less the way you programmed it now, and if you understand my "NEW" remarks, remove them and you can continue tweaking/debugging your algorithm itself but now with a working piece of code.

Good luck!

#include <Array.au3>
; NEW: $Array = "3,4,5,6,9" is fine, but it is bad style to put a string in a variable called $Array
; So: use a string and split that into an array
$inputstring = "3,4,5,6,9"
$Array = StringSplit($inputstring, ",")
_ArrayDisplay($Array)

$c = $Array[1] - 1 ; set $c to $Array[1]-1

;NEW: declace $newstring before use, takes care of one error
$newstring = ""

; NEW: For $i = 2 To $Array[0] won't work because you are looking for $Array[$i+1] in the if, this makes the last loop iteration look in a non-dimensioned element
; So, just go to $Array[0]-1
For $i = 2 To $Array[0] - 1
    ; NEW: <> means NOT equal to (or rather, larger or smaller than), so if the difference is <> 1, it is NOT sequential!
    ; So: changed the <> to == in the following if statement
    If $Array[$i + 1] - $Array[$i] == 1 Then ; if the array is in sequential order adding 1 to $c
        $c = $c + 1
    Else
        $newstring &= $Array[1] - 1 & "," & $Array[$i] - $c ; if not add to string subtracting $c
    EndIf
Next

;NEW: Don't Return $string, this is what you would do if this were a function but it's not
;Return $string

;NEW: Don't _ArrayDisplay($string) because $string is not an array, $Array is your array! :)
;So, just display the $newstring in a messagebox (and the $c counter, since you seem to need that too! :) ):
MsgBox(64, "test", "$newstring: " & $newstring & @CRLF & "$c: " & $c)
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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