Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY member functions
#1
I tried to create the functions: insert, pop, push, reverse and
Warning: ARRAY cannot have member functions

is it normal?
#2
Yes. I should remove this limitation, but it will require much time.
#3
You cannot add member functions to ARRAY, but you can add member functions to a class containing an ARRAY.

Example:
Code:
Copy      Help
;define simple string stack class
class CStrStack -ARRAY(str)a

;examples
CStrStack st
st.Push("abc")

str s
st.Pop(s)
out s

st.Pop(s) ;;error

Member function CStrStack.Push:
Code:
Copy      Help
function $s
a[a.redim(-1)]=s

Member function CStrStack.Pop:
Code:
Copy      Help
function str&s
int i=a.len-1
if(i<0) end "stack is empty"
s=a[i]
a.redim(i)
#4
Ok. Thanks.
#5
Can you add the functions: insert, pop, push, reverse to arrays in the next QM version?
#6
I'll try, thank you.
#7
Can you add the functions: pop, push, reverse, diff... to arrays in the next QM version?

http://php.net/manual/en/ref.array.php
#8
I want to keep exe small and therefore don't add many functions.
push and pop are really useful but can be easily replaced using code like this
Macro Macro1405
Code:
Copy      Help
ARRAY(int) a
int x=5
a[]=x ;;a.push(x)
x=a[a.ubound]; a.redim(a.ubound) ;;x=a.pop()
out x
out a.len

The others are rarely used. They don't require low-level programming (explicit memory allocation etc), therefore you can create user-defined functions to do it.
#9
It would be nice to have other ARRAY functions.

Member function ARRAY.unique
Code:
Copy      Help
function

int i j

for i this.len-1 0 -1
,for(j 0 i) if this[j]=this[i]; this.remove(i); break

Macro Remove duplicates
Code:
Copy      Help
ARRAY a.create(7)

a[0]= 1
a[1]= 1
a[2]= 2
a[3]= 3
a[4]= 3
a[5]= 6
a[6]= 6


a.unique

int i

out
for i 0 a.len
,out a[i]


Forum Jump:


Users browsing this thread: 3 Guest(s)