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

    Reading external files

    Scheduled Pinned Locked Moved Plugins
    8 Posts 3 Posters 502 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      chosen_file is a external file. I would like to search the chosen_file line by line for "XXXX", then save the next line in the data array. How do I increment the read file pointer to the next line?

      aFile = File.open(chosen_file, "r")
      Data = []
      #Search each line for "XXXX"
      aFile.each_line do |line|
        If line == "XXXX" #if found
          #How do I get next_line, to save to Data
          Data.push next_line
        end
      end
      aFile.close
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        aFile.read_line ?

        Thomas Thomassen โ€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          The following generates error message:
          undefined method โ€˜read_lineโ€™ for #<File:C:TEST.DAT>

          aFile = File.open(chosen_file, "r")
          aFile.each_line do |line|
              aFile.read_line
          end
          aFile.close
          
          1 Reply Last reply Reply Quote 0
          • Didier BurD Offline
            Didier Bur
            last edited by

            array_of_lines=IO.readlines(chosen_file)
            

            Will let you do what you want, line by line or not, no pointer, no nothing ๐Ÿ˜Ž

            DB

            1 Reply Last reply Reply Quote 0
            • honoluludesktopH Offline
              honoluludesktop
              last edited by

              Didier, Thanks. I will try that when I go to the office later today ๐Ÿ™‚

              1 Reply Last reply Reply Quote 0
              • honoluludesktopH Offline
                honoluludesktop
                last edited by

                Well, that created an array of the whole file. Can I just get the next (n) line? I can do it with a counter as attached, but the code gets complicated when searching for many different line descriptions.

                aFile = File.open(chosen_file, "r")
                SaveIt = []
                count = 0
                line_location = -1
                aFile.each_line do |line|
                  count = count + 1
                  if line == "xxxx"
                    #calculate the location of the next line
                    line_location = count + 1
                    #test if location and current line match
                    else if line_location == count
                       SaveIt.push line
                    end
                  end
                end
                aFile.close
                
                1 Reply Last reply Reply Quote 0
                • Didier BurD Offline
                  Didier Bur
                  last edited by

                  
                  .../...
                  array_of_lines=IO.readlines(chosen_file)
                  saveIt = []
                  skipNext=false
                  0.upto(array_of_lines.length-1) { |i|
                    if line[i] == "xxxx" and !skipNext
                      saveIt.push(array_of_lines[i+1])
                      skipNext=true
                    else
                      skipNext=false
                    end
                  }
                  .../...
                  
                  

                  DB

                  1 Reply Last reply Reply Quote 0
                  • honoluludesktopH Offline
                    honoluludesktop
                    last edited by

                    :-0....now I get it, thanks.

                    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