Array of Pairs data structure?

Discussion in 'Ruby' started by Greg Willits, Oct 25, 2007.

  1. Greg Willits

    Greg Willits Guest

    In a language called Lasso I am used to a data structure of an array of
    pairs:

    $simpleArray = array( 'color', 'shape', 'size')

    $arrayofPairs = array( 'color' = 'red', 'shape' = 'trapezoid', 'size' =
    'small')

    $simpleMap = map( 'color' = 'red', 'shape' = 'trapezoid', 'size' =
    'small')

    The difference between arrayofPairs and simpleMap is that the map (hash)
    cannot gaurantee a specific order whereas the arrayofPairs does. It's an
    array, so order is preserved. It also has what effectively works just
    like hash keys. I can find, retrieve, and even sort based on the first
    element of the pair. Lasso has a primitive data type of a Pair.

    Does Ruby have a way to create this arrayofPairs? I don't see a data
    type like a "pair" in Ruby, so not sure what to insert into each array
    element to net this same data structure except I guess maybe an array of
    single key hashes?

    ideas? Thanks.

    -- gw
    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Willits, Oct 25, 2007
    #1
    1. Advertising

  2. Greg Willits

    Greg Willits Guest

    OK, nevermind, I think I found enough functionality to get me by.

    An array of arrays is sortable (an array of hashes is not).

    And, array.assoc provides a lookup of a specific "pair"

    y = [['bbb','beta'], ['ggg','gamma'], ['aaa','alpha']]

    puts y.assoc('ggg') # will return ['ggg', 'gamma]

    puts y.sort # actually works. Weeee.


    -- gw


    --
    Posted via http://www.ruby-forum.com/.
     
    Greg Willits, Oct 25, 2007
    #2
    1. Advertising

  3. Greg Willits

    ara.t.howard Guest

    On Oct 25, 2007, at 12:40 AM, Greg Willits wrote:

    > In a language called Lasso I am used to a data structure of an
    > array of
    > pairs:
    >
    > $simpleArray = array( 'color', 'shape', 'size')
    >
    > $arrayofPairs = array( 'color' = 'red', 'shape' = 'trapezoid',
    > 'size' =
    > 'small')
    >
    > $simpleMap = map( 'color' = 'red', 'shape' = 'trapezoid', 'size' =
    > 'small')
    >
    > The difference between arrayofPairs and simpleMap is that the map
    > (hash)
    > cannot gaurantee a specific order whereas the arrayofPairs does.
    > It's an
    > array, so order is preserved. It also has what effectively works just
    > like hash keys. I can find, retrieve, and even sort based on the first
    > element of the pair. Lasso has a primitive data type of a Pair.
    >
    > Does Ruby have a way to create this arrayofPairs? I don't see a data
    > type like a "pair" in Ruby, so not sure what to insert into each array
    > element to net this same data structure except I guess maybe an
    > array of
    > single key hashes?
    >
    > ideas? Thanks.


    use arrayfields.


    cfp:~ > cat a.rb
    require 'rubygems'
    require 'arrayfields' ### gem install arrayfields

    a = Arrayfields.new 'k', 'v', 'K', 'V'
    p a['k']
    p a['K']
    p a

    __END__

    docs @ http://codeforpeople.com/lib/ruby/arrayfields/
    arrayfields-4.5.0/README



    cfp:~ > ruby a.rb
    "v"
    "V"
    ["v", "V"]



    kind regards.

    a @ http://codeforpeople.com/
    --
    share your knowledge. it's a way to achieve immortality.
    h.h. the 14th dalai lama
     
    ara.t.howard, Oct 25, 2007
    #3
  4. On 10/25/07, Greg Willits <> wrote:
    > OK, nevermind, I think I found enough functionality to get me by.
    >
    > An array of arrays is sortable (an array of hashes is not).
    >
    > And, array.assoc provides a lookup of a specific "pair"
    >
    > y = [['bbb','beta'], ['ggg','gamma'], ['aaa','alpha']]
    >
    > puts y.assoc('ggg') # will return ['ggg', 'gamma]
    >
    > puts y.sort # actually works. Weeee.


    You might also consider using a hash, for access and produce the array
    when needed to sort. Consider:

    h = {"color" => "red", "shape" => "trapezoid", "size" => "small" }

    h.to_a # => [["size", "small"], ["shape", "trapezoid"], ["color", "red"]]

    h.to_a.sort # => [["color", "red"], ["shape", "trapezoid"], ["size", "small"]]

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Oct 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    308
    Jianli Shen
    Jun 4, 2005
  2. Replies:
    6
    Views:
    515
    bugbear
    Nov 17, 2006
  3. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    757
    Martin Ambuhl
    Oct 26, 2004
  4. puzzlecracker

    generating random array of pairs

    puzzlecracker, Jul 29, 2008, in forum: Java
    Replies:
    10
    Views:
    581
  5. Tagore
    Replies:
    2
    Views:
    728
    Jorgen Grahn
    Jun 2, 2010
Loading...

Share This Page