File fun! How would you

…index in a hash…
…all directories under ‘src’…
…that contain filetypes in ‘ft[]’…
…without getting lost in arrays inside arrays…

#Here is my try
src="/"
tmpindex = []
filetypes = [".jpg", “.png”, “.gif”, “.bmp”]
filetypes.each { |typ| tmpindex << Dir[src+"**/*"+typ] }

index={}
tmpindex.each { |fpath|
dirpath = File.dirname(File.expand_path(fpath.to_s))
index[dirpath] = File.basename(fpath.to_s)
}
puts index
#bleed
#end

On Nov 21, 9:00 am, Casimir [email protected] wrote:

…index in a hash…
…all directories under ‘src’…
…that contain filetypes in ‘ft[]’…
…without getting lost in arrays inside arrays…

#Here is my try
src=“/”
tmpindex = []
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]

require ‘find’

src = “/”
index = []
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]

Find.find(src) do |path|
index << path if filetypes.include? File.extname(path)
end

On 21.11.2007 16:00, Casimir wrote:

index={}
tmpindex.each { |fpath|
dirpath = File.dirname(File.expand_path(fpath.to_s))
index[dirpath] = File.basename(fpath.to_s)
}
puts index
#bleed
#end

Are you sure you want to remember only one file per directory? If not:

EXTENSIONS = %w{jpg png gif bmp}

index = Hash.new {|h,k| h[k]=[]}

Dir["**/*.{#{EXTENSIONS.join(’,’)}}"].each do |f|
dir, base = File.split f
index[dir] << base
end

And, for the record, the solution with #inject:

EXTENSIONS = %w{jpg png gif bmp}

index = Dir["**/*.{#{EXTENSIONS.join(’,’)}}"].inject(
Hash.new {|h,k| h[k]=[]}) do |h,f|
dir, base = File.split f
h[dir] << base
h
end

Cheers

robert

Thanks for replies yermej and Robert! I learn some new agin! IT’s easy!
8)

More comments further down the post… And questions.

On Nov 21, 9:00 am, Casimir [email protected] wrote:

…index in a hash…
…all directories under ‘src’…
[snip!]

yermej wrote this solution:

require ‘find’

src = “/”
index = []
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]

Find.find(src) do |path|
index << path if filetypes.include? File.extname(path)
end

Well it works great but the results are in an array and would require
further processing before files would be in an hash… with path as keys
(which my orig post didn’t specify though).

Robert K. wrote:

EXTENSIONS = %w{jpg png gif bmp}

index = Hash.new {|h,k| h[k]=[]}

Dir[“**/*.{#{EXTENSIONS.join(‘,’)}}”].each do |f|
dir, base = File.split f
index[dir] << base
end

Wow. That is snappy.

I, too, was annoyed when I couldnt assign stuff into the Hash with <<.

I dont know why. It has more characters than “arr[key]=val”. You could
save almost 2 lines with the = version. It looks better I guess. I
couldnt bend ruby like that tho, over. Haha!

I also like how you handle the assignment of the split file path with
“dir, base = File.split f”.

And I have no idea what the {#{EXTENSIONS.join(‘,’)}} is all about! IRB
just gave me *'s.

lol I am just a dummy! Hahaha!

On Nov 22, 2:30 am, Casimir [email protected] wrote:

end

Well it works great but the results are in an array and would require
further processing before files would be in an hash… with path as keys
(which my orig post didn’t specify though).

Sorry, missed the hash part. Borrowing from Robert’s solution:

require ‘find’

src = ‘/’
filetypes = [“.jpg”, “.png”, “.gif”, “.bmp”]
index = Hash.new {|h, k| h[k] = []}

Find.find(src) do |path|
next unless filetypes.include? File.extname(path)
dir, base = File.split path
index[dir] << base
end

Jeremy