# Newbie problem with modifying array correctly

**URL:** <https://www.ruby-forum.com/t/newbie-problem-with-modifying-array-correctly/84930>\
**Category:** Ruby\
**Created:** [February 8, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/newbie-problem-with-modifying-array-correctly/84930 "2007-02-08T18:37:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 8, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/newbie-problem-with-modifying-array-correctly/84930/1 "2007-02-08T18:37:15Z")

</div>

Hello,

I have an array with subarrays containing integers, and want to make  
sure that the distance between any two numbers next to each other is  
equal to or less than 7, also from one subarray to the next. Rigth now I  
have two blocks to do this, one to check each element of a subarray  
against the previous element, and one to check the last element of a  
subarray against the first element in the next subarray. The problem is  
that whichever block comes last mess up the work done by the first  
block.

Here is an example with an array that is similar to the one my program  
will generate:

[http://pastie.caboo.se/38816](http://pastie.caboo.se/38816)

Here is a smaller example to make it easier to understand:

[http://pastie.caboo.se/38822](http://pastie.caboo.se/38822)

If you’re not familiar with Pastie, you can click “View” on the right to  
see a wrapped version (at least in Safari).

Any help is much appreciated!

PS. Using flatten is really not an option (as far as my newbie eyes can  
see) because when modifying a “bad” integer I can’t just choose any  
number, but need a number from the corresponding skala\_rekke-subarray  
(sorry about the norwegian variable-name). 🙂

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 8, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/newbie-problem-with-modifying-array-correctly/84930/2 "2007-02-08T19:51:55Z")

</div>

On Feb 8, 12:37 pm, Marco G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here is an example with an array that is similar to the one my program  
> will generate:
> 
> [Parked at Loopia](http://pastie.caboo.se/38816)
> 
> Here is a smaller example to make it easier to understand:
> 
> [Parked at Loopia](http://pastie.caboo.se/38822)

HI Marco

I believe the problem is you process each row separately in the first  
loop,  
then go back and process the first element of each row, which in some  
cases  
puts in a value that doesn’t fit in the row.

I modified your example a bit, it does the first row, then for each  
subsequent  
row it checks the first element before doing the rest of the row.  
Have a look at  
[http://pastie.caboo.se/38882](http://pastie.caboo.se/38882)

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 8, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/newbie-problem-with-modifying-array-correctly/84930/3 "2007-02-08T22:31:00Z")

</div>

Wow, thanks! That makes a lot of sense, now that I’ve spent half an hour  
understanding exactly what you were doing. 🙂 Your help is much  
appreciated, I’m learning a lot from it. It dawns on me that learning to  
program is much more about logic than memorizing methods.
