[Note: parts of this message were removed to make it a legal post.]
That works great, thanks Josh. A couple of questions if you don't mind.
1. What is the purpose of the [] in line below? Does it mean collect
whatever matches into an array?
categories << [ line.sub(/^category: /,'').chomp ]
Yes, but not whatever matches. The call to #sub, with the second arg being
an empty string, says to remove "category: " from the string, if it is at
the beginning. And the chomp removes the newline. So if line is "category:
cat1\n", then line.sub(/^category: /,'').chomp will return "cat1". Then we
stick that in the Array
2. I've tried to achieve the same result using an existing array, rather
than reading from the file and I'm stuck. I'm using JRuby 1.6RC1 and
getting this error about NilClass. Any ideas?
irb(main):038:0> arr2 = []
irb(main):039:0> arr
=> [["cat1", "1", "2", "3"], ["cat2", "1", "2"], ["cat3", "1", "2"]]
irb(main):040:0> arr.map do |item|
irb(main):041:1* if item =~ /^cat/
irb(main):042:2> arr2 << [ item ]
irb(main):043:2> else
irb(main):044:2* arr2.last << item
irb(main):045:2> end
irb(main):046:1> end
You are right on, here, just getting confused about your data format, again.
Your code will work correctly if arr is an array of the lines of your file,
such as you would get with File.readlines.
In other words, in your irb example,
arr is [["cat1", "1", "2", "3"], ["cat2", "1", "2"], ["cat3", "1", "2"]]
but in mine, it was read in straight from the file, so it would be
["cat1", "1", "2", "3", "cat2", "1", "2", "cat3", "1", "2"]
If you fix that, it will work correctly.
As a side note, you are doing arr.map (
http://ruby-doc.org/core/classes/Enumerable.html#M001491), but what you
really mean is arr.each (
http://ruby-doc.org/core/classes/Array.html#M000231).
It isn't harming anything, but it is misleading, because map implies you are
trying to create a new array by collecting the results of the blocks for
each element, but really you are just trying to iterate.