[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

add outside variable to array elements

Ignatz Hafflegab

8/23/2007 2:19:00 AM

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.
--
Posted via http://www.ruby-....

6 Answers

Alex Gutteridge

8/23/2007 2:33:00 AM

0

On 23 Aug 2007, at 11:18, Ignatz Hafflegab 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 Gutteridge

Bioinformatics Center
Kyoto University



Peña, Botp

8/23/2007 2:49:00 AM

0

From: Ignatz Hafflegab [mailto:mcpeople@hotmail.com]
# 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

Ignatz Hafflegab

8/24/2007 11:04:00 PM

0

Thank you, Alex Gutteridge.

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.
--
Posted via http://www.ruby-....

Stefan Rusterholz

8/25/2007 10:26:00 AM

0

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
--
Posted via http://www.ruby-....

botp

8/26/2007 3:54:00 PM

0

On 8/25/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> 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.

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

Ignatz Hafflegab

8/27/2007 12:15:00 AM

0

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

--
Posted via http://www.ruby-....