Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by biohazard · Jul 21, 2011 at 09:14 AM · javascriptarraysplittextasset

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)?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biohazard · Jul 21, 2011 at 09:28 AM 0
Share

it's a String[]

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · Jul 21, 2011 at 09:28 AM

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biohazard · Jul 21, 2011 at 09:41 AM 0
Share

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

also, are you sure this is javascript?

avatar image CHPedersen · Jul 21, 2011 at 09:42 AM 0
Share

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. ;)

avatar image biohazard · Jul 21, 2011 at 09:46 AM 0
Share

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

avatar image CHPedersen · Jul 21, 2011 at 09:50 AM 0
Share

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++; }

avatar image biohazard · Jul 21, 2011 at 09:56 AM 0
Share

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

Show more comments
avatar image
0

Answer by rejj · Jul 21, 2011 at 11:57 AM

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]);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Jul 21, 2011 at 12:45 PM 0
Share

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

avatar image
0

Answer by gergerr · Jul 21, 2011 at 12:46 PM

Spam - Removed by Almo

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image biohazard · Jul 21, 2011 at 12:48 PM 0
Share

thanks for the helpful answer? :D :D

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple Array Problem 1 Answer

displaying playerprefs 1 Answer

TextAsset inside the Array - UnityScript/JavaScript 1 Answer

split() for separators more than 1 characters 2 Answers

Increasing And Decreasing An Array Through A Variable 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges