Directory.entries problems with while loops


#1

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?


#2

On Jun 5, 2006, at 8:57 AM, Tait P. 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?

Show us some code and then we can help.

cr


#3

unknown wrote:

On Jun 5, 2006, at 8:57 AM, Tait P. wrote:
Show us some code and then we can help.

cr

here is the code

c=File.dirname(“directory.rb”)
direc=Dir.entries©
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


#4

On Jun 5, 2006, at 9:33 AM, Tait P. wrote:

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

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


#5

Thank you for the help


#6

Tait P. wrote:

b= Regexp.new(/tlb/)

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


#7

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 Y.
http://9cays.com/


#8

On Tue, 6 Jun 2006, Pete Y. wrote:

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 Y.
http://9cays.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