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.





itguy411Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itguy411Author Commented:
Great job !!!!

this is exactly what I needed.  Ruby ( and you ) rocks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Ruby

From novice to tech pro — start learning today.