PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > File fun! How would you...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
File fun! How would you...

Réponse
 
LinkBack Outils de la discussion
Vieux 21/11/2007, 15h00   #1
Casimir
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 21/11/2007, 15h21   #2
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File fun! How would you...

On Nov 21, 9:00 am, Casimir <pikEISPAMMMs...@welho.com> 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
  Réponse avec citation
Vieux 21/11/2007, 18h36   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File fun! How would you...

On 21.11.2007 16:00, Casimir 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"]
> 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
  Réponse avec citation
Vieux 22/11/2007, 08h30   #4
Casimir
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File fun! How would you...

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 <pikEISPAMMMs...@welho.com> 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 Klemme 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!
  Réponse avec citation
Vieux 22/11/2007, 16h10   #5
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: File fun! How would you...

On Nov 22, 2:30 am, Casimir <pikEISPAMMMs...@welho.com> wrote:
> 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 <pikEISPAMMMs...@welho.com> 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).


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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h51.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24745 seconds with 13 queries