Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SomeRandomGuy · Sep 04, 2013 at 09:12 AM · arraysloopiterate

getting element i+x from an array, looping through it

So, lets say I've got an array(a builtin array, but that doesn't really matter for the question) and I want to assign some values to the gameobjects in them.

I get these values from another array, but the numbers may be shifted by x amount. So to assign the right values to the right objects I would need to shift the element I get by using something along the lines of:

array[i+x] = value[i];

Obviously doing something like this will leave the first x elements in array empty, and gives an index out of range on the last x elements.

What I need to do to fix this is to check if i+x is higher than the length of array, and if so i should be 0. I think.

Is there any elegant way of doing this?

Comment
Add comment · Show 7
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 gheeler · Sep 04, 2013 at 09:17 AM 1
Share

why would you want empty elements at the start of the array?

avatar image syclamoth · Sep 04, 2013 at 09:19 AM 3
Share
 array[i + x < array.Length ? i + x : 0]

If you want it to loop around, use

 array[(i + x) % array.Length]
avatar image DannyB · Sep 04, 2013 at 10:06 AM 0
Share

Adding to the previous comment, I also think what you are doing would create a technical debt you will find yourself paying again and again in bugs and maintenance. If the arrays should be parallel, then make sure their indexes align, if not, dont use arrays where possible - switch to lists or some other collection.

If you find yourself in need of help in regards to simple arrays, this should be a hint that there must be an easier way.

avatar image SomeRandomGuy · Sep 04, 2013 at 11:51 AM 0
Share

First of all, I don't want empty elements, I was trying to say, if I'd iterate through the array with a normal for loop, I'd start with i=0, but x will increase, meaning the for loop would skip the first x elements in the array. Also, I'm not entirely sure what the ? and : do in that piece of code, could you explain?

Second, the problem is that the second array is actually parallel to the first normally, but the values assigned to the first array should be shifted by x in a lot of cases. $$anonymous$$ight be easier to explain using a little image:

alt text

shiftingvisual.png (21.5 kB)
avatar image DannyB · Sep 04, 2013 at 11:56 AM 0
Share

? : is a shorthand for if else. a ? b : c is the same as if( a ) then b; else c;

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hoeloe · Sep 05, 2013 at 08:19 AM

Sounds like what you want is the modulus function: %. This takes the first value, and returns the remainder when divided by the second value, so, for example, 10%6 will give 4. I assume you want to wrap the elements round, so the last x values becomes the first x values, in which case this is the perfect solution - you simply use the same code, but taking the modulus of the length! So, instead of using array[i+x], you'd use array[(i+x)%array.Length]. This ensures that the value can never be equal to or greater than the length of the array, and if the given value (i+x) is, then it will wrap around, meaning that if i+x is equal to the array length, it will index into the array at 0, and if it is one greater than the array length, it will index in at 1, etc.

Comment
Add comment · Show 3 · 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 SomeRandomGuy · Sep 06, 2013 at 12:47 PM 0
Share

Had some trouble wrapping my head around this, and ended up using a bit of a roundabout way to do the same thing. this works flawlessly tho. So kudos to both you and syclamoth, as his comment seems to use pretty much the same method. Thanks!

avatar image vivecuervo7 · Oct 20, 2019 at 09:37 AM 0
Share

It might be worth adding the length of the array before perfor$$anonymous$$g the % function, this will allow the array to also cycle in reverse. I use something similar using an integer direction of either 1 or 0 to cycle through elements bidirectionally. I believe the snippet blow may not work as intended if x is less than -array.Length.

var a = array[(i + x + array.Length) % array.Length]

avatar image Bunny83 vivecuervo7 · Oct 20, 2019 at 10:38 AM 0
Share

You are right. The original code doesn't work as soon as x is less than 0. That's because the modulo operator in most program$$anonymous$$g languages do not work as the mathematical modulo. It preserves the sign so negative numbers will result in a negative result. So if the offset is say -3 you would get -3, -2 and -1 for the first 3 values (assu$$anonymous$$g i goes from 0 upwards). By adding one full range it all shifts into the positive range.


Though If you need a negative offset you can also just wrap the offset properly into the positive range. That would be the more secure way I guess. To get the modulo working for all negative numbers you would have to do this:

 ((i + x)%array.Length + array.Length) % array.Length

which looks crazy but always works.

(i + x)%array.Length always returns a value in the range -length+1 to length-1. Adding the length will always make the number positive. The second modulo will ensure to get the right result for positive numbers.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array strings/Loops problem 2 Answers

Iterate through Generic Dictionary? 3 Answers

iterate through array of objects with lists of objects 0 Answers

Using arrays and for loop with GetComponent error 1 Answer

How do I make a waypoint loop function using array? - when it gets to a waypoint, then move to next waypoint 1 Answer


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