sketchucation logo sketchucation
    • Login
    ⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

    Read and Write (update) problem

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 3 Posters 802 Views 3 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.
    • S Offline
      schizo2000
      last edited by

      by the way you should replace ContinueReading with continuereading

      the code would be:
      continuereading=0

      def reeed(continuereading)
      k=[]
      File.open("C:/GHConnection/temp.txt", "r") do |f|
      f.each_line do |e|
      k<<e
      end
      end
      continuereading= k[0].to_i

      puts (k[0])
      return continuereading
      end

      while reeed(continuereading)==1
      reeed(continuereading)
      puts ('I am Reading the Source file')
      sleep(3)
      break if reeed(continuereading)==0
      end

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

        A simple way to read text from a file is this:

        txt = IO.read("C:/TT/temp.txt")

        which return it all as a string.

        To read individual lines use:

        lines = IO.readlines("C:/TT/temp.txt")

        You can then look at each line as needed...

        Remember to strip off the trailing "\n" thus:

        lines.each{|line| p line; line.chomp!; p line }

        TIG

        1 Reply Last reply Reply Quote 0
        • S Offline
          schizo2000
          last edited by

          thanks for your quick reply TIG.
          so the code would be sth like this:

          continuereading=0

          def reeed(continuereading)
          lines = IO.readlines("C:/GHConnection/temp.txt")
          return (lines[0]).to_i
          end

          while ((reeed (continuereading)))==1
          puts ('I am Reading the Source file')
          sleep(3)
          end

          it works fine but again it is 2 slow; all the characters in my text-file do not add up to 100 yet after iteration 3 the solution is practically worthless.

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

            NO use:

            lines = IO.readlines("C;/GHConnection/temp.txt").map{|l| l.chomp }
            
            puts( "Number of lines; #{lines.size}" )
            
            for line in lines
              puts(line)
              # do whatever else with each line here
            end
            
            

            Yes, using the sleep() with a 3 second argument will be slow !

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • S Offline
              schizo2000
              last edited by

              well, we can change the sleep time (or we can omit it) but still 3-times reading is the best I can get out of this.

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

                We are telling you that you should read the file ONCE, in it's ENTIRETY, into an array of lines.

                THEN read from the array of lines, when ever you need to.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • S Offline
                  schizo2000
                  last edited by

                  the problem is that I need the update-data in each cycle and the provider (the one that send the text files) is the one that decides when the reading action should stop. similar to web browsers when we want to get notified when sth changes.
                  I am using the sleep to make the action less frequent in the hope of achieving more/better iterations.
                  does that make sense? is there a better solution?

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    schizo2000
                    last edited by

                    Video of the process
                    https://www.youtube.com/watch?v=EKKCQHfAUqk

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

                      Issue 1:
                      When your code [somehow] updates the model's contents if needs to only affect the relevant instances.
                      How are you getting ALL instances ?
                      How are you identifying your 'linked' instance definitions ?
                      It should be straightforward to separate the two 'types'...

                      Issue 2:
                      Can you not set up a timer to poll the linked data ?
                      So instead of the user clicking on a button and sending an update 'request', you automate it on a click/and stop it on a click...
                      mytimer=UI.start_timer(1, true){ do_'reqeust'_every_second__BUT_when_@stopped_do(UI.stop_timer(mytimer)) }
                      That way it runs all of the time and refreshes the linked data every second...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • S Offline
                        schizo2000
                        last edited by

                        special thanks to you TIG, the timer was genius idea .

                        1 Reply Last reply Reply Quote 0
                        • S Offline
                          schizo2000
                          last edited by

                          Thanks to every one here (and also Thomas Thomassen) the work is now finished!

                          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