Solved

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

Posted on 2012-04-05
3
413 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
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…
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

12 Experts available now in Live!

Get 1:1 Help Now