Solved

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

Posted on 2012-04-05
3
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

749 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