x


Writing certain values of an array into a different array

I've got an array with multiple numbers in it, and i want to take every 15th value in this array and copy it into a different array.

is that possible(preferably without a loop)?

more ▼

asked Jul 21 '11 at 09:14 AM

biohazard gravatar image

biohazard
288 36 42 46

it's a String[]

Jul 21 '11 at 09:28 AM biohazard
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

It's certainly possible, but I don't know how I would do it without a loop, I'm afraid. A loop is the most elegant way traverse arrays.

I would do something like this:

public string[] TakeEvery15th(string[] source)
{
    string[] every15th = new string[source.Length / 15];

    for (int i = 15, j = 0; i < source.Length; i += 15, j++)
    {
        every15th[j] = source[i];
    }

    return every15th;
}

Notice in particular the double counters inside the for-loop. The variable i causes the counter to jump 15 steps at a time in the source array, while the variable j sticks those values in 0, 1, 2... in the destination array.

more ▼

answered Jul 21 '11 at 09:28 AM

CHPedersen gravatar image

CHPedersen
6k 13 22 61

looking good and logic, could've slapped myself for not doing that.

also, are you sure this is javascript?

Jul 21 '11 at 09:41 AM biohazard

I have replaced int[] with string[] to reflect the fact that your numbers are in string format. If you need the strings converted to a number before you return, you can use a parse method, i.e. int.Parse(string) or double.Parse(string), or whatever datatype those numbers are.

This is C#. A translation to javascript should be fairly straight-forward. ;)

Jul 21 '11 at 09:42 AM CHPedersen

translation is going bad, he won't accept the multiple counters in the loop. all the rest is successfully translated

Jul 21 '11 at 09:46 AM biohazard

I'm not familiar with Unity's javascript syntax, so it might be that unlike C#, it doesn't support multiple counters inside for-loops. :/ In that case, you might have to take j out of the for-loop declaration, and increment it manually, like this:

int j = 0; for (int i = 15; i < source.Length; i += 15) { every15th[j] = source[i]; j++; }

Jul 21 '11 at 09:50 AM CHPedersen

loop is starting but doesn't fill the every15th array at all...is there any other solution?

Jul 21 '11 at 09:56 AM biohazard
(comments are locked)
10|3000 characters needed characters left

Something like this should work if you need it in javascript

// assume "source" exists, and is your original array
// "dest" will be your new array.

var dest = [];
var numItems = Math.floor(source.length / 15);

for (var i = 0; i < numItems; ++i) {
    dest.push(source[i * 15]);
}
more ▼

answered Jul 21 '11 at 11:57 AM

rejj gravatar image

rejj
174 1 1 8

Array modification is quite expensive, and as-per the C#, not necessary since the array size is calculable in advance.

Jul 21 '11 at 12:45 PM Waz
(comments are locked)
10|3000 characters needed characters left

Spam - Removed by Almo

more ▼

answered Jul 21 '11 at 12:46 PM

gergerr (suspended)

thanks for the helpful answer? :D :D

Jul 21 '11 at 12:48 PM biohazard
(comments are locked)
10|3000 characters needed characters left

Spam - Removed by Almo

more ▼

answered Jul 21 '11 at 12:51 PM

gergerr (suspended)
(comments are locked)
10|3000 characters needed characters left

[I SHOULD BE SHOT FOR BEING A SPAMBOT]

more ▼

answered Jul 21 '11 at 12:15 PM

gergerr (suspended)

thanks for the helpful answer? :D :D

Jul 21 '11 at 12:48 PM biohazard
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3740
x3461
x1363
x80
x43

asked: Jul 21 '11 at 09:14 AM

Seen: 576 times

Last Updated: Jul 21 '11 at 01:16 PM