I am trying to create an array with all the files of a directory of a
certain file type. I use Directory.entries to get an array of all the
files and folders in the directory. Then when i try to use a while loop
to sort through the array with a Regexp variable i get an infinite loop.
Any suggestions?
···
--
Posted via http://www.ruby-forum.com/.
Show us some code and then we can help.
cr
···
On Jun 5, 2006, at 8:57 AM, Tait Pollard wrote:
I am trying to create an array with all the files of a directory of a
certain file type. I use Directory.entries to get an array of all the
files and folders in the directory. Then when i try to use a while loop
to sort through the array with a Regexp variable i get an infinite loop.
Any suggestions?
unknown wrote:
Show us some code and then we can help.
cr
here is the code
c=File.dirname("directory.rb")
direc=Dir.entries(c)
b= Regexp.new(/tlb/)
counter,counter1=0,0
array=
while(counter<direc.length)
if (direc[counter]=~b)
puts direc[counter]
array[counter1]=direc[counter]
counter=+1
end
counter=+1
end
···
On Jun 5, 2006, at 8:57 AM, Tait Pollard wrote:
--
Posted via http://www.ruby-forum.com/\.
The flaw is here:
counter=+1
You are setting counter equal to +1 when you mean to increment it.
Try:
counter += 1 # notice the + is to the left of the =
Also, you are never incrementing counter1, so perhaps the code should look like this:
c=File.dirname("directory.rb")
direc=Dir.entries(c)
b= Regexp.new(/tlb/)
counter,counter1=0,0
array=
while(counter<direc.length)
if (direc[counter]=~b)
puts direc[counter]
array[counter1]=direc[counter]
counter1 += 1
end
counter += 1
end
···
On Jun 5, 2006, at 9:33 AM, Tait Pollard wrote:
unknown wrote:
On Jun 5, 2006, at 8:57 AM, Tait Pollard wrote:
Show us some code and then we can help.
cr
here is the code
c=File.dirname("directory.rb")
direc=Dir.entries(c)
b= Regexp.new(/tlb/)
counter,counter1=0,0
array=
while(counter<direc.length)
if (direc[counter]=~b)
puts direc[counter]
array[counter1]=direc[counter]
counter=+1
end
counter=+1
end
Tait Pollard wrote:
unknown wrote:
Show us some code and then we can help.
cr
here is the code
c=File.dirname("directory.rb")
direc=Dir.entries(c)
b= Regexp.new(/tlb/)
counter,counter1=0,0
array=
while(counter<direc.length)
if (direc[counter]=~b)
puts direc[counter]
array[counter1]=direc[counter]
counter=+1
end
counter=+1
end
Hi Tait,
did you look at Dir.glob (http://corelib.rubyonrails.org/classes/Dir.html#M000865\)? I guess that would solve your problem in one line of code.
Can't really tell without seeing test data, but I guess you could get rid of the infinite loop symptom by checking for <= instead of < in your loop or you can get rid of the cause by removing the first "counter+=1" line. It will be incremented anyway. Btw. there are better loop constructs with built-in counters, but for your case I would go with Dir.glob and forget about traversing the directories by hand.
Cheers,
Mariano
···
On Jun 5, 2006, at 8:57 AM, Tait Pollard wrote:
I've got to say, this is the least Ruby-ish Ruby I've ever seen! Someone has already pointed out Dir.glob, and that's probably the best approach, but even assuming you don't have that you can do:
array = Dir.entries(File.dirname("directory.rb")).grep(/tlb/)
Even if, for some strange reason, you needed to write the loop yourself, you could simplify it to:
array =
direc.each do |file|
array << file if file =~ /tlb/
end
Ruby has a lot of powerful methods for operations on Arrays. You should have a good read through the docs for Array and Enumerable!
Pete Yandell
···
On 06/06/2006, at 12:33 AM, Tait Pollard wrote:
unknown wrote:
On Jun 5, 2006, at 8:57 AM, Tait Pollard wrote:
Show us some code and then we can help.
cr
here is the code
c=File.dirname("directory.rb")
direc=Dir.entries(c)
b= Regexp.new(/tlb/)
counter,counter1=0,0
array=
while(counter<direc.length)
if (direc[counter]=~b)
puts direc[counter]
array[counter1]=direc[counter]
counter=+1
end
counter=+1
end
--
Posted via http://www.ruby-forum.com/\.
i realize it doesn't apply for most people, but i make a habit of always using
Dir.glob and other Dir operations with the block form. the difference in
performance between
Dir.glob(glob) do |path|
...
end
and
paths = Dir.glob(glob)
paths.each do |path|
...
end
becomes very important when directories contain more than 100,000 entries.
this rule can be generalized into 'always use the block form of methods unless
you have good reason not to'. just thought i'd thow that out there since it's
bitten me once or twice.
regards.
-a
···
On Tue, 6 Jun 2006, Pete Yandell wrote:
I've got to say, this is the least Ruby-ish Ruby I've ever seen! Someone has
already pointed out Dir.glob, and that's probably the best approach, but
even assuming you don't have that you can do:
array = Dir.entries(File.dirname("directory.rb")).grep(/tlb/)
Even if, for some strange reason, you needed to write the loop yourself, you could simplify it to:
array =
direc.each do |file|
array << file if file =~ /tlb/
end
Ruby has a lot of powerful methods for operations on Arrays. You should have
a good read through the docs for Array and Enumerable!
Pete Yandell
http://9cays.com/
--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama