Dynamically create an array

Hi Experts,

I am trying to create an array from hash

example in my hash I have value like

grades =
{ { "subject" => english,
           "Jim" => 94,
           "Billy" => 58
         }

   { "subject" => math,
           "Jim" => 90,
           "Billy" => 80
         }

    { "subject" => science,
           "Jim" => 99,
           "Billy" => 58
         }

}

now i want to make an array or output like this

94,90,99
58,80,58

i am looking for some help in google but no luck

Robert

···

--
Posted via http://www.ruby-forum.com/.

Hi,

that "hash" is nowhere near a valid Ruby expression. If this is supposed
to be an array of hashes (with commas between the elements), use
Array#map:

jims_grades = grades.map {|grade| grade['Jim']}
p jims_grades

···

--
Posted via http://www.ruby-forum.com/.

This code is not a valid hash in ruby 1.9.3.
However if I unterstood what you are trying to ask, I would take a look at
collect method for your array.

in which may return an array of the return of a given block.

e.g.

grade = {"subject"=>:math, "Jim"=>94, "Billy"=>50}
grade.collect { |key, value| value } # [:math 94, 50]
grade.collect { |key, value| value unless key == 'subject' } # [nil, 94,
50]

later you can use reject method of Array to remove the nil values.

···

2012/12/10 robert lengu <lists@ruby-forum.com>

Hi Experts,

I am trying to create an array from hash

example in my hash I have value like

grades =
{ { "subject" => english,
           "Jim" => 94,
           "Billy" => 58
         }

   { "subject" => math,
           "Jim" => 90,
           "Billy" => 80
         }

    { "subject" => science,
           "Jim" => 99,
           "Billy" => 58
         }

}

now i want to make an array or output like this

94,90,99
58,80,58

i am looking for some help in google but no luck

Robert

--
Posted via http://www.ruby-forum.com/\.

--
Pedro Henrique de Souza Medeiros
----------------------------------
Cel: +55 (61) 9197-0993
Email: pedrosnk@gmail.com

Beautiful is better than ugly,
Explicit is better than implicit,
Simple is better than complex,
Complex is better than complicated.

The Zen of Python, by Tim Peters

grades = [
  { "subject" => "english",
    "Jim" => 94,
    "Billy" => 58
  },

  { "subject" => "math",
    "Jim" => 90,
    "Billy" => 80
  },

  { "subject" => "science",
     "Jim" => 99,
     "Billy" => 58
  },

]

jim = []
billy = []

grades.each do |hash|
  jim << hash['Jim']
  billy << hash['Billy']
end

p jim
p billy

--output:--
[94, 90, 99]
[58, 80, 58]

···

--
Posted via http://www.ruby-forum.com/.

In this type of case I'd strongly suggest looking into making a class for
subject, it saves quite a bit of time in the long run versus a hacked
together hash. As more grades and subjects are added the Hash will become
slower and more confounded to use.

I also don't entirely understand the reasoning behind your layout. Subject
should not be on the same level with the student/grade pair. It doesn't
make sense in that manner. If we put it in bullet points it's a little
clearer:

*BAD*

   - Grades
      - Subject
      - Student
         - Grade

*GOOD*

···

*
*

   - Grades
      - Subject
         - Students

Again, the current hierarchy makes no sense. and it would be far easier to
manipulate with a clearer structure that cleanly separates different types
of data.

The basic frame for a class would be: Grades contain many subjects, each
with several students that all have one grade. This can easily change of
course, but with the one grade system it works.

On Mon, Dec 10, 2012 at 4:58 PM, 7stud -- <lists@ruby-forum.com> wrote:

grades = [
  { "subject" => "english",
    "Jim" => 94,
    "Billy" => 58
  },

  { "subject" => "math",
    "Jim" => 90,
    "Billy" => 80
  },

  { "subject" => "science",
     "Jim" => 99,
     "Billy" => 58
  },

]

jim =
billy =

grades.each do |hash|
  jim << hash['Jim']
  billy << hash['Billy']
end

p jim
p billy

--output:--
[94, 90, 99]
[58, 80, 58]

--
Posted via http://www.ruby-forum.com/\.