File fun! How would you...

C

Casimir

....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
 
Y

yermej

...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
 
R

Robert Klemme

...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

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
 
C

Casimir

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

More comments further down the post... And questions.
...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 said:
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!
 
Y

yermej

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

More comments further down the post... And questions.
...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).

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top