sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Help with arrays

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 3 Posters 238 Views
    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.
    • PixeroP Offline
      Pixero
      last edited by

      I need to test each elements in one array (test_array) and if the test is true
      put elements from another array (array) in certain indexes of a new array (new_array).
      If that index is already "occupied" the new element should overwrite the current.
      How do I do it?
      Here is some pseudo code to explain what I want:

      	# Three arrays
      	# test_array
      	# array
      	# new_array
      
      	test_array.each { |i|
      		if test1==true
      			new_array[0] = array[i]
      		end
      		if test2==true
      			new_array[1] = array[i]
      		end
      		if test3==true
      			new_array[2] = array[i]
      		end
      		if test4==true
      			new_array[3] = array[i]
      		end
      	}
      
      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Your code is almost there.

        There is the .each_with_index method that may be more readable.
        test_array.each_with_index {|e,i|

        A question... does the data array contain beforehand, the same number of elements, as the new_array.

        If not, you need to use a hash with integer keys, instead of an array as the ouptut data.

        I'm not here much anymore.

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

          Jan, what platform are you on, PC or Mac?

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • PixeroP Offline
            Pixero
            last edited by

            PC.
            Thanks for your answer. Is my way of putting the element from array into new_array correct?
            What happens if a index is occupied? Is that element moved to the next index or simply overwritten?
            (new_array is empty, you could say I sort array into new_array.)

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

              The way I'd do it is probably not the proper way... but it'd work...

              ` tarray=[11, 22,33, 44]
              aarray=[1, 22, 333, 44]
              narray=Array.new(tarray.length)
              p narray

              [nil, nil, nil, nil]
              tarray.length.times{|i| narray[i]=tarray[i] if tarray[i]==aarray[i] }
              p narray
              aarray=[nil, 22, nil, 44]`

              This works if you want to match exact array entries BUT use ' array.include?(value)' if you simply want to find the entry anywhere in the arrays

              TIG

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

                @pixero said:

                Is my way of putting the element from array into new_array correct?

                No not with .each, but possibly using .each_with_index

                @pixero said:

                What happens if a index is occupied? Is that element moved to the next index or simply overwritten?

                It is always overwritten when you use the [=] assigment method, and the index is occupied.

                @pixero said:

                (new_array is empty, you could say I sort array into new_array.)

                The [=] assigment method will automatically insert nil (blank,) index members if you specify an index greater than those in the array.
                If the array is empty, and you do a[3='Jan'], the array will be [nil,nil,nil,'Jan']
                if the array was ['Bill','Dan'] and you do a[4='Jan'], the array will be ['Bill','Dan',nil,nil,'Jan']

                I'm not here much anymore.

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

                  @pixero said:

                  ..., you could say I sort array into new_array.

                  IF you REALLY want to sort to a new array, use new_array = old_array.sort

                  IF you wish to sort an array itself, use old_array.sort!
                  (notice the exclamation point.)

                  I'm not here much anymore.

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

                    @pixero said:

                    PC.

                    Do you have the Ruby Core Reference CHM (Windows Compiled Help HTML,) for Ruby 1.8.6 ??
                    If not download it from my post in the Ruby Resources topic

                    Notice at the top of each class or module, the Mixin Modules are listed.

                    Array class has Enumerable mixed-in, so it inherits all those methods.

                    I'm not here much anymore.

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

                      If you could tell us what kind of test your doing.. it would help us choose the proper Enumerable method for you.
                      I'm thinking probably the .collect method is perhaps what you want.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • PixeroP Offline
                        Pixero
                        last edited by

                        Its actually the continuation of this thread: http://forums.sketchucation.com/viewtopic.php?f=180&t=33862

                        I have to digest this and try what you recommended before getting back.

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

                          @pixero said:

                          Its actually the continuation of this thread: [url]=ttp://forums.sketchucation.com/viewtopic.php?f=180&t=33862]How to compare points on a plane?[/url]

                          OK.

                          So the data_array contains Geom::Point3d objects?
                          What kind of object does the test_array contain?

                          What is the logical condition that you want the data_array elements to be added to the new_array?
                          If a false results from the logical test, is putting nil into the new_array OK, or do you wish some other value (perhap [0,0,0] or whatever) ?

                          We need to be careful when comparing Geom::Point3d objects, as Google overrode the .< and .== methods (to make them dependant upon the 0.001" internal tolerance.) You should decide if that's OK for your purposes.

                          I'm not here much anymore.

                          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