Jump to content

create new array, without / except values of an other one


sbt
 Share

Recommended Posts

And this version:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 441, 342, 192, 124)
$Input1 = GUICtrlCreateInput("7,8,13,14,15,20", 48, 32, 257, 21)
$Input2 = GUICtrlCreateInput("25", 48, 120, 257, 21)
$Button1 = GUICtrlCreateButton("Button1", 88, 192, 169, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $aArray1, $aArray2
While 1

    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button1
                $aArray1 = StringSplit(GUICtrlRead($Input1), ",", 2)
                $aArray2 = GUICtrlRead($Input2)
                MsgBox(0, "Test", ArrayExclude($aArray1, $aArray2))
    EndSelect

WEnd

Func ArrayExclude($aA1, $end = 25, $start = 1)
    Local $i, $j = 0
    Local $aA2[$end - $start + 1]
    For $i = $start To $end
        $aA2[$j] = $i
        $j += 1
    Next
    $j = 0
    For $i = 0 To UBound($aA1) - 1
        $aA1[$i] = Number($aA1[$i])
        While $j < UBound($aA2)
            $aA2[$j] = Number($aA2[$j])
            If $aA2[$j] = $aA1[$i] Then
                _ArrayDelete($aA2, $j)
                $j -= 2
            EndIf
            $j += 1
        WEnd
        $j = 0
    Next
    _ArraySort($aA2)
    $string = $aA2[0]
    For $i = 1 To UBound($aA2) - 1
        If $aA2[$i] - $aA2[$i -1] <> 1 Then $string &= "-" &  $aA2[$i - 1] & ","  & $aA2[$i]
    Next
     $string &= "-" & $aA2[$i - 1]
    Return $string
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

You probably want to read up a bit on arrays. They are incredibly usefull, but you need to know how to declare/copy and modify them. (You where declaring an array in an improper way)

I've fixed the script and modified the function to return a string as that seems to be what you are looking for.

I've also fixed a mistake of my own:( $aExclude[$i-1] - $iEnd instead of $iEnd - $aExclude[$i-1])

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 441, 342, 192, 124)
$Input1 = GUICtrlCreateInput("7,8,13,14,15,20", 48, 32, 257, 21)
$Input2 = GUICtrlCreateInput("25", 48, 120, 257, 21)
$Button1 = GUICtrlCreateButton("Button1", 88, 192, 169, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button1
                Global $aArray1 = StringSplit(GUICtrlRead($Input1), ",", 2)
                ;just use $aArray directly instead of copying to another one
                Global $sResult = _ArrayInvert($aArray1, 1, GUICtrlRead($Input2)) ;you had an additional bracket before GuiCtrlRead
                MsgBox(0,"Result",$sResult)
    EndSelect
WEnd

Func _ArrayInvert(Const ByRef $aExclude, $iStart, $iEnd)

    If Not IsArray($aExclude) Then Return SetError(1)
    If $iStart >= $iEnd Then Return SetError(2)
    If $iStart > $aExclude[0] Then Return SetError(3)
    If $iEnd < $aExclude[UBound($aExclude)-1] Then Return SetError(4)

    Local $sReturn
    For $i = 0 To UBound($aExclude)-1
        Switch $aExclude[$i] - $iStart
            Case 0
            Case 1
                $sReturn &= "," & $iStart
            Case Else
                $sReturn &= "," &  $iStart & "-" & $aExclude[$i]-1
        EndSwitch
        $iStart = $aExclude[$i]+1
    Next
    Switch $iEnd - $aExclude[$i-1]
        Case 0
        Case 1
            $sReturn &= "," &  $iEnd
        Case Else
            $sReturn &= "," &  $aExclude[$i-1]+1 & "-" & $iEnd
    EndSwitch
    Return StringTrimLeft($sReturn,1)
EndFunc

Edit: aww too slow.

Edited by Tvern
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...