1

I've a file containing hundreds of object and value combination like below manner. I want to get input from user as object name & numeric value and return that associated value.

Object  cefcFRUPowerOperStatus
Type    PowerOperType 
        1:offEnvOther
        2:on
        3:offAdmin
        4:offDenied
        5:offEnvPower
        6:offEnvTemp

Object  cefcModuleOperStatus
Type    ModuleOperType 
        1:unknown
        2:ok
        3:disabled
        4:okButDiagFailed
        5:boot
        6:selfTest

E.g. - input -

           objectName = 'cefcModuleOperStatus'

           TypeNumber = '4'

Return - 'okButDiagFailed'

I am not aware of Ruby and get this done to help my peer. So please excuse if this is a novice question.

Note:- I've to create the file so with any file format it would be a great help.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
TheMightyNight
  • 161
  • 1
  • 2
  • 13
  • 2
    Can you convert that input file to JSON or YAML or anything that's more conventional? Even CSV would be a step up. – tadman Apr 12 '17 at 06:25
  • @tadman: Yes i can. Conversion from whichever file format will be helpful. – TheMightyNight Apr 12 '17 at 06:36
  • @tadman is exactly right. please check my answer that shows how to do this with a working example. – Shaunak Apr 12 '17 at 06:40
  • What have you tried? What is the code you are having trouble with? What is the problem with your code? Do you get an error? If yes, what is the precise error you are getting? Does the actual result differ from the expected result? If yes, what is the result you are expecting, why are you expecting that result, what is the actual result and how do they differ? Does the observed behavior of the code differ from the intended behavior? If yes, what is the intended behavior and why, what is the behavior you are seeing, and how do they differ? What is the specification for the intended behavior? – Jörg W Mittag Apr 12 '17 at 07:12

1 Answers1

5

If like you say you have control over creating the original data file, then creating it in json format would make accessing it trivial.

Here is a repl.it of complete working example. Just select the main.rb file and hit run!

For example if you create json file like:

data.json

{
  "cefcFRUPowerOperStatus": {
    "type": "PowerOperType",
    "status": {
      "1": "offEnvOther",
      "2": "on",
      "3": "offAdmin",
      "4": "offDenied",
      "5": "offEnvPower",
      "6": "offEnvTemp"
    }
  },
  "cefcModuleOperStatus": {
    "type": "ModuleOperType",
    "status": {
      "1": "unknown",
      "2": "ok",
      "3": "disabled",
      "4": "okButDiagFailed",
      "5": "boot",
      "6": "selfTest"
    }
  }
}

Then parsing it and accessing it in Ruby is as simple as :

require 'json'
file = File.read('data.json')
data = JSON.parse(file)

#accessing this data is simple now:

puts data["cefcModuleOperStatus"]["status"]["4"]
# > okButDiagFailed

Note: that this JSON format will work if your statuses are unique. If they are not, you can still use this way, but you will need to convert JSON to an array format. Let me know if this is the case and I can show you how to modify the json and ruby code for this.

Hope that helps, let me know if you have further questions about how this works.

Shaunak
  • 17,377
  • 5
  • 53
  • 84