sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Reading external files

    Scheduled Pinned Locked Moved Plugins
    8 Posts 3 Posters 521 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