Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 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