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

Help with arrays

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 3 Posters 238 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.
  • D Offline
    Dan Rathbun
    last edited by 8 Jan 2011, 17:10

    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
    • D Offline
      Dan Rathbun
      last edited by 8 Jan 2011, 17:14

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

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • P Offline
        Pixero
        last edited by 8 Jan 2011, 17:18

        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
        • T Offline
          TIG Moderator
          last edited by 8 Jan 2011, 17:33

          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
          • D Offline
            Dan Rathbun
            last edited by 8 Jan 2011, 17:41

            @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
            • D Offline
              Dan Rathbun
              last edited by 8 Jan 2011, 17:51

              @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
              • D Offline
                Dan Rathbun
                last edited by 8 Jan 2011, 17:52

                @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
                • D Offline
                  Dan Rathbun
                  last edited by 8 Jan 2011, 17:55

                  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
                  • P Offline
                    Pixero
                    last edited by 8 Jan 2011, 18:29

                    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
                    • D Offline
                      Dan Rathbun
                      last edited by 8 Jan 2011, 18:54

                      @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
                      1 / 1
                      • First post
                        11/11
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement