Haris said:
Hi.
How can I parse a tree (arrays within arrays), for example a tree of
strings, to print all strings ?
Thanks.
tree walking hints at a recursive routine...
def tree_walk(array)
array.each do |item|
if item.class == Array
tree_walk(item)
else
puts item
end
end
end
or for the one-liners among us
def tree_walk(array)
array.each { |item| (item.class == Array)? tree_walk(item) : puts item }
end
However, I don't reccomend testing on the item.class. I can't explain
why very well, but it seems wrong to me. Maybe someone else can...?
=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================