split string?

Discuss Lemur and share techniques.
newtfish
Newbie
Posts:41
Joined:09 Apr 2011 19:32
split string?

Post by newtfish » 21 Feb 2015 15:53

Hi,

I'm wondering if it's possible to split a string, on a particular character, to create an array?

e.g.
decl string;
string = split('Mystringishere Mystringisthere', ' ');


Therefore:
string[0] is 'Mystringishere'

and

string[0] is 'Mystringisthere'

oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19

Re: split string?

Post by oldgearguy » 23 Feb 2015 10:48

you have to write your own split function. Use sizeof() to get the size of the array, then loop through looking for your split character. Once you find it, you can use subarray() to chop out the pieces.

Since Lemur doesn't support char arrays (or indeed any multi-dimensional arrays), it's hard to make it really generic since if there's more than 1 split char found, you'll need to have space to store all the new pieces. I'd probably pass in the original string and the split character and return an array of positions where the split character was found (empty array indicating not found at all since 0 is a valid offset, unless you decide to throw away split characters found at the very beginning and very end - see next comment for why that might be good). Then the calling function could do a sizeof() and see how many splits are there (numSplits = sizeof(result)+1, as long as there are no split chars at the beginning or end) and loop through using subarray() to pull out the pieces you care about.

Ozymandias
Newbie
Posts:18
Joined:18 Nov 2014 17:47

Re: split string?

Post by Ozymandias » 25 Feb 2015 14:43

oldgearguy,

I was under the impression that you can't treat a string as an array. If that's the case, how does your approach get around this?

I'm still pretty new to Lemur, so apologies if I've misunderstood your post.

oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19

Re: split string?

Post by oldgearguy » 25 Feb 2015 15:51

Ozymandias wrote:oldgearguy,

I was under the impression that you can't treat a string as an array. If that's the case, how does your approach get around this?

I'm still pretty new to Lemur, so apologies if I've misunderstood your post.
Yeah, you are right. I thought for sure I was taking apart strings in one of my templates, but in fact everything was being kept in an array. So, the answer at this point is - once it's in string format you can't manipulate it further, only display it.

newtfish
Newbie
Posts:41
Joined:09 Apr 2011 19:32

Re: split string?

Post by newtfish » 27 Feb 2015 20:07

Thanks oldgearguy,

This raises another question in my mind. How is it possible to turn it into an array and then into a number (directly)? Without turning it into string...

How does Lemur differentiate between what is being converted into a number, and what is being converted into a string? Is there some special function needed to subarray it and then turn it into a number? e.g.

Variable > ASCII array > loop through till char found > subarray (to split) > Then what????

The next question I have is, how efficient is this? Clearly there is a lot of processing happening here.

Cheers

N

oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19

Re: split string?

Post by oldgearguy » 28 Feb 2015 13:30

What exactly are you trying to achieve?

If you have an array of characters (say from a MIDI receive operation), the common method of subtracting it from the ASCII value of 0 to get a number (i.e. 38h - 30h = 8) and also doing some checks and processing for a-f are what people do in Lemur-land. You also have to track the place (10, 100, 1000, 1/10, etc) and multiply and add properly.

Basically, data type transforms involving numbers are a PITA. Not hard, but tedious to do and get right for all cases. If you can leave it as a number for as long as possible and use whatever built-in numerical processing is available, the better.

Let us know the goal/scenario and maybe an alternate approach would work better for you.

newtfish
Newbie
Posts:41
Joined:09 Apr 2011 19:32

Re: split string?

Post by newtfish » 28 Feb 2015 21:00

Hi oldgearguy,

I'm sending an OSC message over midi (as sysex): (so this is what is received in the Lemur)

"/Knob/x 0.933000"

Which when converted to sysex is:

{45,75,110,111,98,47,120,32,48,46,57,51,51,48,48,48}

I'm guessing I have to split on space " " which is 32?

So for the float/number I'm left with: (there are always the same number of decimal places)

array1 = {48,46,57,51,51,48,48,48}

I'm not really sure what to do next? How to turn this into a float? There is a decimal point as the second number (46), so also not sure how to get this to function?

Do I have to loop through each number and find the converted number, and then the decimal position? e.g.

if(array1[0] == 48){numval1 = 0};
if(array1[2] == 48){numval2 = "."};

cheers

newtfish
Newbie
Posts:41
Joined:09 Apr 2011 19:32

Re: split string?

Post by newtfish » 28 Feb 2015 23:28

Strange, I get the feeling this should work. However it gives me "15" rather than "0.530000", for the values:

sysex4 = {48,46,57,51,51,48,48,48};

A script called "ChangeValues()" to change the midi values to numbers:

Code: Select all

decl a;
if(array == 48){
		a = 0;
}else if(array == 49){
		a = 1;
}else if(array == 50){
		a = 2;
}else if(array == 51){
		a = 3;
}else if(array == 52){
		a = 4;
}else if(array == 53){
		a = 5;
}else if(array == 54){
		a = 6;
}else if(array == 55){
		a = 7;
}else if(array == 56){
		a = 8;
}else if(array == 57){
		a = 9;
}
return a;
And another script to loop through and put them in the right decimal places

Code: Select all

sysex4 = {48,46,57,51,51,48,48,48};

decl i;
decl e;
decl f;
decl g = sysex4;
decl h;
decl j;

e = sizeof(sysex2);

for(i = 0; i < e; i++){
		if(i == 0){f = 0};
		if(i == 1){f = 0};
		if(i == 2){f = 10};
		if(i == 3){f = 100};
		if(i == 4){f = 1000};
		if(i == 5){f = 10000};
		if(i == 6){f = 100000};
		if(i == 7){f = 1000000};

		if(i !=1 ){
				j = ChangeValues(g[i]) / f;
				h += j;
		}


}

Monitor12.value = h;

oldgearguy
Regular
Posts:315
Joined:02 Nov 2013 11:19

Re: split string?

Post by oldgearguy » 01 Mar 2015 20:57

Here's something quick.

It works for your example (0.933). I think I coded in some basic error handling and such, but you might want to test it across many cases including no space, no decimal, etc and put in error handling for all that.

let me know if you have any questions on how it works.

Oh -- edit the sysex string in the doMath() script and then click on the Calc button to execute it.
Attachments
setting.jzml
decimal math
(2.8KiB)Downloaded 89 times

newtfish
Newbie
Posts:41
Joined:09 Apr 2011 19:32

Re: split string?

Post by newtfish » 02 Mar 2015 18:24

Thanks oldgearguy.

This is brilliant. Works perfectly.

Cheers

N

Post Reply