Passing values from one array to another

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Passing values from one array to another

Post 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:

Code: Select all

ArrayTwo=ArrayOne;
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?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Passing values from one array to another

Post 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++;
  }
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Passing values from one array to another

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Passing values from one array to another

Post by mattallen37 »

Ok, thanks for the advice, it works a lot better your way.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Passing values from one array to another

Post 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)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Passing values from one array to another

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Passing values from one array to another

Post 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
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Passing values from one array to another

Post 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)
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Passing values from one array to another

Post 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 :P
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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Passing values from one array to another

Post by HaWe »

[very very OT]
@matt:
of course, shame on me - it's earlier in Michigan, not later like I falsely thought... :oops:
@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]
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest