Jump to content

Enlarge()


ezzetabi
 Share

Recommended Posts

A decent scripter can do that in half second, but I still think that it may be posted here. Since anyone may need this.

It is a easy func for enlarging a Array of one and updating also the position [0] to the new size.

I use this when loading stuff where I can't know the number of items I'll have.

$a = StringSplit('1|2|3|4|5|6|7|8','|')
MsgBox(0,'',$a[0] & ' - ' & $a[$a[0]])
_Enlarge($a)
$a[$a[0]] = 'New item'
MsgBox(0,'',$a[0] & ' - ' & $a[$a[0]])



Func _Enlarge(ByRef $aArray)
   If Not IsArray($aArray) Then
      SetError(1)
      Return -1
   EndIf
   
   $aArray[0] = UBound($aArray)
   ReDim $aArray[UBound($aArray) + 1]
   Return 0
EndFunc
Link to comment
Share on other sites

Here is a small adjustment to your script to allow the user to add any number of elements to the array:

Func _Enlarge(ByRef $aArray, $iElements)
  If Not IsArray($aArray) Then
     SetError(1)
     Return -1
  EndIf
  
  If Not IsNumeric($iElements)
     SetError(2)
     Return -1
  EndIf

  $aArray[0] = UBound($aArray)
  ReDim $aArray[UBound($aArray) + $iElements]
  Return 0
EndFunc

This is just a natural addition to the function you posted. I personally wouldn't have passed the array ByRef because by doing so you are making a development decision for the scripter that I don't believe is your place. I also don't understand why you keep the previous array size in the [0] element, this should change when you resize. This is how I would have done it (not necisarily any better):

Func _Enlarge( $aArray, $iElements )
  If Not IsArray($aArray) Then
     SetError(1)
     Return $aArray
  EndIf
  
  If Not IsNumeric($iElements)
     SetError(2)
     Return $aArray
  EndIf

  ReDim $aArray[UBound($aArray) + $iElements]
  $aArray[0] = UBound($aArray)
  Return $aArray
EndFunc

I don't have time right now to test either algorithm, but I believe they show proof of concept.

BTW: Good Work!

*** Matt @ MPCS

Edited by Matt @ MPCS
Link to comment
Share on other sites

I am sorry dear. Your script is wrong. :)

First one corrected. (With a little addon)

Func _Enlarge(ByRef $aArray, $iElements)
 If Not IsArray($aArray) Then
    SetError(1)
    Return -1
 EndIf

 If $iElements = '' Then $iElements = 1

 If Not IsInt($iElements)
    SetError(2)
    Return -1
 EndIf

 $aArray[0] = UBound($aArray) - 1 + $iElements
 ReDim $aArray[UBound($aArray) + $iElements]
 Return 0
EndFunc

Second one corrected.

Func _Enlarge( $aArray, $iElements )
 If Not IsArray($aArray) Then
    SetError(1)
    Return $aArray
 EndIf
 
 If $iElements = '' Then $iElements = 1

 If Not IsInt($iElements)
    SetError(2)
    Return $aArray
 EndIf

 ReDim $aArray[UBound($aArray) + $iElements]
 $aArray[0] = UBound($aArray) - 1;The array are indexed from 1 to Ubound -1
 
 Return $aArray
EndFunc

The point of the line

$aArray[0] = UBound($aArray)

is that the array is indexed from 1 to UBound - 1 (since [0] keep the size it is not from 0 to ubound -1), enlarging of 1 means that the [0] will become Ubound - 1 + 1 = Ubound, so for avoiding a useless operation I put the line before enlarging. You see?

I am happy you answered, some time ago I'd not posted a such easy script, but after seeing some of yours I understand that even simple things that help community.

Link to comment
Share on other sites

Although I don't especially like that you imply that my scripting is remedial... thank you for correcting the errors. As posted, these were untested and glance impressions of your code. I'll be sure to keep in mind not to tamper with your scripts in the future... no matter how f***ed they are.

*** Matt @ MPCS

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...