How to increase variable inside the while loop

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”]

    puts current-#{j}     --------------
$j +=1

end

So after this, i wanted to do like this

current-#{i} == current-#{j}

but that syntax is wrong …

can any one please help me out …

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.

Hi,

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.

On Wed, Nov 21, 2012 at 11:36 PM, Ferdous ara [email protected]
wrote:

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. :slight_smile:

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:

current_first = linecount.times.map {|i|
current_result[“#{server_name}-#{i + 1}”][“usable”]}

So after this, i wanted to do like this

current-#{i} == current-#{j}

Assuming we have current_first and current_second then you could just do

current_first == current_second

Kind regards

robert

HI thanks
at last i did like this

current=curl ................
current_result=JSON.parse(current)
remote_1=curl ...........
remote1_result=JSON.parse(remote_1)
remote_2=........................
remote2_result=JSON.parse(remote_2)

#puts "[INFO] Going Through All the #{linecount} pimps "
$i = 1
$num = linecount.to_i + 1
#$faulty_count = 0
while $i < $num do
var1= current_result[“servername-#$i”][“usable”]
var2= remote1_result[“servername-#$i”][“usable”]
var3= remote2_result[“servername-#$i”][“usable”]

    puts "#{var1}  #{var2}  #{var3}"

    end



$i +=1

end

So the output its like

324617621504 376666077184 378329620480
338000445440 396440872960 337314316288
403153707008 450874927104 399995699200
697021360128 285726838784 281589653504

So now what i am after is :

need to compare if one of the value is 60% less the rest of the two

697021360128 285726838784 281589653504

this is currently is going to out of my knowledge …

thanks for help

Am 22.11.2012 21:39, schrieb Ferdous ara:

697021360128 285726838784 281589653504

this is currently is going to out of my knowledge …

thanks for help

Ever heard of if-statements?

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.

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.

Am 22.11.2012 09:01, schrieb Robert K.:

#$faulty_count = 0
while $i < $num do
current-#{i} = current_result["#{server_name}-#$i"][“usable”]

     puts current-#{i}     --------------
 $i +=1

end

You have no reason to make i' andnum’ global variables, so don’t.

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:

current_first = linecount.times.map {|i|
current_result["#{server_name}-#{i + 1}"][“usable”]}

Probably easier to grasp for a beginner:

current_first = []
linecount.times do |i|
current_first << current_result["#{server_name}-#{i + 1}"][“usable”]
end

if var1 > (var2 + var3)*0.6
#do something
elsif var2 > (var1 + var3)*0.6
#do something else
elsif var3 > (var2 + var1)*0.6
#do something else again
end

Am 22.11.2012 22:41, schrieb Joel P.:

if var1 > (var2 + var3)*0.6
#do something
elsif var2 > (var1 + var3)*0.6
#do something else
elsif var3 > (var2 + var1)*0.6
#do something else again
end

I’m not sure whether he wanted to compare with the sum of
the other two values.

@ Ferdous ara:

If you want to sort some values, use an array and
the method `sort’ (surprise!):

sorted_list = [4, 6, 2].sort # => [2, 4, 6]

There are also methods to get the minimum or maximum of an array.

ok i got work way round

     array2= array.sort.reverse
    p array2
    puts array2[0]
    puts array2[2]

its giving me appropriate result …

let me finished rest,… let see

Hi Thanks
yes, i was thinking of using aRRAY

array = Array.new
array.push var1.to_i
array.push var2.to_i
array.push var3.to_i
p array.sort.reverse
puts (array[0])
puts (array[2])

but problem is , array[0] should get the value of after sorting, but it
does not have a look

[378329643008, 376666247168, 324617576448]
324617576448
378329643008
[396440922112, 338000334848, 337314353152]
338000334848
337314353152

if i can just compare first value 378329643008 and the last value
324617490432

and can get 60% difference , that will be fine .

i know how to get 60% difference. but dont understand, , after shorting
how to get
the correct value.

Thanks