Hi, my question might be confusing as its hard for me to make it clear,
what i want is , something like bellow
$i = 1
$num = linecount.to_i + 1
#$faulty_count = 0
while $i < $num do
current-#{i} = current_result["#{server_name}-#$i"]["usable"]
puts current-#{i} --------------
$i +=1
end
Basically ,
i want to store value of current_result["#{servername}-#$i"]["usable"]
in to a variable (incremental variable )
like current-#{i} , but the syntax is not right ?? what will be right
way to do this ??
End gloal is, i will have another loop like
$j = 1
$num = linecount.to_i + 1
#$faulty_count = 0
while $i < $num do
current-#{j} = current_result["#{server_name}-#$j"]["usable"]
In your wording, "current" is a variable, but it has nothing assigned to
it so it is nil.
If you want to show the current value of "i", then just write "puts i".
If you want to gather each line for some sort of comparison I'd
personally read each into an array with something like "my_array.push"
and then compare two arrays afterwards.
you cannot change "living" Ruby code like you try to do. That is, you
cannot just inject a #{i} somewhere and have the Ruby interpreter
process it as if the value was actually in the code.
If you want to dynamically create variables, you either need
metaprogramming or eval() with the latter always being the last resort
("eval() is evil"). For local variables, however, eval() is really the
only
possibility -- which already shows there's something wrong.
If you want to collect values, use arrays like Joel suggested. Don't use
variables with an index appended to them. That's extremely ugly to work
with and just unclean.
You should also note that "while" loops and manual counter arithmetic is
rather rare in Ruby. Unlike BASIC or C or something, Ruby is a high
level language that relies on more intelligent methods like "upto" or
"each" or "each_with_index". You rarely ever have to do this "i += 1"
stuff.
Hi, my question might be confusing as its hard for me to make it clear,
Chances are that if you understand what you want the solution will
come much easier.
what i want is , something like bellow
$i = 1
$num = linecount.to_i + 1
#$faulty_count = 0
while $i < $num do
current-#{i} = current_result["#{server_name}-#$i"]["usable"]
puts current-#{i} --------------
$i +=1
end
Basically ,
i want to store value of current_result["#{servername}-#$i"]["usable"]
in to a variable (incremental variable )
As others have said: store in an Array. I don't know what
current_result refers to but it looks like something like this could
work:
Hi Thanks ..
That extra end was copy past mistakes.
I Tryed to use if else but the logic is not entering
In my head.
How to compare 3 values ..
I was thinking to do sorting then some how comparing
But because of 3 values I'm confused.
Thanks for further advice.
Also:
- your while loop is very un-idiomatic
- do not use global variables
- the indentation is a mess
- you have an `end' too much, which should cause an infinite loop
As I told you already months ago, you finally should start to learn
the basics (now). You also should not ignore the advice you get here,
but instead try to use it to improve your programming skills.