sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    If else

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 419 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

      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