?
Solved

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

Posted on 2008-11-17
6
Medium Priority
?
272 Views
Last Modified: 2012-05-05
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
Comment
Question by:itguy411
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:nchristie
ID: 22977335
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
 

Author Comment

by:itguy411
ID: 22978476
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
 
LVL 1

Expert Comment

by:nchristie
ID: 22978866
Can you post the entire block of code in question, as well as how you're trying to reference the parsed values.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:itguy411
ID: 22978948
#!/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
 
LVL 1

Accepted Solution

by:
nchristie earned 2000 total points
ID: 22979470
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
 

Author Closing Comment

by:itguy411
ID: 31517421
Great job !!!!

this is exactly what I needed.  Ruby ( and you ) rocks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question