0

I'm new to programming. Is there a way for Ruby to select the conditions for column 1 and 2, while I print columns 0 and 3 (In the context of the same row)

Take for example I would like to print out the "description" and "equipment" when there is an event type of "high" and severity "1".

enter image description here

I could not print out my equipment and description

require "csv"
equipment = []
description = []
def find_severity(event_type, severity)
    alarms = CSV.read 'alarms.csv', {col_sep: ';'}
    result = alarms.select do |level|
          level[1] == event_type && level[2].to_i == severity.to_i
          equipment[] = level[0]
          description[] = level[3]
    end
end

p find_severity('high', 1)
p equipment
p description
7
  • "equipment[] = level[10]" - no, this is not php. Here it's done differently: equipment.push(level[10]). And these should be outside the select, I think. Commented Feb 13, 2018 at 7:58
  • Also, level[10] and level[13]? What did you mean by that? You only have 4 columns. Commented Feb 13, 2018 at 7:59
  • Ah, sorry I changed the values. Commented Feb 13, 2018 at 8:06
  • The issue is that I would like to check if the event type is 'high' and severity is '1' before printing out 'DT24/SIG Repair doors' and '90430/RSO' 'Repair lift' Commented Feb 13, 2018 at 8:11
  • yep, select first, print later. Commented Feb 13, 2018 at 8:13

1 Answer 1

2

I suggest breaking down the tasks into methods for their respective responsibilities. You have here loading, filtering, and transforming. The methods could reflect that. (I haven't tested this code, but this should show the general idea.)

I think you'd be better off with something like this:

require "csv"

def load_alarms
  CSV.read 'alarms.csv', {col_sep: ';'}
end

def filter_by_event_type_and_severity(alarms, event_type, severity)
  alarms.select do |alarm|
    alarm[1] == event_type && alarm[2].to_i == severity.to_i
  end
end

target_alarms = filter_by_event_type_and_severity(
    load_alarms, 'high', 1)
equipments   = target_alarms.map { |alarm| alarm[0] }
descriptions = target_alarms.map { |alarm| alarm[3] }

p target_alarms
p equipments
p descriptions

Even if the implementations of the methods are trivially simple, this will give you the practice of separating code into logical parts, each of which is highly cohesive or specialized, with minimal coupling (dependency) on other parts of the code.

One of the biggest mistakes of beginners is trying to do too much in the same chunk of code -- high and low level code, and with totally unrelated subjects. Learning how to break problems down into smaller problems, and logically organizing them, is one of the important skills you can learn.

By the way, I don't recommend the to_i call on severity. Since the parameter is logically a number, you shouldn't really permit a string, IMO.

Sign up to request clarification or add additional context in comments.

18 Comments

Hi for the code 'def filter_by_event_type_and_severity(alarms, event_type, severity)' I get an error "wrong number of arguments (given 2, expected 3") would you know how to fix this? Thanks!
Sounds like you're only passing 2 arguments. I added the first argument, the array of records, to what you had. So maybe that's the one that's missing? You can post code to show us; one site for that is gist.github.com.
gist.github.com/kaeltharlas/cdeddca2d0d71dbeea40ffa160fbb27b Here's the code. It's the same as yours, could you try to run it? Thanks!
One way to create the display line would be to use printf-style formatting to put the entries in aligned columns, like this: eq_and_desc_lines = target_alarms.map { |alarm| "%-15.15s%s" % [alarm[0], alarm[3]] }. You might need to adjust the width setting for proper alignment.
target_alarms.map { |alarm| [alarm[0], alarm[3]] }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.