newruby
newruby

Reputation: 67

Convert a pipe delimited file into a hash?

I'm using Ruby 1.9.3 and have two questions:

(1) How do I convert a text file which has multiple data fields separated by pipe into a hash?:

name | student-id | maths | english | science | grade | remarks   | 
abc  |  10001     |  90   | 80      | 80      | A+    | excellent |
.
.
.

(2) Additionally, if I have a new column which is another hash:

name | student-id | maths | english | science | grade | remarks   | options     |
abc  |  10001     |  90   | 80      | 80      | A+    | excellent | <condition> |
.
.
.

Where, | Options | is another hash of:

| Stream      | Average   |
| Com Science |  90       |
| Elec Eng    |  85       |
| Mech Eng    |  80       |
.
.
.

I want to list all the students who qualify for all the different streams based on their average scores.

Upvotes: 1

Views: 1583

Answers (1)

the Tin Man
the Tin Man

Reputation: 160551

To start, use Ruby's built-in CSV class. Specify the :col_sep value as '|' to override the default value of ','. Both CSV.new and CSV.open allow you to specify that option.

Upvotes: 3

Related Questions