Hi all,
Erik Veenstra wrote:
....
s.split(/\s*<[^<>]*>\s*/).reject{|x| x.empty?}
gegroet,
Erik V. - http://www.erikveen.dds.nl/
As a newbie I thought I'd have a go at this.
What I was trying to do was take Eriks code above, get the text between
tags into an array and then print it out as:
[message, the message to echo, Yes, unless data is included...]
I can do it by the look of things but if there are any suggestions how to improve this I'd appreciate it. Ie is the {} the most efficient way to fill the array? Is there a better way to print it out?
# --------------------------------
foo = " <td valign=\"top\">message</td> <td valign=\"top\">the message to echo.</td> <td valign=\"top\" align=\"center\">Yes, unless data is included in a character section within this element.</td> </tr> "
# I want to fill an array so I can display in the format
# [message, the message to echo, Yes, unless...]
a = Array.new
# I think I understand this.
# /\s*<[^<>]*>\s*/ = find all tags
# \s* find 0 or more spaces
# <[^<>]*> find anything between and including <>
# \s* as above
# and reject them (.reject)
# whats left (text between tags) use as x in the block |x|
# x seemed to include empty strings so only add x to the array if not ""
foo.split(/\s*<[^<>]*>\s*/).reject{|x| a.insert(-1,x) if x != ""}
# Trying to find the best way to print this???
# nothing like what I want
# puts "--- print a ---"
print a
# extra space after last item
# puts "\n\n--- print \"[\" a.each{|x| print x + \", \" print \"]\" ---"
print "[ "
a.each{|x| print x + ", "}
print "]"
# close but must know array size
# puts "\n\n print \"[\" + a[0] + \", \" + a[1] + \", \" + a[2] + \"]\""
print "[" + a[0] + ", " + a[1] + ", " + a[2] + "]\n"
# probably the most 'right' output wise
puts "\n\n--- for i in 0...a.length-1 ---"
print "[ "
for i in 0...a.length-1
print a[i] + ", "
end
print a[a.length-1]
print "]"
# --------------------------------
thanks,
Mark