December 03, 2020, 08:30:29 PM

Author Topic: [bmx] Insert value into an array by Bremer [ 1+ years ago ]  (Read 653 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Insert value into an array
Author : Bremer
Posted : 1+ years ago

Description : This function will enable you to insert a value into an existing array. You pass the array, an index number and a value. Then the function will resize the array and insert the value into the index location.

Code :
Code: BlitzMax
  1. Local a:Int[] = [0,1,2,3,4,6,7,8,9]
  2.  
  3. Print "Before: "+Len(a)+" fields"
  4. For Local b:Int = 0 To 8
  5.         Print a[b]
  6. Next
  7.  
  8. a = insertIntoIntArray( a[..], 5, 5 )
  9.  
  10. Print "After: "+Len(a)+" fields"
  11. For Local c:Int = 0 To 9
  12.         Print a[c]
  13. Next
  14.  
  15. Function insertIntoIntArray:Int[](Array:Int[],index:Int,value:Int)
  16.         Local remain:Int = (Len(array)) - index
  17.         Local result:Int[Len(array)+1]
  18.         For Local count:Int = 0 To index-1      ' copy all values before insert
  19.                 result[count] = Array[count]
  20.         Next
  21.         result[index] = value                           ' insert value
  22.         For Local count2:Int = 0 To remain-1    ' copy remaining values after insert
  23.                 result[index+count2+1] = Array[index+count2]
  24.         Next
  25.         Return result
  26. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal