sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    If else

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 416 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      borg.stef
      last edited by

      Can the following be written in ruby? because it does not seem to work correctly

      if counter % 4 == 0
      			machine = "#{a[counter]"  	
      		elsif counter % 4 == 0.25
      			x = a[counter].to_f
      		elsif counter % 4 == 0.5
      			y = a[counter].to_f
      		elsif counter % 4 == 0.75
      			rot = a[counter].to_i
                      end
      
      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Your missing a closing "}" in the String assignment:

        machine = "#{a[counter]"

        should be:
        machine = "#{a[counter]}"

        You can also use:
        machine = a[counter].to_s

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • B Offline
          borg.stef
          last edited by

          yes that's ok because i mistyped here..but still not working. Can decimal points be used in elsif?

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            You can test if x==0.678 BUT your code [probably] never returns a decimal value ?????

            What is 'a' ? An array ??
            What is 'counter' - a float or an integer ?
            If it's an integer you'll never get a decimal result as the answer is always an integer ?
            Assuming counter is an integer as 0,1,2,3,4 etc
            if 'counter' is 4 then counter % 4 is 0
            But
            if 'counter' is 0 then counter % 4 is 0
            if 'counter' is 1 then counter % 4 is 1
            if 'counter' is 2 then counter % 4 is 1
            if 'counter' is 3 then counter % 4 is 3
            You will never get a decimal value like 0.5 unless counter ends with 0.5 !
            So you can't rely on this as you get the same % answer for 0,4,8,12 etc - unless that is acceptable in your coding ??
            To test these things simple type into the Ruby Console 0%4 and get 0, 4%4 and get 0 etc etc.., without making a complex test that does NOT give the results you 'assumed'...

            TIG

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @borg.stef said:

              Can the following be written in ruby? because it does not seem to work correctly

              Try this:

              if counter % 4.0 == 0.0
              	machine = "#{a[counter]}"  	
              elsif counter % 4.0 == 0.25
              	x = a[counter].to_f
              elsif counter % 4.0 == 0.5
              	y = a[counter].to_f
              elsif counter % 4.0 == 0.75
              	rot = a[counter].to_i
              end
              

              Or this:

              if counter / 4.0 == 0.0
              	machine = "#{a[counter]}"  	
              elsif counter / 4.0 == 0.25
              	x = a[counter].to_f
              elsif counter / 4.0 == 0.5
              	y = a[counter].to_f
              elsif counter / 4.0 == 0.75
              	rot = a[counter].to_i
              end
              

              You need to use a Float as the divisor to get a Float result.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • B Offline
                borg.stef
                last edited by

                a = []
                	counter = 0
                	file = File.new("trial.txt", "r")
                	while (line = file.gets)
                		a[counter] = line
                
                		if counter % 4 == 0     # lines 0 , 4, 8 etc
                			machine = "#{a[counter]}" 
                		elsif counter % 4 == 0.25      # lines 1, 5, 9 etc
                			x = a[counter].to_f    
                		elsif counter % 4 == 0.5    # lines 2, 6, 10 etc
                      			y = a[counter].to_f    
                		elsif counter % 4 == 0.75     # lines 3, 7, 11 etc
                			rot = a[counter].to_i
                end
                end
                
                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  This should work:

                  a = []
                  counter = 0
                  file = File.new("trial.txt", "r")
                  while (line = file.gets)
                  	a[counter] = line
                  
                  	if counter % 4 == 0.0     # lines 0 , 4, 8 etc
                  		machine = "#{a[counter]}" 
                  	elsif counter % 4 == 1.0      # lines 1, 5, 9 etc
                  		x = a[counter].to_f    
                  	elsif counter % 4 == 2.0    # lines 2, 6, 10 etc
                        		y = a[counter].to_f    
                  	elsif counter % 4 == 3.0     # lines 3, 7, 11 etc
                  		rot = a[counter].to_i
                  	end
                  end
                  

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    You haven't understood what I said several posts back
                    elsif counter % 4 == 0.25 # lines 1, 5, 9 etc
                    will never return true as it returns '1' NOT '0.25'... to test this simply type 1%4 or 5%4 in the Ruby Console and you'll get '1' NOT '0.25' !
                    Also note how 1/4 will return 0 because dividing an integer by an integer gives an integer [and 5/4 gives 1 !]... BUT 1/4.0 gives 0.25 as you have divided an integer by a float and you get a float.
                    SO your test is elsif counter % 4 == 1 # lines 1, 5, 9 etc πŸ˜’

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      borg.stef
                      last edited by

                      hmmm this worked now...but I didn't understand why you did 1% 4 = 1.0 etc?

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        You may wish to use the 3rd line as:
                        file = IO.readlines("trial.txt")

                        .. then change the while loop to an array each iterator:
                        ` file.each_with_index do |line,counter|

                        code

                        end`

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @borg.stef said:

                          hmmm this worked now...but I didn't understand why you did 1% 4 = 1.0 etc?

                          Because I did what TIG said, I tested it at the console, and saw it returned 1,2,3, ...

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • B Offline
                            borg.stef
                            last edited by

                            ok sorry TIG i saw your post now 😳

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              @dan rathbun said:

                              You may wish to use the 3rd line as:
                              file = IO.readlines("trial.txt")

                              .. then change the while loop to an array each iterator:
                              ` file.each_with_index do |line,counter|

                              code

                              end`

                              If you do this, you do not need a = [], because the var file is the array ( IO.readlines returns an array of textlines.)

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • B Offline
                                borg.stef
                                last edited by

                                ok thanks both!!

                                1 Reply Last reply Reply Quote 0
                                • C Offline
                                  Cleverbeans
                                  last edited by

                                  From the looks of your code you're interested specifically in blocks of four lines, in which case you may gain some mileage out of the array.slice method by doing the following.

                                  
                                  for i in 0..a.length/4 #there are i blocks of four lines
                                      machine,x,y,rot = a.slice(i*4,4)
                                      #more code
                                  end
                                  
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • 1 / 1
                                  • First post
                                    Last post
                                  Buy SketchPlus
                                  Buy SUbD
                                  Buy WrapR
                                  Buy eBook
                                  Buy Modelur
                                  Buy Vertex Tools
                                  Buy SketchCuisine
                                  Buy FormFonts

                                  Advertisement