Created
January 28, 2011 16:48
-
-
Save apk/800535 to your computer and use it in GitHub Desktop.
Converting find output into a tree of hashes (and a funny use of inject)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree={} | |
STDIN.each_line do |l| | |
a=l.strip.split '/' | |
a.inject(tree) { |t,n| t[n] ||= {} } | |
end | |
puts tree.inspect | |
def rek t, p | |
t.each_pair do |k, v| | |
if (v.empty?) | |
puts p + "/" + k | |
else | |
rek(v, p + "/" + k) | |
end | |
end | |
end | |
rek tree, "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment