Increment outerloop value in innerloop in each do Syntax

Hi All,

I am new to RUBY. Please help me in solving the below problem.
I have a c code and I am trying to do the same in RUBY.

   for(int i = 0; i < 10;i++)
   {
      for(int j = 0; j< 5; j++)
      {
         if(i == j)
         {
            printf("%d - its J value\n",j);
            i++;
         }
      }
      printf("%d - its I value\n",i);
   }

Above is my C code. The output of above code is:

0 - its J value
1 - its J value
2 - its J value
3 - its J value
4 - its J value
5 - its I value
6 - its I value
7 - its I value
8 - its I value
9 - its I value

I want to do the same in RUBY. I have written following script

(0..9).each do |i|
   (0..5).each do |j|
      if( i == j)
         puts "#{j} J Value"
      end
   end
   puts "#{i} I Value"
end

But its not giving the desired output. Please suggest me how to achieve
the above output using RUBY script.

Thanks in advance.

Regards,
Nag.

···

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

Nag Raj wrote:

Hi All,

I am new to RUBY. Please help me in solving the below problem.
I have a c code and I am trying to do the same in RUBY.

   for(int i = 0; i < 10;i++)
   {
      for(int j = 0; j< 5; j++)
      {
         if(i == j)
         {
            printf("%d - its J value\n",j);
            i++;
         }
      }
      printf("%d - its I value\n",i);
   }

Above is my C code. The output of above code is:

0 - its J value
1 - its J value
2 - its J value
3 - its J value
4 - its J value
5 - its I value
6 - its I value
7 - its I value
8 - its I value
9 - its I value

I want to do the same in RUBY. I have written following script

(0..9).each do |i|
   (0..5).each do |j|
      if( i == j)
         puts "#{j} J Value"
      end
   end
   puts "#{i} I Value"
end

But its not giving the desired output. Please suggest me how to achieve
the above output using RUBY script.

I don't think you can use each on the i value. I would use a while loop to check for the value of i.

···

Thanks in advance.

Regards,
Nag.

Hi All,

I am new to RUBY. Please help me in solving the below problem.
I have a c code and I am trying to do the same in RUBY.

  for(int i = 0; i < 10;i++)
  {
     for(int j = 0; j< 5; j++)
     {
        if(i == j)
        {
           printf("%d - its J value\n",j);
           i++;
        }
     }
     printf("%d - its I value\n",i);
  }

Wow, is there any use case for this pattern?

I want to do the same in RUBY. I have written following script

(0..9).each do |i|
  (0..5).each do |j|
     if( i == j)
        puts "#{j} J Value"
     end
  end
  puts "#{i} I Value"
end

Be aware that i or j are in no way the same as i and j in
your C program. Here, you iterate over the set of natural
numbers from 0..9 and 0..5, binding i and j to those values
for every step of the iteration. So i and j are not controlling
your iteration - each does.
So any change to i and j will be lost at the end of the do-block.

But, with a bit of trickery, you can achieve this:

   (0..9).each do |i|
     if j = (0..5).find{ |j| i==j }
       puts "#{j} J Value"
     else
       puts "#{i} I Value"
     end
   end

#find finds the first element of the range that satisfies
the condition given in the block. If there is none, it returns
nil, which is not true in a boolean sense. As the statement in
the condition can be any ruby expression (because the always return
a value, there is no void), we can immediately assign that to j.

As j has a true value (everything but nil and false) if the find
condition matches, the first branch of the if is used. Otherwise,
j is nil and i is printed.

Again: you iterate over a set, this is no counting loop. This might
take a bit to get used to, but in the end, you can use such nice
things as #find, #map, #inject on it. Learn them, they are a rubyists
mightiest tools.

Have a lot of fun.

Regards,
Florian Gilcher

···

On Dec 2, 2009, at 9:28 PM, Nag Raj wrote:

Michael W. Ryder wrote:

Nag Raj wrote:

         {
1 - its J value

the above output using RUBY script.

I don't think you can use each on the i value. I would use a while loop
to check for the value of i.

Hi Michael,

I have to get it done with each do syntax. Because I have a structure
type tag in xml file and I need to move onto next element if my
comparision goes fine.

Please help me in this. Thanks in advance.

Regards,
Nag.

···

--
Posted via http://www.ruby-forum.com/\.

Florian Gilcher wrote:

        {
           printf("%d - its J value\n",j);
           i++;
        }
     }
     printf("%d - its I value\n",i);
  }

Wow, is there any use case for this pattern?

end

Be aware that i or j are in no way the same as i and j in
your C program. Here, you iterate over the set of natural
numbers from 0..9 and 0..5, binding i and j to those values
for every step of the iteration. So i and j are not controlling
your iteration - each does.
So any change to i and j will be lost at the end of the do-block.

But, with a bit of trickery, you can achieve this:

   (0..9).each do |i|
     if j = (0..5).find{ |j| i==j }
       puts "#{j} J Value"
     else
       puts "#{i} I Value"
     end
   end

#find finds the first element of the range that satisfies
the condition given in the block. If there is none, it returns
nil, which is not true in a boolean sense. As the statement in
the condition can be any ruby expression (because the always return
a value, there is no void), we can immediately assign that to j.

As j has a true value (everything but nil and false) if the find
condition matches, the first branch of the if is used. Otherwise,
j is nil and i is printed.

Again: you iterate over a set, this is no counting loop. This might
take a bit to get used to, but in the end, you can use such nice
things as #find, #map, #inject on it. Learn them, they are a rubyists
mightiest tools.

Have a lot of fun.

Regards,
Florian Gilcher

Hi All,

Thank you very much Michael and Florian for your help. I am sorry John
if I made something wrong. I tried with continue, break and next.
Nothing worked out for me. Unluckily I have to insert some code
according to this pattern. Because remaining file maintained this format
so I have to. At last after some manipulation, I got the below code
which works similar to the above codes given by Michael and Florian.
Once again thank you very much for the help.

myval = true
(0..9).each do |i|
   (0..5).each do |j|
      if(i==j)
         puts "#{j} its J value"
         myval = false
      end
   end
   if(myval)
      puts "#{i} its I value"
   end
   myval = true
end

Regards,
Nag

···

On Dec 2, 2009, at 9:28 PM, Nag Raj wrote:

--
Posted via http://www.ruby-forum.com/\.

Nag Raj wrote:

Michael W. Ryder wrote:

Nag Raj wrote:

         {
1 - its J value

the above output using RUBY script.

I don't think you can use each on the i value. I would use a while loop
to check for the value of i.

Hi Michael,

I have to get it done with each do syntax. Because I have a structure type tag in xml file and I need to move onto next element if my comparision goes fine.

The following outputs the same results as your C++ code:

i = 0
while (i < 10)
   (0..5).each do |j|
     if (i == j)
       puts "#{j} its J Value"
       i += 1
     end
   end
   puts "#{i} its I Value"
   i += 1
end

0 its J Value
1 its J Value
2 its J Value
3 its J Value
4 its J Value
5 its J Value
6 its I Value
7 its I Value
8 its I Value
9 its I Value

If this isn't what you are looking for I don't understand what you are trying to do.

···

Please help me in this. Thanks in advance.

Regards,
Nag.

Good Day Nag,

I have to get it done with each do syntax. Because I have a structure
type tag in xml file and I need to move onto next element if my
comparision goes fine.

If you would like assistance please don't try to outsmart us. If you have a
specific issue (and apparantly this double loop is not your issue) please
just discuss the issue and not try to make it pretty or such.

Ruby isn't like C in many respects to looping and you probably are not
approaching your actual problem in a very "Rubyesque" method by doing it a C
style loop.

Specifcally unless you need the element index for some reason - you will
find many Ruby idioms never use an index based loop but rather an "object"
based loop instead iterating over the actual objects you are intersted in.

Again, absent what you are specifically trying to accomplish - it's
difficult to assist.

John

···

On Wed, Dec 2, 2009 at 5:29 PM, Nag Raj <raju.srirangam@gmail.com> wrote: