# Add outside variable to array elements

**URL:** <https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776>\
**Category:** Ruby\
**Created:** [August 23, 2007, 4:18am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776 "2007-08-23T04:18:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 23, 2007, 4:18am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/1 "2007-08-23T04:18:47Z")

</div>

Hi,

I have all of six hours of programming experience, so I hope that  
youâ€™ll bear with me. Ain’t all that comfortable with terminology and  
such.

I opened a binary file called â€œ16bytesâ€ and read it into a string. Then  
I unpacked the string into an array of Short Integers. At least thatâ€™s  
what I assume that I did.

What Iâ€™d like to do is add an outside variable via the keyboard to each  
element in the array.

Okay, Iâ€™ll admit that I barely understood what Iâ€™ve written so far, but  
I think itâ€™s correct. Burst my bubble, if you like. It wonâ€™t hurt my  
feelings too much.

Hereâ€™s the code:

whole\_file = open(‘16bytes’, ‘rb’) { |f| f.read}  
ar1 = whole \_file.unpack(‘S\*’)

puts(‘Enter an increase value’)  
incVal=gets.chomp  
incVal.to\_i

ar2 = ar1.collect { |x| x + incVal}  
puts ar2

I assume the thing doesnâ€™t work because incVal is outside of the curly  
brace doo-dad. It wouldnâ€™t surprise me if thereâ€™s other problems, too.

Go nuts with the constructive criticism.

So how can a person add a user-entered number to each value in an array  
like this?

Thanks.

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 23, 2007, 4:34am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/2 "2007-08-23T04:34:42Z")

</div>

On 23 Aug 2007, at 11:18, Ignatz H. wrote:

> whole\_file = open(‘16bytes’, ‘rb’) { |f| f.read}  
> ar1 = whole \_file.unpack(‘S\*’)
> 
> puts(‘Enter an increase value’)  
> incVal=gets.chomp  
> incVal.to\_i

This should be incVal = incVal.to\_i. String#to\_i _returns_ the  
integer your String represents it does not convert the String object  
into an Integer.

> ar2 = ar1.collect { |x| x + incVal}  
> puts ar2

There are some typos like the space in ‘whole \_file’, but I assume  
those are just typos (use cut&paste). You don’t say what error(s) you  
get, so it is hard to say what your problem is apart from the  
incVal.to\_i part.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 23, 2007, 4:49am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/3 "2007-08-23T04:49:42Z")

</div>

From: Ignatz H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Go nuts with the constructive criticism.

rubyists don’t criticize, they just fade away 🙂

my simple advice: run your code a line at a time in irb

kind regards -botp

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 25, 2007, 1:03am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/4 "2007-08-25T01:03:47Z")

</div>

Thank you, Alex G…

incVal = incVal.to\_i worked just as you suspected it would.

And thank you, botp, for the irb suggestion.

My apologies for the typos, as well as for not supplying error messages.  
The computer I was using at the time didn’t have Ruby, so I had to rely  
on my faulty memory. The vodka probably didn’t help matters much.

I’ve done some more reading, and I’m a little embarrassed at having  
called a code block a “curly brace doo-dad.”

BTW, I couldnâ€™t find anything about cut&paste in the Core API  
documentation.

I have this code now:

whole\_file = open(‘16bytes’, ‘rb’) { |f| f.read}  
ar1 = whole\_file.unpack(‘S\*’)

puts(‘Enter an increase value’)  
incVal=gets.chomp  
incVal = incVal.to\_i

ar2 = ar1.collect { |x| x + incVal}

ar2= ar2.pack(‘S\*’)

open(‘output’,‘wb’){|f| f.write ar2}

But there’s a problem with this. If, for instance, ‘16bytes’ contained  
the Short 65535 and incVal=12, the result would be 65547. That’s too  
big to store as a Short. The 65547 wraps over to 11 when it gets saved  
in ‘output’.

Is there any way to iterate over an array and apply a conditional like  
if x \< 45000 then x = x + incVal  
rather than having everything in the array get incVal added to it? I  
doubt that Iâ€™ll ever have an incVal over 20000.

PS I was joking about cut&paste.

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 25, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/5 "2007-08-25T12:25:54Z")

</div>

PeÃ±a, Botp wrote:

> ```
> ar2 = ar1.collect { |x| x<45000 ? (x + incVal) : x}
> 
> ```
> 
> or  
> ar2 = ar1.collect { |x| if x\<45000 then (x + incVal) else x end}  
> or  
> ar2 = ar1.select{|x| x\<45000}.collect{|x| x + incVal}
> 
> i usually prefer the last one, since i think like: “select those  
> elements \< 45000, then with those collections, increment each”. but that  
> is just me.
> 
> welcome to ruby.  
> kind regards -botp

The last one is not equivalent to the other two. It will drop values \>=  
45000.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 26, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/6 "2007-08-26T17:54:07Z")

</div>

On 8/25/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > welcome to ruby.  
> > kind regards -botp
> 
> The last one is not equivalent to the other two. It will drop values \>=  
> 45000.

oops. mea culpa. what was i thinking again ☹ yes, pls ignore the  
last one. at least i may be right on the testing part :))  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 27, 2007, 2:15am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/7 "2007-08-27T02:15:22Z")

</div>

Thanks, guys.  
I’ll try to ask more intelligent questions in the future.

---

<div class="post-metadata">

**Author:** ![not\_pol\_pot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/90db22/32.png) [@not\_pol\_pot](https://www.ruby-forum.com/u/not_pol_pot)\
**Post date:** [August 25, 2007, 9:41am UTC](https://www.ruby-forum.com/t/add-outside-variable-to-array-elements/112776/8 "2007-08-25T09:41:42Z")

</div>

From: Ignatz H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# whole\_file = open(‘16bytes’, ‘rb’) { |f| f.read}

# ar1 = whole\_file.unpack(‘S\*’)

# puts(‘Enter an increase value’)

# incVal=gets.chomp

# incVal = incVal.to\_i

2 lines above can be merged like,

incVal = gets.to\_i

# ar2 = ar1.collect { |x| x + incVal}

# ar2= ar2.pack(‘S\*’)

# open(‘output’,‘wb’){|f| f.write ar2}

# 

# Is there any way to iterate over an array and apply a conditional like

# if x \< 45000 then x = x + incVal

refer back to the line containing

```
ar2 = ar1.collect { |x| x + incVal}

```

change it to any of the ff (pls test all)

```
ar2 = ar1.collect { |x| x<45000 ? (x + incVal) : x}

```

or  
ar2 = ar1.collect { |x| if x\<45000 then (x + incVal) else x end}  
or  
ar2 = ar1.select{|x| x\<45000}.collect{|x| x + incVal}

i usually prefer the last one, since i think like: “select those  
elements \< 45000, then with those collections, increment each”. but that  
is just me.

welcome to ruby.  
kind regards -botp
