Solved

How do I redo an array in ruby to an array of arrays ?

Posted on 2012-04-05
3
428 Views
Last Modified: 2012-04-06
My data looks like this:

<todo>
filename: call john doe_20120405_081431_
call john doe_20120405_081431_about car
call john doe_20120405_081431_about car_about offer
Update-YN:y
OpenClosed_OC:O
Status-RYG:G
<todo>
filename: till garden_20120405_081432_
till garden_20120405_081432_get gas
till garden_20120405_081432_get oil
Update-YN:N
OpenClosed_OC:O
Status-RYG:G
<todo>

The objective is to pull out the first <todo> and if "Update-YN:Y" then process just that todo. I am thinking I need to make an array from the original array splitting the elements upon <todo>. I will have many more than two elements however this is representitive data. This is a project for my learning.

My code thus far is:

@pimworking = File.open('pim_working.txt')
puts @pimworking.readlines

thx

timfox123
0
Comment
Question by:TIMFOX123
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 37815763
I think you are looking for something like this

pimworking = File.open('pim_working.txt').readlines

todos = Array.new()
row = Array.new()

pimworking.each do |ln|
  # there is no fallback for last of todos (so assuming series ends with <todo>)
  if ln.match(/<todo>/) then
    temp = Array.new(row)  # make a shadow copy instead of reference
    todos << temp
    row.clear
  else
    row << ln
  end
end

puts todos.select{|ar| ar.any?{ |w| w.index("Update-YN:y")} }[0]

Open in new window

0
 

Author Closing Comment

by:TIMFOX123
ID: 37815976
This totally works.  

Been working on this too long  

thanks !!!!

Great job
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37816141
welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now