• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

How do I pull 3 numbers out of a string in ruby ?

OK, this is my 4th line of ruby code so you can say I am new.  I do know shell a bit.

Code:
directorylist = %x[sjirdtag   `inquire | grep i500 | cut -f1  -d :` ]
directorylist.each { |i| print i.grep(/imp_exp=1/) , "\n"}

Returns
        Elem[004]: tag_val=1 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=1 imp_exp=1
        Elem[005]: tag_val=1 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=1 imp_exp=1
        Elem[006]: tag_val=1 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=1 imp_exp=1


I want to pull the values 004, 005 and 006 out of the strings and store them to a new array.





0
itguy411
Asked:
itguy411
  • 3
  • 3
1 Solution
 
nchristieCommented:
This tells us to find the pattern starting with "[" (must use "\" escape character to reference this character). The "{0,0}" syntax tells us we want to match but not use the brackets. Then we match all digits until we reach "]".

There's a great Ruby syntax guide here.
a="Elem[004]: tag_val=1 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=1 imp_exp=1"
puts a[ /\[{0,0}\d+\]{0,0}/ ]

Open in new window

0
 
itguy411Author Commented:
Here is what I get:

"./ru.import_label_inventory" 14 lines, 334 characters
bash-2.05#   ./ru.import_label_inventory
./ru.import_label_inventory:12:in `[]': can't convert Regexp into Integer (TypeError)
        from ./ru.import_label_inventory:12
        from ./ru.import_label_inventory:9:in `each'
        from ./ru.import_label_inventory:9

0
 
nchristieCommented:
Can you post the entire block of code in question, as well as how you're trying to reference the parsed values.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
itguy411Author Commented:
#!/opt/csw/bin/ruby
directorylist = %x[sjirdtag   `inquire | grep i500 | cut -f1  -d :` ]
directorylist.each { |i| tmp =  i.grep(/imp_exp=1/)
puts tmp[ /\[{0,0}\d+\]{0,0}/ ]

Here is the raw data from the second line of code (sjirdtag)

Tag Data for scsidev@4.1.1, Element Type IMPORT/EXPORT:
        Elem[001]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[002]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[003]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[004]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[005]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[006]: tag_val=1 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=1 imp_exp=1
                   VolumeTag=<DP0225L3                        >
        Elem[007]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=1
                   VolumeTag=<DP0252L3                        >
        Elem[008]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[009]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[010]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[011]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[012]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[013]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[014]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[015]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[016]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[017]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0
        Elem[018]: tag_val=0 pres_val=1 inp_enab=1 exp_enab=1 access=1 full=0 imp_exp=0

0
 
nchristieCommented:
This is because the Ruby grep() function returns an array. You need to iterate the resultant arrays & simultaneously parse and push into your output array.

aryParsedIndexes = Array.new()
directorylist = %x[sjirdtag   `inquire | grep i500 | cut -f1  -d :` ]
directorylist.each { |i|
   tmp = i.grep(/imp_exp=1/)
   tmp.each { |j|
      aryParsedIndexes.push( j[/\[{0,0}\d+\]{0,0}/].to_i() )
   }
}

aryParsedIndexes.each { |i| puts i.to_s() }
0
 
itguy411Author Commented:
Great job !!!!

this is exactly what I needed.  Ruby ( and you ) rocks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now