Page 1 of 1
Passing values from one array to another
Posted: 25 Feb 2011, 01:44
by mattallen37
How do you make an array equal all but the first part of another array? I want to do something like this:
Code: Select all
byte ArrayOne[];
byte ArrayTwo[];
...function call that returns, filling ArrayOne with values...
At this point, ArrayOne is the result of a function (undetermined length).
I now want to pass those values to ArrayTwo, but I don't care about the first three bytes of ArrayOne.
I don't want to do the following:
because it would include the three first "trash" bytes.
I don't want to do this either:
Code: Select all
ArrayTwo[0]=ArrayOne[3];
ArrayTwo[1]=ArrayOne[4];
ArrayTwo[2]=ArrayOne[5];
ArrayTwo[3]=ArrayOne[6];
ArrayTwo[4]=ArrayOne[7];
ArrayTwo[5]=ArrayOne[8];
ArrayTwo[6]=ArrayOne[9];
because I don't know how many bytes ArrayOne contains, and I do not want a runtime error.
How should I do it?
Re: Passing values from one array to another
Posted: 25 Feb 2011, 01:54
by mattallen37
Ok, I think I figured it out. This seems to do what I want:
Code: Select all
i=0;
ArrayTwoLength=ArrayLen(ArrayOne)-3;
byte ArrayTwo[ArrayTwoLength];
repeat(ArrayLen(ArrayOne)-3){
ArrayTwo[i]=ArrayOne[i+3];
i++;
}
Re: Passing values from one array to another
Posted: 25 Feb 2011, 03:20
by afanofosc
From NXCDefs.h:
Code: Select all
/**
* Copy an array subset.
* Copy a subset of the source array starting at the specified index and
* containing the specified number of elements into the destination array.
* \param aout The output array containing the subset.
* \param asrc The input array from which to copy a subset.
* \param idx The start index of the array subset.
* \param len The length of the array subset.
*/
inline void ArraySubset(variant & aout[], variant asrc[], unsigned int idx, unsigned int len);
// ...
#define ArraySubset(_aout, _asrc, _idx, _len) asm { arrsubset _aout, _asrc, _idx, _len }
You can pass NA for either the idx or the len. It means 0 for the index. It means "rest of elements" when used with len. You definitely should use this instead of a loop.
John Hansen
Re: Passing values from one array to another
Posted: 25 Feb 2011, 03:38
by mattallen37
Ok, thanks for the advice, it works a lot better your way.
Re: Passing values from one array to another
Posted: 25 Feb 2011, 08:27
by HaWe
afanofosc wrote:You can pass NA for either the idx or the len. ... You definitely should use this instead of a loop.
John Hansen
why not use a loop (I personally prefer for-loops or while-loops)
Re: Passing values from one array to another
Posted: 25 Feb 2011, 08:55
by mattallen37
I think this is faster and more efficient. Likely, loop uses resources that don't need to be used with the ArraySubset function. Before, I have always only used loops, but only because I didn't know that ArraySubset existed. It seems pretty good to me. I can see how a loop could still be preferred, like if you did some other math or whatever at the same time, and needed an incrementing value in it.
Re: Passing values from one array to another
Posted: 25 Feb 2011, 09:00
by HaWe
This question actually was addressed to John "a-fan-of-osc (?)" ;)
I was curious about his "You definitely should use this instead of a loop."
Most of the time I do quite different things in my programs instead of copying arrays from one to another, so the runtime performance should be below any measurability ...
ps: what is the local time in Michigan ? Won't you soon need to go to bed ? ;)
pps: in "real C" probably only the pointer to the starting index mem address would be copied :P
Re: Passing values from one array to another
Posted: 26 Feb 2011, 05:22
by afanofosc
Let the firmware do in one line of code what would otherwise (via a for or while loop) take many lines of codes repeated over and over again.
John Hansen
P.S. I am a fan of Orson Scott Card (aka osc)
Re: Passing values from one array to another
Posted: 26 Feb 2011, 06:55
by mattallen37
doc-helmut wrote:This question actually was addressed to John "a-fan-of-osc (?)"
I was curious about his "You
definitely should use this instead of a loop."
Most of the time I do quite different things in my programs instead of copying arrays from one to another, so the runtime performance should be below any measurability ...
ps: what is the local time in Michigan ? Won't you soon need to go to bed ?
pps: in "real C" probably only the pointer to the starting index mem address would be copied
Oh ok, sorry.
The time here in Michigan, is EST, so -5. Berlin is CET, so +1. That means that it is 6 hours earlier here than there (assuming you are in the CET time zone). When you posed that, it registers as exactly 4:00 am, so for you it should read 10:00 am. I am posting this at about 1:55 am here, so about 7:55 am for you. Typically, I go to bed at around 4:30 or 5:00 am, and get up mid afternoon. It is much easier for me to concentrate on what I am doing when the house is quiet.
Re: Passing values from one array to another
Posted: 26 Feb 2011, 09:21
by HaWe
[very very OT]
@matt:
of course, shame on me - it's earlier in Michigan, not later like I falsely thought...
@John:
glad to read that there are still other SF fans alive (except me)
I'm a fan of Stanislaw Lem, especially his short stories about Pirx, maybe you already know him?
[/very very OT]