• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Read and Write (update) problem

Scheduled Pinned Locked Moved Developers' Forum
12 Posts 3 Posters 761 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 11 Jul 2014, 15:34

    hello.
    I have a Ruby code that I want to use to gather data but it is to slow;
    does anybody know why>?

    ContinueReading=0

    def reeed(ContinueReading)
    k=[]
    File.open("C:/TT/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
    • S Offline
      schizo2000
      last edited by 11 Jul 2014, 15:47

      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
      • T Offline
        TIG Moderator
        last edited by 11 Jul 2014, 15:54

        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 11 Jul 2014, 16:29

          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
          • D Offline
            Dan Rathbun
            last edited by 11 Jul 2014, 17:06

            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 11 Jul 2014, 17:18

              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
              • D Offline
                Dan Rathbun
                last edited by 11 Jul 2014, 17:28

                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 11 Jul 2014, 17:37

                  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 16 Jul 2014, 16:44

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

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 16 Jul 2014, 21:32

                      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 18 Jul 2014, 12:17

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

                        1 Reply Last reply Reply Quote 0
                        • S Offline
                          schizo2000
                          last edited by 20 Jul 2014, 15:35

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

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          1 / 1
                          • First post
                            1/12
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement