Can Anyone Explain This Array Behavior?

Discuss Lemur and share techniques.
Post Reply
ForestCat
Regular
Posts:59
Joined:02 Mar 2012 21:00
Can Anyone Explain This Array Behavior?

Post by ForestCat » 19 Aug 2015 00:59

I was trying to match an incoming sysex substring, but I've trimmed this down to the bare essentials to demonstrate the problem & the template below is dirt-simple.
Objective: To compare a subarray to another array
Problem: Getting a "match" on certain arrays with different contents

I'm certain I'm doing this wrong, otherwise it would be all over this board. If there is a better/simpler way to compare arrays, I sure am ready for it :-)

How it works:

Three buttons, three monitors.
The buttons load Array1 with three different 13-byte strings.
CompareArrays() script executes on expression Array1 (whenever it's changed by a button)
it grabs a subarray and compares it to the same fixed 6-byte array in each case,
The three monitors display the substring from Array1, the fixed array, and a 1 for match, 0 for no match.

Case 1 should match, and it does.
Case 2 should NOT match, but it does.
Case 3 should NOT match, and it doesn't

At the limit of my talent trying to understand why.

Here's the main compare script:

//Array1 is loaded in the buttons
decl SubArray1 = subarray(Array1,7,6); // grab a substring to compare

SubArray_1.value = arrayToHex(SubArray1); //display substring in hex in monitor

decl CompareArray = {0x00, 0x00, 0x15, 0x00, 0x2C, 0x5F}; //here's our string to compare
Compare_Array.value = arrayToHex(CompareArray); //display the compare string in hex in monitor
if (! sumof(CompareArray - SubArray1) ) Match.value = 1; //compare the arrays
// if they match, display 1, otherwise display 0
else Match.value = 0;

**************************************
Attachments
nn_StringCompare-01.jzml
(10.75KiB)Downloaded 96 times

Macciza
Regular
Posts:1325
Joined:07 Dec 2011 04:57
Location:Sydney, Australia.

Re: Can Anyone Explain This Array Behavior?

Post by Macciza » 21 Aug 2015 08:10

The problem is that the script does not actually check equivalence of every item, just a tally i.e. sumof ....
Those values just happen to return 9 and -9 as well as lots of 0's. when summed it equals 0

If you are expecting 0's then a check using firstof is better - if firstof is not equal to sizeof then values are not all 0 . . ..
Can have a look for some compare functions a bit later if you need ...
cheers
MM
iMac 2.8G i7 12G 10.6.8/10.7.2, Legacy Dexter/Lemur, Liine Lemur/iPad2, KMI SoftStep, 12Step & QuNeo , B-Controls, Mackie C4 etc
MaxMSP, Live Suite, Native Instrument stuff, etc Modified Virtual Guitar System etc All Projects/Modules © CC-BY-NC-SA[*][/b]

ForestCat
Regular
Posts:59
Joined:02 Mar 2012 21:00

Re: Can Anyone Explain This Array Behavior?

Post by ForestCat » 21 Aug 2015 17:31

Thanks, Macciza. While waiting for a reply, I found your solution here: https://forum.liine.net/viewtopic.php?f ... 1248#p4799

It works on an iPad running Lemur 4.12, It does NOT work on either an iPhone or iPod Touch running 5.xx.
It appears something was changed in the vector code somewhere along the line?

What is working for the time being is this, taken from the SLVector library. It's more code/processing than I wanted, though, since it relies on sort() (below):

***********************
//Return 1 if vector b is a subset of vector a (a contains all of the elements in b) and 0 otherwise
decl i, j, found, lenA=sizeof(a), lenB=sizeof(b), sa=sort(a), sb=sort(b);

if(lenB > lenA) return 0;
for(i=0; i < lenB; i++)
{
found = 0;
for(j=0; j < lenA; j++)
{
if(sa[j] == sb) { found = 1; break; }
}

if(!found) return 0;
}

return 1;

******************
sort() function
******************
//Sort vector a using comb sort
decl i, len=sizeof(a), gap=len, swapped=0, tmp;

while(gap > 1 || swapped)
{
if(gap > 1) gap = floor(gap/1.247330950103979);
swapped = 0;

for(i=0; (gap + i) < len; ++i)
{
if(a - a[i + gap] > 0)
{
tmp = a;
a = a[i + gap];
a[i + gap] = tmp;
swapped = 1;
}
}
}

return a;

Post Reply