Thank you to everyone for all of your help.
I was trying to use upto to go through an array of arrays while using if
statments. Could anyone enlighten me on what I am doing wrong with the
if statements?
I am sure it is something easy.
I tried this:
0.upto( a.size-2 ) {|i|
if (a[i][2]==a[i+1][2] and a[i][0]==a[i+1][0]) then
print { (a[i+1][3] - a[i][3]) / a[i][3] * 100 } /n
else print 0
end}
Why don't you give some sample data, and show what happens when you run it.
Preferably make a small, standalone program which demonstrates the problem.
Do you get an exception raised? A wrong result?
Looking at the above, the fact that you have used curly brackets in the
'print' statement certainly won't help... this creates a code block and will
give you a confusing error like this:
irb(main):003:0> print { 4 + 5 } / 3
nilNoMethodError: undefined method `/' for nil:NilClass
from (irb):3
This is equivalent to:
print( { 4+5 } ) / 3
^ ^ ^
> > >
> > value returned by
> > print() is nil
> >
> a code block
> which is not used
>
no arguments
to print
BTW you probably want to use puts rather than print, so your numbers are on
separate lines; or at least print a space between them.
Your above code does also highlight a skeleton in Ruby's closet: note that
puts (4+5)/3
puts(4+5)/3
are two different constructs, even though they differ only by one space.
They are equivalent to, respectively,
puts( (4+5)/3 ) # probably what you want
( puts(4+5) ) / 3 # prints 9, tries to divide nil by 3
B.
···
On Mon, Feb 12, 2007 at 08:05:01PM +0900, Michael Sc wrote:
from :0