Jump to content

_ArrayShift and _ArrayUnshift


Enigma
 Share

Recommended Posts

I did a search and no one has done this before (according to search that is). Thought I'd post.

#include <Array.au3>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;BEGIN DOC
;
;-Calling Sequence:
;
; _ArrayUnshift()
;
;-Description:
;
; Push a new entry onto the front of an array.
;
;-Inputs:
;
; $a_Array = The array to modify.
; $a_Value = The value to push onto the array.
;
;-Outputs:
;
; $a_Array is modified.
;
;-Revisions:
;
; Name Company Date
; ---------------------------------------------------------------------------
; Mark Manning Simulacron I Mon 07/30/2012 1:38:04.67
; Original Program.
;
;
;END DOC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func _ArrayUnshift( ByRef $a_Array, $a_Value )
local $ret = ubound( $a_Array );

if( $ret > 0 )then
_ArrayReverse( $a_Array );
_ArrayAdd( $a_Array, $a_Value );
_ArrayReverse( $a_Array );
return true;
else
return false;
endif
endfunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;BEGIN DOC
;
;-Calling Sequence:
;
; _ArrayShift()
;
;-Description:
;
; Pop the first entry off of an array.
;
;-Inputs:
;
; $a_Array = The array that is modified.
; $a_Value = The variable to hold the popped off value.
;
;-Outputs:
;
; $a_Array is modified by this function.
; $a_Value is modified to the popped off value.
;
;-Revisions:
;
; Name Company Date
; ---------------------------------------------------------------------------
; Mark Manning Simulacron I Mon 07/30/2012 1:38:28.57
; Original Program.
;
;
;END DOC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
func _ArrayShift( ByRef $a_Array, ByRef $a_Value )
local $ret = ubound( $a_Array );

if( $ret > 0 )then
_ArrayReverse( $a_Array );
$a_Value = _ArrayPop( $a_Array );
_ArrayReverse( $a_Array );
return true;
else
return false;
endif
endfunc
;
; tests
;
dim $i, $a_Value;
dim $ary[10];

consolewrite( "Original Array" & @crlf );
for $i=0 to 9
$ary[$i] = $i;
consolewrite( "Ary[" & $i & "] = " & $i & @crlf );
next

_ArrayUnshift( $ary, 11 );
consolewrite( "------------------------------------------------------------------" &@crlf );
consolewrite( @crlf & "_ArrayUnshift test" & @crlf );
consolewrite( "There are " & ubound($ary) & " entries in the array." & @crlf );
for $i=0 to ubound($ary)-1
consolewrite( "Ary[" & $i & "] = " & $ary[$i] & @crlf );
next

     _ArrayShift( $ary, $a_Value );
consolewrite( "------------------------------------------------------------------" &@crlf );
consolewrite( @crlf & "_ArrayShift test" & @crlf );
consolewrite( "There are " & ubound($ary) & " entries in the array." & @crlf );
for $i=0 to ubound($ary)-1
consolewrite( "Ary[" & $i & "] = " & $ary[$i] & @crlf );
next

Nothing fancy. Just a simple unshift and shift function. If the person who maintains the Array.au3 UDF wants to include these - be my guest. :-)

The best way to run the test is through the AutoIt Debugger since it captures the console output and displays it automatically. You should see:

Original Array

Ary[0] = 0

Ary[1] = 1

Ary[2] = 2

Ary[3] = 3

Ary[4] = 4

Ary[5] = 5

Ary[6] = 6

Ary[7] = 7

Ary[8] = 8

Ary[9] = 9

------------------------------------------------------------------

_ArrayUnshift test

There are 11 entries in the array.

Ary[0] = 11

Ary[1] = 0

Ary[2] = 1

Ary[3] = 2

Ary[4] = 3

Ary[5] = 4

Ary[6] = 5

Ary[7] = 6

Ary[8] = 7

Ary[9] = 8

Ary[10] = 9

------------------------------------------------------------------

_ArrayShift test

There are 10 entries in the array.

Ary[0] = 0

Ary[1] = 1

Ary[2] = 2

Ary[3] = 3

Ary[4] = 4

Ary[5] = 5

Ary[6] = 6

Ary[7] = 7

Ary[8] = 8

Ary[9] = 9

I just checked the PHP website about array_shift and array_unshift and an interesting thing is - if _ArrayAdd() were modified a bit so it did one or more entries based upon whether or not the incoming information was an array or not (see the UBound() function) then this _ArrayUnshift() would work exactly like the PHP function.

Later!

Link to comment
Share on other sites

This is just something like _ArrayDelete() and _ArrayInsert(), or did I miss something?

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Wow! Thought I was seeing double there when I saw your avatar! Still freaking me out a bit. My mind wants to put those two eyes and mouths together. :-P

Anyway - yes and no! :-) _ArrayInsert() does do what _ArrayUnshift() does except _ArrayUnshift is more restricted. _ArrayDelete() does do what _ArrayShift() does - but - _ArrayDelete just deletes the entry. _ArrayShift() returns the entry. But _ArrayShift() could probably be modified to get the value and return it before using _ArrayDelete(). Or if the person who works on the Array.au3 code wanted to modify the _ArrayDelete() function to return the value of whatever it is deleting - then these two functions would not be really needed. I'm just used to using the PHP function calls and didn't see the other ones. For some reason I took _ArrayDelete() to mean to delete the entire array and _ArrayInsert() to insert an entirely new array. What I get for not actually reading the function information. My bad. :-)

Edited by Enigma
Link to comment
Share on other sites

I don't get why you introduce the value 11 with _ArrayUnshift. I would expect -1.which comes before zero. :blink:

In AutoIt it is quite common to exclude the first element from the count, so Ary[0] would be 10. The function Ubound gives the total number of elements which in this case is 11.

Although it's not really needed, I think it's a good idea anyway.

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