multi-dimentional array

Discussion in 'Ruby' started by Joan Gu, Mar 25, 2008.

  1. Joan Gu

    Joan Gu Guest

    I need to insert Broad Category value and Topics value into a table,
    doing something like this (where 'Police, Crime, Drugs' are topics
    values and 'Crime & Law Enforcement' is broad category value):

    obj1 = 'Police, Crime, Drugs'
    obj1 = obj1.split(',').map do |tag_name|
    execute "insert into tags (name,counter) values
    ('#{tag_name.strip.downcase}', 0)"
    t = Tag.find_by_name(tag_name.strip.downcase)
    tt = t.tag_with('Crime & Law Enforcement')
    end

    When it comes with multiple broad categories and topics, I want to build
    an array in a form like
    [['Police, Crime, Drugs','Crime & Law Enforcement'],['Fire, Emergency
    Services','Emergency Management'], ['Schools, Colleges,
    Libraries','Education'],...]
    to iterate the executions. I worked for quite some hours, but still
    can't make it work. Can someone help?

    Joan
    --
    Posted via http://www.ruby-forum.com/.
    Joan Gu, Mar 25, 2008
    #1
    1. Advertising

  2. Hi --

    On Tue, 25 Mar 2008, Joan Gu wrote:

    > I need to insert Broad Category value and Topics value into a table,
    > doing something like this (where 'Police, Crime, Drugs' are topics
    > values and 'Crime & Law Enforcement' is broad category value):
    >
    > obj1 = 'Police, Crime, Drugs'
    > obj1 = obj1.split(',').map do |tag_name|
    > execute "insert into tags (name,counter) values
    > ('#{tag_name.strip.downcase}', 0)"
    > t = Tag.find_by_name(tag_name.strip.downcase)
    > tt = t.tag_with('Crime & Law Enforcement')
    > end
    >
    > When it comes with multiple broad categories and topics, I want to build
    > an array in a form like
    > [['Police, Crime, Drugs','Crime & Law Enforcement'],['Fire, Emergency
    > Services','Emergency Management'], ['Schools, Colleges,
    > Libraries','Education'],...]
    > to iterate the executions. I worked for quite some hours, but still
    > can't make it work. Can someone help?


    I think you want something like this:

    tagsets = [['Police, Crime, Drugs','Crime & Law Enforcement'],
    ['Fire, Emergency Services','Emergency Management'],
    ['Schools, Colleges, Libraries','Education']]

    tagsets.each do |tagset|
    broad, topic = tagset
    broads = broad.split(",").map {|s| s.strip }
    puts "Broad: #{broads.join("; ")}\n\tTopic: #{topic}" # etc.
    end


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    ADVANCING WITH RAILS April 14-17 New York City
    INTRO TO RAILS June 9-12 Berlin
    ADVANCING WITH RAILS June 16-19 Berlin
    See http://www.rubypal.com for details and updates!
    David A. Black, Mar 25, 2008
    #2
    1. Advertising

  3. Joan Gu wrote:
    > I need to insert Broad Category value and Topics value into a table,
    > doing something like this (where 'Police, Crime, Drugs' are topics
    > values and 'Crime & Law Enforcement' is broad category value):


    How about using a hash with arrays underneath?

    Something like:


    category_hash={}
    cat1 = 'Crime & Law Enforcement'
    cat2 = 'Emergency Management'
    obj1 = 'Police, Crime, Drugs'
    obj2 = 'Fire, Emergency Services'

    category_hash[cat1]= obj1.split(",").map {|s| s.strip}
    category_hash[cat2]= obj2.split(",").map {|s| s.strip}

    You can then add new items like this:

    category_hash[cat1] << 'New Crime related item'

    puts category_hash.inspect
    #=>{"Emergency Management"=>["Fire", "Emergency Services"], "Crime & Law
    Enforcement"=>["Police", "Crime", "Drugs", "New Crime related item"]}

    and you can then iterate through categories and within each category
    through the items.

    Mac
    --
    Posted via http://www.ruby-forum.com/.
    Paul Mckibbin, Mar 25, 2008
    #3
  4. Joan Gu

    Joan Gu Guest

    To Mac,

    Your solution sounds like a good approach, but may not fit in my
    situation, since I am actually writing Rails migration file, which is
    more like a 'one time deal'. :)

    Thanks for your help.
    Joan
    --
    Posted via http://www.ruby-forum.com/.
    Joan Gu, Mar 25, 2008
    #4
  5. Joan Gu

    Todd Benson Guest

    On Tue, Mar 25, 2008 at 9:48 AM, Joan Gu <> wrote:
    > I need to insert Broad Category value and Topics value into a table,
    > doing something like this (where 'Police, Crime, Drugs' are topics
    > values and 'Crime & Law Enforcement' is broad category value):
    >
    > obj1 = 'Police, Crime, Drugs'
    > obj1 = obj1.split(',').map do |tag_name|
    > execute "insert into tags (name,counter) values
    > ('#{tag_name.strip.downcase}', 0)"
    > t = Tag.find_by_name(tag_name.strip.downcase)
    > tt = t.tag_with('Crime & Law Enforcement')
    > end
    >
    > When it comes with multiple broad categories and topics, I want to build
    > an array in a form like
    > [['Police, Crime, Drugs','Crime & Law Enforcement'],['Fire, Emergency
    > Services','Emergency Management'], ['Schools, Colleges,
    > Libraries','Education'],...]
    > to iterate the executions. I worked for quite some hours, but still
    > can't make it work. Can someone help?
    >
    > Joan


    Can't help you with migrations, but what's your relationship model?
    Is it one to one and that's why you want an array like that? Is it
    one many (which is what I'd suspect)? Is it many to many?

    You did say "a" table. Somehow, that doesn't seem to correctly fit your model.

    Todd
    Todd Benson, Mar 25, 2008
    #5
  6. > Can't help you with migrations, but what's your relationship model?
    > Is it one to one and that's why you want an array like that? Is it
    > one many (which is what I'd suspect)? Is it many to many?
    >
    > You did say "a" table. Somehow, that doesn't seem to correctly fit your
    > model.
    >
    > Todd

    Seems to me that could even be n-n, since a category like "Police
    Academy" could fit into two topics. :)

    Mac
    --
    Posted via http://www.ruby-forum.com/.
    Paul Mckibbin, Mar 27, 2008
    #6
    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. Willem Oosthuizen
    Replies:
    1
    Views:
    2,776
    Jonathan Bromley
    Jul 9, 2003
  2. Michael Neuling
    Replies:
    4
    Views:
    2,897
    Michael Neuling
    Nov 27, 2003
  3. Abby
    Replies:
    2
    Views:
    601
  4. ranjmis
    Replies:
    14
    Views:
    513
    Ben C
    Mar 12, 2006
  5. NvrBst
    Replies:
    3
    Views:
    428
    NvrBst
    Nov 13, 2008
Loading...

Share This Page