1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Rep Power

    Simple hash question


    Regarding hashes;

    I'm trying to create a simple hash table. It has 8 students with 8 ID numbers, and 2 subjects as inputs. Each student has a unique ID number, and there are two subjects, and each student is in one or both of the subjects.

    I'd like to associate each subject with the ID numbers of the students taking the subject. So the first subject, say math, is associated with student numbers 1, 2, 3, and 4, and the second, english, is associated with 4, 5, 6, 7, 8. Must I type in the association for each student number, rather than point at the group?


    :math => [ 1, 2, 3, 4 ]

    rather than

    :math => 1, :math => 2, :math => 3, :math => 4

    I know the first example does work. But it uses an array around the ID numbers, which indexes each of the elements in that array (as arrays do). For this program, the order is arbitrary, so the indexing is uneccessary , thus wasting memory for nothing.

    Anyone have a solution for me?

  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    So you want a hash of hashes then? Something like this ought to do the trick for you:
    #!/usr/bin/env ruby
    subjects = {
             :math    => { 1 => nil, 2 => nil, 3 => nil, 4 => nil, }, 
             :english => { 4 => nil, 5 => nil, 6 => nil, 7 => nil, },
    subjects.keys.each do |subject|
        puts subject
        students = subjects[subject]
        students.keys.each do |student|
            puts "\t" + student.to_s
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Constant Limbo
    Rep Power
    Originally Posted by dg_83
    thus wasting memory for nothing.
    It is very unlikely that this should be a concern for you.
    It is also unlikely that there is a waste anywhere here. If you have to store 4 values then you need space for 4 values to be stored. How you arrange those 4 elements may differ but I doubt you are going to even be able to measure the difference in either the memory or speed.

    As a side note, your second example will overwrite the satellite data of the hash at each iteration:
    irb(main):001:0> h = {}
    => {}
    irb(main):002:0> h[:math] = 1
    => 1
    irb(main):003:0> h[:math] = 2
    => 2
    irb(main):004:0> h[:math] = 3
    => 3
    irb(main):005:0> h[:math] = 4
    => 4
    irb(main):006:0> p h
    => nil
    So in order to store all of the elements (in a non-destructive manner) you will need an array or - as Scorpions4ever pointed out - you can use a hash.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Rep Power
    Thanks for the replies. I think my first question was a little misguided - time to do some reading.

IMN logo majestic logo threadwatch logo seochat tools logo