sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Find a component definition in a list

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 195 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.
    • G Offline
      glro
      last edited by

      i select an object in the model
      If it is a component, the code gets its definition and puts it in a string named "ref"

      for e in selection
              if e.is_a? Sketchup;;ComponentInstance
                  ref = e.definition.name.to_s
              end 
      end
      

      then i want to compare the "ref" to strings in a list (an array?) called "references", and find the one that matches

      for z in (0..715) do
      		if references[z] == ref
      			puts "hello"
      		end
      end 
      

      I know there is one string among the 716 strings, that matches to "ref", but the program never says "hello", doesn't find it

      what am i missing? Is it a bug in Ruby?

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

        Define 'ref' OUTSIDE of the block iteration

        ref=nil
        for e in selection
          if e.is_a? Sketchup;;ComponentInstance
            ref = e.definition.name.to_s
          end
        end
        

        Some simpler code would be

        ref=selection.grep(Sketchup;;ComponentInstance)[0]
        return nil unless ref
        

        Then

        references.each{|r|
          if ref==r
            puts 'hello'
            break
          end
        }
        

        OR MUCH simpler

        puts 'hello' in references.include?(ref)
        

        Are you sure that the exact 'name' is in the array ?
        What is 'ref' ??

        TIG

        1 Reply Last reply Reply Quote 0
        • G Offline
          glro
          last edited by

          @glro said:

          what am i missing? Is it a bug in Ruby?

          sorry for asking the question, i found the answer...

          		for z in (0..715) do
          			#rstrip to suppress whitespaces
          			if references[z].rstrip == ref
          			puts "hello"
          			end #if
          		end #for z in
          

          i had whitespaces at the end of references[z]; to remove them:
          52 str.rstrip
          Returns a copy of str with trailing whitespace removed.

          ERROR: The request could not be satisfied

          favicon

          (www.tutorialspoint.com)

          funny how programming depends so much on very trivial details...

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            A less verbose version:.

            <span class="syntaxdefault">result&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">references</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find&nbsp;</span><span class="syntaxkeyword">{&nbsp;|</span><span class="syntaxdefault">string</span><span class="syntaxkeyword">|&nbsp;</span><span class="syntaxdefault">string</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">rstrip&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxdefault">ref&nbsp;</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">puts&nbsp;</span><span class="syntaxstring">'Hello'&nbsp;</span><span class="syntaxkeyword">if&nbsp;</span><span class="syntaxdefault">result</span>
            

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

            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