Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array functions - Unique, In_Array, Intersection
#1
Does QM support functions like the PHP function In_Array

http://php.net/manual/en/function.in-array.php


or findings the intersection of 2 sorted arrays?
http://www.geeksforgeeks.org/union-and-i...-arrays-2/

I saw in http://www.quickmacros.com/forum/showthread.php?tid=603 that someone was asking about other array functions and reply that it would be hard for all array types.

I just posted some for making unique array to forum http://www.quickmacros.com/forum/showthr...p?tid=5144.
I think I will need help to do these though my use case is limited to str array and perhaps int.

For In_Array function, I want to test whether there is match between all elements of one array within the elements of second array.

For example, I would like to determine, if "A" AND "C" AND "E" are in ARRAY(str) a = "A[]B[]C[]D[]E"
If all elements are there (any order), there then would return true. Any element missing, would return False.
For simplicity, it can be exact match but ideally you could have option for wildcard or regex type matching.

The way I have done this is to take the ARRAY(str) and put it into a regular string and then do a bunch of conditional findrx statements on it:
i.e.

Code:
Copy      Help
ARRAY(str) a = "A[]B[]C[]D[]E"
str s = a
if findrx(s "A") > -1 and findrx(s "C") > -1 and findrx(s "E") > -1;
,out "TRUE"

I guess this strategy could also work for OR statements i.e. Are any of these elements (A, B, or C) in the array.

Is there an easy way to pull out just one dimension from a multi-dimensional array into a str variable.
If 1-dim, it's just
Code:
Copy      Help
ARRAY(str) a = "A[]B[]C[]D[]E"
str s = a

but with multi-dimension, you have to do something like this:
Macro Macro21
Code:
Copy      Help
ARRAY(str) MultiDimArray.create(3 5)

MultiDimArray[2 0] = "red"
MultiDimArray[2 1] = "red"
MultiDimArray[2 2] = "blue"
MultiDimArray[2 3] = "blue"
MultiDimArray[2 4] = "red"



for _i 0 MultiDimArray.len
,str StringCollFrom3rdColumnOf2DimArray
,StringCollFrom3rdColumnOf2DimArray = F"{StringCollFrom3rdColumnOf2DimArray}[]{MultiDimArray[2 _i]}"
out StringCollFrom3rdColumnOf2DimArray.trim

Is there a syntax like:
str s = a[2] to make string collection out of 3rd column elements?

Similarly for Intersection, I know how to loop one array against the other, which should be fine for small comparisons but not necessarily good for large comparisons:

Macro ArrayIntersect_Str
Code:
Copy      Help
ARRAY(str) s1 = "red[]blue[]green[]yellow"

ARRAY(str) s2 = "red[]orange[]blue[]green[]brown[]pink"

ARRAY(str) s3

int i j
for i 0 s1.len
,for j 0 s2.len
,,if s1[i] = s2[j]
,,,s3[] = s1[i]
,,,break
if s3.len
,out F"Total intersecting elements = {s3.len}"
,out s3
else
,out F"No intersecting elements"

Just wondering if there is a more elegant way of doing this or perhaps can be added to QM in future (I see your comments on problem with adding as member functions (various array types, etc) but perhaps these array user-defined functions can be assembled into QM Extensions folder. (I just saw ArrayIntFromList but there could be many others as above).

Thanks so much!!!!!,
S
#2
QM2 does not have an easy and elegant way to create such functions. Need to create functions for each array type. Need for(i 0 a.len) ..., and it is slow in QM2.
Currently I cannot create such functions. Don't have time etc.


Forum Jump:


Users browsing this thread: 1 Guest(s)