Register    Login    Forum    FAQ    PinSimDB.org

Board index » Resources » Resources Exchange




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: FP Scripts 'C_Array' & 'C_Hash' v1.1
 Post Posted: Thu Feb 26, 2015 8:20 pm 
Offline
User avatar

Joined: Fri Sep 09, 2011 9:14 pm
Posts: 142
Hi again guys! Today I come with something a bit different.
As a script practice, I made a class which is like a very rudimentary Array template for FP, because VB handles Arrays but FP script doesn't support them properly.

These "C_Array" objects support an internally alterable max of 99 items (tested with Integers and Strings). The code also has some methods to make your life a bit easy or infinitely more complicated depending on the situation *laughs*

Code:
Code:
' *********************************************************************
' **                                                                 **
' **                      C_Array Class v1.1                              **
' **                                                                 **
' *********************************************************************

' ---------------------------------------------------------------------
'    This is a Script Implementation for FP that allows you to use a sort of simple Arrays,
'    to handle data mutually. You can use and modify it for your tables freely.
'    I'm not an experienced scripter, only an early learner.
'    So probably some things could be improved.
'
'  To generate an Array use this caller as a main example:

'Dim Array
'Set Array = New C_Array
'Array.index(0) = 0
'Array.index(1) = 12
'Array.index(2) = 0
'Array.index(3) = 0

'   This will generate this Array: [0, 12, 0, 0], and you can get any of its data using:

'a = Array.index(1) ' => 12

'   To display all the Array data on a string format ("[0, 12, 0, 0]") use:

'AddDebugText(Array.Print)

'   Other methods are described below.
'  Hope it will be useful for somebody!~

' ---------------------------------------------------------------------

Dim nil

Class C_Array
   Public index(99)       ' Call a index using Array.index(ID)
   Private MaxLength      ' Max Length allowed for a C_Array. Used internally.
   Private PClone         ' Private Clone of the C_Array. Used internally.

' ---------------------------------------------------------------------
' * Initialize
' ---------------------------------------------------------------------

   Private Sub Class_Initialize()
      MaxLength = 99
      Dim i : For i = 0 To MaxLength
         ' Assign 'nil' data to each index
         index(i) = nil
      Next
   End Sub

' ---------------------------------------------------------------------
' * Length
' ---------------------------------------------------------------------
'   Gets the Array Length
'    Examples:
'       ["Hello", "World"] => 2
'       [0, 1, nil, 3] => 4
' ---------------------------------------------------------------------

   Public Function Length()
      Length = 0
      Dim i : For i = 0 To MaxLength
         If TypeName(index(i)) <> "Empty" Then
            Length = i + 1
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * Count
' ---------------------------------------------------------------------
'   Gets the Array Length, considering the not 'nil' values
'    Examples:
'       ["Hello", "World"] => 2
'       [0, 1, nil, 3] => 3
' ---------------------------------------------------------------------

   Public Function Count()
      Count = 0
      Dim i : For i = 0 To (Length - 1)
         If TypeName(index(i)) <> "Empty" Then
            Count = Count + 1
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' **** TermCount
' ---------------------------------------------------------------------
'   Counts how many times the Array contains a term
'    Examples:
'       [0, 0, 1] -> TermCount(0) => 2
'       ["A", "B"] -> TermCount("C") => 0
' ---------------------------------------------------------------------

   Public Function TermCount(ByVal term)
      TermCount = 0
      Dim i : For i = 0 To (Length - 1)
         If TypeName(index(i)) = "String" Or TypeName(index(i)) = "Integer" Then
            If (index(i) = term) Then
               TermCount = TermCount + 1
            End If
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * First
' ---------------------------------------------------------------------
'   Gets the first index data.
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function First()
      First = index(0)
   End Function

' ---------------------------------------------------------------------
' * Last
' ---------------------------------------------------------------------
'   Gets the last not-'nil' index data
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function Last()
      Last = index(Length - 1)
   End Function

' ---------------------------------------------------------------------
' * IsEmpty
' ---------------------------------------------------------------------
'   The Array IsEmpty only if all its index equal 'nil'
' ---------------------------------------------------------------------

   Public Function IsEmpty()
      IsEmpty = (Count = 0)
   End Function

' ---------------------------------------------------------------------
' * Clear
' ---------------------------------------------------------------------
'   Clears the Array setting all its index to 'nil'
' ---------------------------------------------------------------------

   Public Sub Clear()
      Dim i : For i = 0 To MaxLength
         index(i) = nil
      Next
   End Sub

' ---------------------------------------------------------------------
' * Include
' ---------------------------------------------------------------------
'   Checks if the Array contains a term.
'   Example:
'   ["Future", "Pinball"] -> Include "Visual" => False
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function Include(ByVal term)
      Include = False
      Dim i : For i = 0 To (Length - 1)
         If (index(i) = term) Then
            Include = True
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * GetIndex
' ---------------------------------------------------------------------
'   Gets the (last) position of a term. If the Array doesn't include the term
'   it returns '-1'
'   Example:
'   ["Light1", "Light2", "SPLight"] -> GetIndex "SPLight" => 2
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function GetIndex(ByVal term)
      GetIndex = -1
      Dim i : For i = 0 To (Length - 1)
         If (index(i) = term) Then
            GetIndex = i
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * Print
' ---------------------------------------------------------------------
'   Print the Array on an easy readable format
' ---------------------------------------------------------------------

   Public Function Print()
      Print = "["
      Dim Sep
      Sep = False
      Dim i : For i = 0 To (Length - 1)
         Dim New_Text
         If TypeName(index(i)) = "C_Array" Or TypeName(index(i)) = "C_Hash" Then
            New_Text = index(i).Print
         Else
            New_Text = index(i)
         End If
         If (Sep = False) Then
            Print = Print & New_Text
            Sep = True
         Else
            Print = Print & "," & New_Text
         End If
      Next
      Print = Print & "]"
   End Function

' ---------------------------------------------------------------------
' * Clone
' ---------------------------------------------------------------------
'   Clones the current state of the array on PClone
' ---------------------------------------------------------------------

   Private Sub Clone()
      Set PClone = new C_Array
      Dim i : For i = 0 To (Length - 1)
         Select Case TypeName(index(i))
         Case "String", "Integer"
            PClone.index(i) = index(i)
         Case "C_Array"
            Set PClone.index(i) = New C_Array
            PClone.index(i).Replace index(i)
         Case "C_Hash"
            Set PClone.index(i) = New C_Hash
            PClone.index(i).Replace index(i)
         Case "Empty"
            PClone.index(i) = nil
         End Select
      Next
   End Sub

' ---------------------------------------------------------------------
' * Push
' ---------------------------------------------------------------------
'   Inserts a new term on the end of the Array.
'   Example:
'   [0, 2] -> Push(5) => [0, 2, 5]
' ---------------------------------------------------------------------

   Public Sub Push(ByVal term)
      Dim n
      If IsEmpty() Then
         n = 0
      Else
         n = Length
      End If
      Insert n, term
   End Sub

' ---------------------------------------------------------------------
' * Unshift
' ---------------------------------------------------------------------
'   Inserts a new term on the start of the Array.
'   Example:
'   [0, 2] -> Unshift(5) => [5, 0, 2]
' ---------------------------------------------------------------------

   Public Sub Unshift(ByVal term)
      Insert 0, term
   End Sub

' ---------------------------------------------------------------------
' * Insert
' ---------------------------------------------------------------------
'   Inserts a new term on the assigned position of the Array.
'   Examples:
'   [0, 2] -> Insert(1, 5) => [0, 5, 2]
'   [0, 2] -> Insert(5, 5) => [0, 2, nil, nil, nil, 5]
' ---------------------------------------------------------------------

   Public Sub Insert(ByVal pos, ByVal term)
      Clone()
      Dim i : For i = pos To (MaxLength - 1)
         Select Case TypeName(PClone.index(i))
         Case "String", "Integer"
            index(i+1) = PClone.index(i)
         Case "C_Array"
            Set index(i+1) = New C_Array
            index(i+1).Replace PClone.index(i)
         Case "C_Hash"
            Set index(i+1) = New C_Hash
            index(i+1).Replace PClone.index(i)
         Case "Empty"
            index(i+1) = nil
         End Select
      Next
      Select Case TypeName(term)
      Case "String", "Integer"
         index(pos) = term
      Case "C_Array"
         Set index(pos) = New C_Array
         index(pos).Replace(term)
      Case "C_Hash"
         Set index(pos) = New C_Hash
         index(pos).Replace(term)
      Case "Empty"
         index(pos) = nil
      End Select
   End Sub

' ---------------------------------------------------------------------
' * Delete
' ---------------------------------------------------------------------
'   Deletes a term across the Array
'   Example:
'   [0, 1, 1, 2] -> Delete(1) => [0, 2]
' ---------------------------------------------------------------------

   Public Sub Delete(ByVal term)
      Do While (Include(term) = True)
         DeleteAt(GetIndex(term))
      Loop
   End Sub

' ---------------------------------------------------------------------
' * DeleteAt
' ---------------------------------------------------------------------
'   Deletes the term of an assigned position
'   Example:
'   [0, 1, 1, 2] -> DeleteAt(1) => [0, 1, 2]
' ---------------------------------------------------------------------

   Public Sub DeleteAt(ByVal pos)
      Clone()
      Dim i : For i = pos To (Length - 1)
         Select Case TypeName(PClone.index(i+1))
         Case "String", "Integer"
            index(i) = PClone.index(i+1)
         Case "C_Array"
            Set index(i) = New C_Array
            index(i).Replace PClone.index(i+1)
         Case "C_Hash"
            Set index(i) = New C_Hash
            index(i).Replace PClone.index(i+1)
         Case "Empty"
            index(i) = nil
         End Select
      Next
   End Sub

' ---------------------------------------------------------------------
' * Pop
' ---------------------------------------------------------------------
'   Deletes the last term of the Array and returns it
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function Pop()
      Pop = index(Length - 1)
      DeleteAt(Length - 1)
   End Function

' ---------------------------------------------------------------------
' * Shift
' ---------------------------------------------------------------------
'   Deletes the first term of the Array and returns it
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function Shift()
      Shift = index(0)
      DeleteAt(0)
   End Function

' ---------------------------------------------------------------------
' * Compact
' ---------------------------------------------------------------------
'   Deletes all the 'nil' values on the Array
'   Example:
'   [0, 2, nil, nil, 5, nil, 4] => [0, 2, 5, 4]
' ---------------------------------------------------------------------

   Public Sub Compact()
      Dim n
      n = 0
      Dim i : For i = 0 To (Length - 1)
         If TypeName(index(n)) = "Empty" Then
            DeleteAt(n)
         Else
            n = n + 1
         End If
      Next
   End Sub

' ---------------------------------------------------------------------
' * Uniq
' ---------------------------------------------------------------------
'   Unifies every value of the Array.
'   Example:
'   [0, 1, 1, 1, 3, 3] => [0, 1, 3]
' ---------------------------------------------------------------------

   Public Sub Uniq()
      Dim term
      Dim i : For i = (Length - 1) To 0 Step -1
         If TypeName(index(i)) = "String" Or TypeName(index(i)) = "Integer" Then
            term = index(i)
            If (TermCount(term) > 1) Then
               DeleteAt(i)
            End If
         End If
      Next
   End Sub

' ---------------------------------------------------------------------
' * Reverse
' ---------------------------------------------------------------------
'   Reverses the term order of the Array
'   Example:
'   ["Pinball", "Wizard"] => ["Wizard", "Pinball"]
' ---------------------------------------------------------------------

   Public Sub Reverse()
      Clone()
      Dim i : For i = 0 To (Length - 1)
         Select Case TypeName(PClone.index((Length - 1) - i))
         Case "String", "Integer"
            index(i) = PClone.index((Length - 1) - i)
         Case "C_Array"
            Set index(i) = New C_Array
            index(i).Replace PClone.index((Length - 1) - i)
         Case "C_Hash"
            Set index(i) = New C_Hash
            index(i).Replace PClone.index((Length - 1) - i)
         Case "Empty"
            index(i) = nil
         End Select
      Next
   End Sub

' ---------------------------------------------------------------------
' * RotateRight
' ---------------------------------------------------------------------
'   Rotates the order of the Array 1 place to the Right
'   Example:
'   [0, 1, 2, 3] => [3, 0, 1, 2]
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Sub RotateRight()
      Clone()
      Dim i : For i = 1 To (Length - 1)
         index(i) = PClone.index(i - 1)
      Next
      index(0) = PClone.index(Length - 1)
   End Sub

' ---------------------------------------------------------------------
' * RotateLeft
' ---------------------------------------------------------------------
'   Rotates the order of the Array 1 place to the Right
'   Example:
'   [0, 1, 2, 3] => [1, 2, 3, 0]
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Sub RotateLeft()
      Clone()
      Dim i : For i = 0 To ((Length - 1) - 1)
         index(i) = PClone.index(i + 1)
      Next
      index(Length - 1) = PClone.index(0)
   End Sub

' ---------------------------------------------------------------------
' * ToString
' ---------------------------------------------------------------------
'   Converts the Array to a String joining its components
'   (Only for String-based Arrays!)
'   Example:
'  ["Future", "Pinball"] => "FuturePinball"
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function ToString()
      ToString = ""
      Dim i : For i = 0 To (Length - 1)
         ToString = ToString & index(i)
      Next
   End Function

' ---------------------------------------------------------------------
' * ToStringWithSep
' ---------------------------------------------------------------------
'   Converts the Array to a String using a separator.
'   (Only for String-based Arrays!)
'   Examples:
'  ["Future", "Pinball"] -> ToStringWithSep(" ") =>  "Future Pinball"
'  ["A", "B", "C"] -> ToStringWithSep(", ") =>  "A, B, C"
'   WARNING! Only works with String and Integer based Arrays
' ---------------------------------------------------------------------

   Public Function ToStringWithSep(ByVal WSep)
      ToStringWithSep = ""
      Dim Sep
      Sep = False
      Dim i : For i = 0 To (Length - 1)
         If (Sep = False) Then
            ToStringWithSep = ToStringWithSep & index(i)
            Sep = True
         Else
            ToStringWithSep = ToStringWithSep & WSep & index(i)
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * Replace
' ---------------------------------------------------------------------
'   Replaces the entire Array to coincide to another one
'   Used as a Clone statement
' ---------------------------------------------------------------------

   Public Sub Replace(ByVal Array)
      Clear()
      Dim i : For i = 0 To (Array.Length - 1)
         index(i) = Array.index(i)
      Next
   End Sub
End Class


An example of usage:

Code:
' ' *********************************************************************
' **                                                                 **
' **                        Light Management                              **
' **                                                                 **
' *********************************************************************

Sub ResetLights(Byval Array, Byval State)
   Dim i
   For i = 0 To (Array.Length - 1)
      Eval(Array.index(i)).set State
   next
End Sub

Function CheckLights(Byval Array, Byval State)
   Dim i
   CheckLights = true
   For i = 0 to (Array.Length - 1)
      if eval(Array.index(i)).State <> State then
         CheckLights = false
         exit function
      end if
   next
End function

Sub RotateLights(Byval Array, Byval Direction)
   Dim NewArray
   Set NewArray = New C_Array
   NewArray.Replace(Array)
   Select Case Direction
      Case 0 : NewArray.RotateLeft()
      Case 1 : NewArray.RotateRight()
   End Select
   Dim i
   For i = 0 to (Array.Length - 1)
      Dim State
      State = Eval(NewArray.index(i)).State
      NewArray.index(i) = State
   Next
   For i = 0 to (Array.Length - 1)
      Eval(Array.index(i)).State = NewArray.index(i)
   Next
End Sub


Last edited by Wecoc on Wed Mar 04, 2015 7:58 pm, edited 3 times in total.

Top 
 Profile  
 
 Post subject: Re: FP Script 'C_Array' v1.0
 Post Posted: Thu Feb 26, 2015 10:17 pm 
Offline

Joined: Mon Jan 27, 2014 12:36 pm
Posts: 2133
Location: Ontario, Canada
you must be clairvoyant !

this is precisely what I want to learn.
I wll read this and get back to you thanks!

a string of 99 variable integers is what I
need!

of course I don't know how it works

I am considering a true multiplayer template
using variable integers in strings


Top 
 Profile  
 
 Post subject: Re: FP Script 'C_Array' v1.0
 Post Posted: Fri Feb 27, 2015 7:51 am 
Offline
User avatar

Joined: Fri Sep 09, 2011 9:14 pm
Posts: 142
Quote:
I am considering a true multiplayer template using variable integers in strings

You got my attention on this!


Last edited by Wecoc on Thu Apr 02, 2015 3:27 am, edited 1 time in total.

Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.0
 Post Posted: Fri Feb 27, 2015 7:25 pm 
Offline
User avatar

Joined: Fri Sep 09, 2011 9:14 pm
Posts: 142
Ok, finally I did all I planned to do.
As an alternative of usage I did also the C_Hash class, with the same methodology as Array, but with Hash specific methods and properties. C_Hash does not require C_Array but if you use it I recommend you to use also the Array, because Array is more used on other script lenguages than Hash.

Code:
' *********************************************************************
' **                                                                 **
' **                       C_Hash Class v1.1                              **
' **                                                                 **
' *********************************************************************

' ---------------------------------------------------------------------
'    This is a Script Implementation for FP that allows you to use a sort of simple Hashes,
'    to handle data mutually. You can use and modify it for your tables freely.
'    I'm not an experienced scripter, only an early learner.
'    So probably some things could be improved.
'
'  To generate a Hash use this caller as a main example:

'Dim Hash
'Set Hash = New C_Hash
'Hash.Create 0, "A"
'Hash.Create 1, "B"

'   This will generate this Hash: {0=>"A", 1=>"B"}, (Key=>Value) and you can get any of its data using:

'a = Hash.GetValue(0) ' => "A"
'b = Hash.GetKey("B") ' => 1

'   To display all the Hash data on a string format ("{0=>A, 1=>B}") use:

'AddDebugText(Hash.Print)

'   Other methods are described below.
'  Hope it will be useful for somebody!~
' ---------------------------------------------------------------------

'Dim nil ' Disable this line if you are also using C_Array
         ' C_Array will be only needed to use C_Hash for the methods 'Keys' and 'Values',
         ' all other methods are not C_Array dependent

Class C_Hash
   Public Index(99)     ' Key or Value index. Used internally.
   Private MaxLength      ' Max Length allowed for a C_Hash. Used internally.
   Private PClone         ' Private Clone of the C_Array. Used internally.
   Public Default         ' Default Value when you call it from a not defined Key. Equals 'nil' by default.

' ---------------------------------------------------------------------
' * Initialize
' ---------------------------------------------------------------------

   Private Sub Class_Initialize()
      MaxLength = 99
      Default = nil
      Dim i : For i = 0 To MaxLength
         ' Assign 'nil' data to each index
         index(i) = nil
      Next
   End Sub

' ---------------------------------------------------------------------
' * Length
' ---------------------------------------------------------------------
'   Gets the Hash Length
'    The Pair Length (or Count) is the half of the total items length.
'   Example:
'   {0=>"A",1=>"B"} => 4
' ---------------------------------------------------------------------

   Public Function Length()
      Length = 0
      Dim i : For i = 0 To MaxLength
         If TypeName(index(i)) <> "Empty" Then
            Length = i + 1
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * Count
' ---------------------------------------------------------------------
'   The Pair Length, so the half of the total items length.
'   Example:
'   {0=>"A",1=>"B"} => 2
' ---------------------------------------------------------------------

   Public Function Count()
      Count = Length / 2
   End Function

' ---------------------------------------------------------------------
' * Create
' ---------------------------------------------------------------------
'   This is the method used to add data on a Hash.
'   Example:
'   {0=>"A"} -> Create 1, "B" => {0=>"A", 1=>"B"}
' ---------------------------------------------------------------------

   Public Sub Create(Byval k, Byval v)
      Dim n
      If IsEmpty() Then
         n = 0
      Else
         n = Length
      End If
      Select Case TypeName(k)
         Case "String", "Integer"
            index(n) = k
         Case "C_Array"
            Set index(n) = New C_Array
            index(n).Replace k
         Case "C_Hash"
            Set index(n) = New C_Hash
            index(n).Replace k
      End Select
      Select Case TypeName(v)
         Case "String", "Integer"
            index(n+1) = v
         Case "C_Array"
            Set index(n+1) = New C_Array
            index(n+1).Replace v
         Case "C_Hash"
            Set index(n+1) = New C_Hash
            index(n+1).Replace v
      End Select
   End Sub

' ---------------------------------------------------------------------
' * GetKey
' ---------------------------------------------------------------------
'   Gets the Key corresponding to a Value
'   Example:
'   {0=>"A"} -> GetKey("A") => 0
' ---------------------------------------------------------------------

   Public Function GetKey(Byval v)
      Dim i : For i = 0 to (Length - 1)
         If (i mod 2 = 1) Then
            Dim CheckValue
            Select Case TypeName(index(i))
            Case "String", "Integer"
               If TypeName(v) = "String" Or TypeName(v) = "Integer" Then
                  CheckValue = (index(i) = v)
               Else
                  CheckValue = False
               End If
            Case "C_Array", "C_Hash"
               If TypeName(v) = TypeName(index(i)) Then
                  Dim n : For n = 0 to (v.Length - 1)
                     If v.index(n) = index(i).index(n) Then
                        CheckValue = True
                     Else
                        CheckValue = False
                        Exit For
                     End If
                  Next
               Else
                  CheckValue = False
               End If
            End Select
            If (CheckValue = True) Then
               Select Case TypeName(index(i - 1))
               Case "String", "Integer"
                  GetKey = index(i - 1)
               Case "C_Array"
                  Set GetKey = New C_Array
                  GetKey.Replace index(i - 1)
               Case "C_Hash"
                  Set GetKey = New C_Hash
                  GetKey.Replace index(i - 1)
               End Select
            End If
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * GetValue
' ---------------------------------------------------------------------
'   Gets the Value corresponding to a Key
'   Example:
'   {0=>"A"} -> GetValue(0) => "A"
' ---------------------------------------------------------------------

   Public Function GetValue(Byval k)
      Dim i : For i = 0 to (Length - 1)
         If (i mod 2 = 0) Then
            Dim CheckKey
            Select Case TypeName(index(i))
            Case "String", "Integer"
               If TypeName(k) = "String" Or TypeName(k) = "Integer" Then
                  CheckKey = (index(i) = k)
               Else
                  CheckKey = False
               End If
            Case "C_Array", "C_Hash"
               If TypeName(k) = TypeName(index(i)) Then
                  Dim n : For n = 0 to (k.Length - 1)
                     If k.index(n) = index(i).index(n) Then
                        CheckKey = True
                     Else
                        CheckKey = False
                        Exit For
                     End If
                  Next
               Else
                  CheckKey = False
               End If
            End Select
            If (CheckKey = True) Then
               Select Case TypeName(index(i + 1))
               Case "String", "Integer"
                  GetValue = index(i + 1)
               Case "C_Array"
                  Set GetValue = New C_Array
                  GetValue.Replace index(i + 1)
               Case "C_Hash"
                  Set GetValue = New C_Hash
                  GetValue.Replace index(i + 1)
               End Select
            End If
            If TypeName(GetValue) = "Empty" Then
               GetValue = Default
            End If
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * DeleteByKey
' ---------------------------------------------------------------------
'   Deletes a Pair using its Key as the identifier
'   Example:
'   {0=>"A", 1=>"B"} -> DeleteByKey 1 => {0=>"A"}
' ---------------------------------------------------------------------

   Public Sub DeleteByKey(Byval k)
      Dim i : For i = 0 to (Length - 1)
         If (i mod 2 = 0) Then
            Dim CheckKey
            Select Case TypeName(index(i))
            Case "String", "Integer"
               If TypeName(k) = "String" Or TypeName(k) = "Integer" Then
                  CheckKey = (index(i) = k)
               Else
                  CheckKey = False
               End If
            Case "C_Array", "C_Hash"
               If TypeName(k) = TypeName(index(i)) Then
                  Dim n : For n = 0 to (k.Length - 1)
                     If k.index(n) = index(i).index(n) Then
                        CheckKey = True
                     Else
                        CheckKey = False
                        Exit For
                     End If
                  Next
               Else
                  CheckKey = False
               End If
            End Select
            If (CheckKey = True) Then
               Clone()
               Dim j : For j = (i) To (Length - 1)
                  Select Case TypeName(PClone.index(j + 2))
                  Case "String", "Integer"
                     index(j) = PClone.index(j + 2)
                  Case "C_Array"
                     Set index(j) = New C_Array
                     index(j).Replace PClone.index(j + 2)
                  Case "C_Hash"
                     Set index(j) = New C_Array
                     index(j).Replace PClone.index(j + 2)
                  Case "Empty"
                     index(j) = nil
                  End Select
               Next
            End If
         End If
      Next
   End Sub

' ---------------------------------------------------------------------
' * DeleteByValue
' ---------------------------------------------------------------------
'   Deletes a Pair using its Value as the identifier
'   Example:
'   {0=>"A", 1=>"B"} -> DeleteByValue "B" => {0=>"A"}
' ---------------------------------------------------------------------

   Public Sub DeleteByValue(Byval v)
      Dim i : For i = 0 to (Length - 1)
         If (i mod 2 = 1) Then
            Dim CheckValue
            Select Case TypeName(index(i))
            Case "String", "Integer"
               If TypeName(v) = "String" Or TypeName(v) = "Integer" Then
                  CheckValue = (index(i) = v)
               Else
                  CheckValue = False
               End If
            Case "C_Array", "C_Hash"
               If TypeName(v) = TypeName(index(i)) Then
                  Dim n : For n = 0 to (v.Length - 1)
                     If v.index(n) = index(i).index(n) Then
                        CheckValue = True
                     Else
                        CheckValue = False
                        Exit For
                     End If
                  Next
               Else
                  CheckValue = False
               End If
            End Select
            If (CheckValue = True) Then
               Clone()
               Dim j : For j = (i - 1) To (Length - 1)
                  Select Case TypeName(PClone.index(j + 2))
                  Case "String", "Integer"
                     index(j) = PClone.index(j + 2)
                  Case "C_Array"
                     Set index(j) = New C_Array
                     index(j).Replace PClone.index(j + 2)
                  Case "C_Hash"
                     Set index(j) = New C_Array
                     index(j).Replace PClone.index(j + 2)
                  Case "Empty"
                     index(j) = nil
                  End Select
               Next
            End If
         End If
      Next
   End Sub

' ---------------------------------------------------------------------
' * IsEmpty
' ---------------------------------------------------------------------
'   The Array IsEmpty only if all its index equal 'nil'
' ---------------------------------------------------------------------

   Public Function IsEmpty()
      IsEmpty = (Length = 0)
   End Function

' ---------------------------------------------------------------------
' * Clear
' ---------------------------------------------------------------------
'   Clears the Hash setting all its index to 'nil'
' ---------------------------------------------------------------------

   Public Sub Clear()
      Dim i : For i = 0 To MaxLength
         index(i) = nil
      Next
   End Sub

' ---------------------------------------------------------------------
' * Print
' ---------------------------------------------------------------------
'   Print the Hash on a easy readable format
' ---------------------------------------------------------------------

   Public Function Print()
      Print = "{"
      Dim Sep
      Sep = False
      Dim i : For i = 0 To (Length - 1) step 2
         Dim New_Key
         If TypeName(index(i)) = "C_Array" Or TypeName(index(i)) = "C_Hash" Then
            New_Key = index(i).Print
         Else
            New_Key = index(i)
         End If
         Dim New_Value
         If TypeName(index(i+1)) = "C_Array" Or TypeName(index(i+1)) = "C_Hash" Then
            New_Value = index(i+1).Print
         Else
            New_Value = index(i+1)
         End If
         If (Sep = False) Then
            Sep = True
         Else
            Print = Print & ","
         End If
         Print = Print & New_Key & "=>" & New_Value
      Next
      Print = Print & "}"
   End Function

' ---------------------------------------------------------------------
' * Clone
' ---------------------------------------------------------------------
'   Clones the current state of the hash on PClone
' ---------------------------------------------------------------------

   Private Sub Clone()
      Set PClone = new C_Hash
      Dim i : For i = 0 To (Length - 1)
         Select Case TypeName(index(i))
         Case "String", "Integer"
            PClone.index(i) = index(i)
         Case "C_Array"
            Set PClone.index(i) = New C_Array
            PClone.index(i).Replace index(i)
         Case "C_Hash"
            Set PClone.index(i) = New C_Hash
            PClone.index(i).Replace index(i)
         Case "Empty"
            PClone.index(i) = nil
         End Select
      Next
   End Sub

' ---------------------------------------------------------------------
' * IncludeKey
' ---------------------------------------------------------------------
'   Checks if the Hash contains a Key
'   WARNING! Only works with String and Integer based Hashes
' ---------------------------------------------------------------------

   Public Function IncludeKey(ByVal k)
      IncludeKey = Keys.Include(k)
   End Function

' ---------------------------------------------------------------------
' * IncludeValue
' ---------------------------------------------------------------------
'   Checks if the Hash contains a Value
'   WARNING! Only works with String and Integer based Hashes
' ---------------------------------------------------------------------

   Public Function IncludeValue(ByVal v)
      IncludeValue(v) = Values.Include(v)
   End Function

' ---------------------------------------------------------------------
' * Replace
' ---------------------------------------------------------------------
'   Replaces the entire Hash to coincide to another one
'   Used as a Clone statement
' ---------------------------------------------------------------------

   Public Sub Replace(ByVal Hash)
      Clear()
      Dim i
      For i = 0 To Hash.Length
         index(i) = Hash.index(i)
      Next
   End Sub

' ---------------------------------------------------------------------
' * Keys
' ---------------------------------------------------------------------
'   Creates an Array containing all the Keys of the Hash
'   WARNING! Requires C_Array
'   WARNING! Only works with String and Integer based Hashes
' ---------------------------------------------------------------------

   Public Function Keys
      Set Keys = New C_Array
      Dim i : For i = 0 To (Length - 1)
         If (i mod 2 = 0) Then
            Keys.Push index(i)
         End If
      Next
   End Function

' ---------------------------------------------------------------------
' * Values
' ---------------------------------------------------------------------
'   Creates an Array containing all the Values of the Hash
'   WARNING! Requires C_Array
'   WARNING! Only works with String and Integer based Hashes
' ---------------------------------------------------------------------

   Public Function Values
      Set Values = New C_Array
      Dim i : For i = 0 To (Length - 1)
         If (i mod 2 = 1) Then
            Values.Push index(i)
         End If
      Next
   End Function
End Class


And an example of Hash & Array usage:

Code:
' *********************************************************************
' **                                                                 **
' **                      Light Management B                         **
' **                                                                 **
' *********************************************************************

Dim GreenHash1 : Set GreenHash1 = New C_Hash
   DefineLight GreenHash1, "Green1", 215, 583
Dim GreenHash2 : Set GreenHash2 = New C_Hash
   DefineLight GreenHash2, "Green2", 253, 583
Dim GreenHash3 : Set GreenHash3 = New C_Hash
   DefineLight GreenHash3, "Green3", 290, 583
Dim GreenHash4 : Set GreenHash4 = New C_Hash
   DefineLight GreenHash4, "Green4", 326, 583

Sub DefineLight(Byval Hash, Byval Name, Byval x, Byval y)
   Hash.Clear()
   Hash.Create "Name", Name
   Hash.Create "X", x
   Hash.Create "Y", y
End Sub

Dim GreenSeq
Set GreenSeq = New C_Array
GreenSeq.Push "GreenHash1" 'GreenHash1.GetValue("Name")
GreenSeq.Push "GreenHash2" 'GreenHash2.GetValue("Name")
GreenSeq.Push "GreenHash3" 'GreenHash3.GetValue("Name")
GreenSeq.Push "GreenHash4" 'GreenHash4.GetValue("Name")

' LightStateByRect GreenSeq, BulbOn, 0, 0, 270, 600

Sub LightStateByRect(ByVal Array, ByVal State, ByVal Xs, ByVal Ys, ByVal Xe, ByVal Ye)
   Dim i
   For i = 0 to (Array.Length - 1)
      Dim X
      Dim Y
      X = Eval(Array.index(i)).GetValue("X")
      Y = Eval(Array.index(i)).GetValue("Y")
      If (X > Xs) And (X < Xe) And (Y > Ys) And (Y < Ye) Then
         Dim Name
         Name = Eval(Array.index(i)).GetValue("Name")
         Eval(Name).State = State
      End If
   Next
End Sub


Every time I do this type of scripts, the very next day I think "I have to drink less" :lol:


Last edited by Wecoc on Thu Apr 02, 2015 3:28 am, edited 2 times in total.

Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.0
 Post Posted: Mon Mar 02, 2015 8:40 pm 
Offline
User avatar

Joined: Thu Jul 01, 2010 3:02 pm
Posts: 265
Very cool on this Wecoc! I've tried to use the Eval function before to generate event handlers dynamically based upon function inputs (e.g. you want to register for all 15 Hit events you can spit out the subroutine via the Eval function). Very neat to see ways to make script programming in FP easier!

-mark


Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.0
 Post Posted: Tue Mar 03, 2015 9:49 pm 
Offline

Joined: Mon Jan 27, 2014 12:36 pm
Posts: 2133
Location: Ontario, Canada
trying to get get debug window to show stuff and I get error message:


You do not have the required permissions to view the files attached to this post.


Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.0
 Post Posted: Wed Mar 04, 2015 1:16 pm 
Offline
User avatar

Joined: Fri Sep 09, 2011 9:14 pm
Posts: 142
Hmm... No idea what happens here, I tested it and it works fine to me.
Are you sure you defined the "Array" object as a C_Array?


Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.0
 Post Posted: Wed Mar 04, 2015 6:08 pm 
Offline

Joined: Mon Jan 27, 2014 12:36 pm
Posts: 2133
Location: Ontario, Canada
Ok thanks.. I am off to the races now


Top 
 Profile  
 
 Post subject: Re: FP Scripts 'C_Array' & 'C_Hash' v1.1
 Post Posted: Wed Mar 04, 2015 8:41 pm 
Offline
User avatar

Joined: Fri Sep 09, 2011 9:14 pm
Posts: 142
*Updates to 1.1*

I just made little changes here. Length was returning one less all the time so I had to change it across the scripts.

There remain two things that could be improved in both scripts, but I'll let it for now.

- The 'nil' variable dependence. I know you can use 'nothing' directly, and I tried it in the very start, but when I tried I had some annoying bugs so I decided to use that variable.

- MaxLength is not modifiable, it always equals 99. Somehow, using Redim Index(MaxLength), or maybe Redim preserve Index(MaxLength) it could be defined a useful SetMaxLength method.

Anyway, here is the last example of usage:

Code:
' *********************************************************************
' **                                                                 **
' **                      Light Management C                         **
' **                                                                 **
' *********************************************************************

Dim SavingArray
Set SavingArray = New C_Array

' SaveLightState SavingArray, "Green1"
' SaveLightState SavingArray, "Green2"
' SaveLightState SavingArray, "Green3"
' SaveLightState SavingArray, "Green4"

Sub SaveLightState(ByVal Array, ByVal LightName)
   Dim LightHash : Set LightHash = New C_Hash
    Dim Light : Set Light = Eval(LightName)
   LightHash.Create "ObjType", "Light"
   LightHash.Create "Name", LightName
   LightHash.Create "State", CInt(Light.State)
   LightHash.Create "BlinkPattern", Light.BlinkPattern
   LightHash.Create "BlinkInterval", CInt(Light.BlinkInterval)
   Array.Push(LightHash)
End Sub

Sub LoadLightState(ByVal Array, ByVal LightName)
   Dim i : For i = 0 to (Array.Length - 1)
      Dim LightHash : Set LightHash = New C_Hash
      LightHash.Replace Array.index(i)
      If (LightHash.GetValue("ObjType") = "Light") Then
         If (LightHash.GetValue("Name") = LightName) Then
            Dim Light : Set Light = Eval(LightName)
            Light.State = LightHash.GetValue("State")
            Light.BlinkPattern = LightHash.GetValue("BlinkPattern")
            Light.BlinkInterval = LightHash.GetValue("BlinkInterval")
         End If
      End If
   Next
End Sub

' LoadAllState SavingArray

Sub LoadAllState(ByVal Array)
   Dim i : For i = 0 to (Array.Length - 1)
      Dim LightHash : Set LightHash = New C_Hash
      LightHash.Replace Array.index(i)
      If (LightHash.GetValue("ObjType") = "Light") Then
         LoadLightState Array, LightHash.GetValue("Name")
      End If
   Next
End Sub


To recap on the used examples, always referring to lights:
- The first one allows to rotate, check and set the state of lights.
- The second one uses X and Y ranges to set the state of certain lights.
- The third one allows to save and load automatically the state, blink pattern and blink interval of lights.

That's all, folks!


Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 9 posts ] 

Board index » Resources » Resources Exchange


Who is online

Users browsing this forum: No registered users and 5 guests

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron