Forum: Ruby Directory.entries problems with while loops

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4b781b6ab8600b559b8d729f423db26e?d=identicon&s=25 Tait Pollard (customsgod)
on 2006-06-05 15:57
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?
A0c079a7c3c9b2cf0bffebd84dc578b0?d=identicon&s=25 unknown (Guest)
on 2006-06-05 16:23
(Received via mailing list)
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?

Show us some code and then we can help.

cr
4b781b6ab8600b559b8d729f423db26e?d=identicon&s=25 Tait Pollard (customsgod)
on 2006-06-05 16:33
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
A0c079a7c3c9b2cf0bffebd84dc578b0?d=identicon&s=25 unknown (Guest)
on 2006-06-05 16:54
(Received via mailing list)
On Jun 5, 2006, at 9:33 AM, Tait Pollard 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(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
F0aaa796f43b5c4bc21db2051ecb4bfa?d=identicon&s=25 Mariano Kamp (Guest)
on 2006-06-05 16:57
(Received via mailing list)
Tait Pollard 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
4b781b6ab8600b559b8d729f423db26e?d=identicon&s=25 Tait Pollard (customsgod)
on 2006-06-05 17:05
Thank you for the help
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2006-06-06 03:15
(Received via mailing list)
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/
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-06-06 03:21
(Received via mailing list)
On Tue, 6 Jun 2006, Pete Yandell 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 Yandell
> 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
This topic is locked and can not be replied to.