Dir.entries, output showing nils

When I output to line for RegisteredUser it shows all filenames as nil
for all but the files. What can I change to show only the filename
meeting the conditions?
Thanks, MC

outputs…
nil
nil
nil
nil
AUR123443.txt

Dir.entries(ascpath).each do |filename|

if filename =~ /^AU/ then
if filename[2…2] == “R” then
RegisteredUser = filename
else
NonRegisteredUser = filename
end
end

#below shows nil for all files and then shows the registereduser file.
puts RegisteredUser

end

On Jul 23, 2009, at 11:42 AM, Mmcolli00 Mom wrote:

AUR123443.txt

#below shows nil for all files and then shows the registereduser file.
puts RegisteredUser

end

Posted via http://www.ruby-forum.com/.

First, Capitalized names in Ruby are constants. You probably want a
local_variable instead.

I’m not sure from your example and question what you really want, but
here are some options:

An array of filenames that start with AU

files = Dir.glob(“AU*”) # or Dir[“AU*”]

Split one array into a pair of arrays (in another array) based on a

condition:
files.partition {|filename| filename[2…2] == “R”}

(which is Enumerable#partition)

Does that help?

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

On Jul 23, 2009, at 11:42 AM, Mmcolli00 Mom wrote:

AUR123443.txt

#below shows nil for all files and then shows the registereduser file.
puts RegisteredUser

end

Posted via http://www.ruby-forum.com/.

First, Capitalized names in Ruby are constants. You probably want a
local_variable instead.

I’m not sure from your example and question what you really want, but
here are some options:

An array of filenames that start with AU

files = Dir.glob(“AU*”) # or Dir[“AU*”]

Split one array into a pair of arrays (in another array) based on a

condition:
files.partition {|filename| filename[2…2] == “R”}

(which is Enumerable#partition)

Does that help?

My project consists of me reading the contents of a folder. The folder
will have 5 files for testing. The files are named as such…

AUR234_somename.txt
PUR233_somename.txt…

If a file starts with AU I know that this file fits the Aero group and
the third letter being ‘R’ tells me if it is registered. If a file fits
the condition. I want to store that filename so that other programs can
peform I/O tests on it. The 5 files will be replaced occasionally but
with the same strict naming conventions. Each file will be meant for a
different type of test. The number represent a count that will be
validated later in the test as well.

Good tip- I will apply lowercase letters to the filenames. I don’t think
the partition will help in this case though. Thanks -MC

On Fri, Jul 24, 2009 at 01:23:27AM +0900, Rob B. wrote:

nil
end

(which is Enumerable#partition)

Does that help?

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

He’s also defining it inside the ‘each’ which, I believe, localizes the
variable. Thus,
it will be nil at the beginning of each loop. If not set (i.e. the file
does not match), it will
be nil, as he is saying now.

Seems to me you want something more like this:

if filename =~ /^AUR/ then
puts filename
end

It should also be noted that you do not need to say [2…2] if you want
the 3rd char of a string, just [2]
will do.

HTH,

  • Kyle

It should also be noted that you do not need to say [2…2] if you want
the 3rd char of a string, just [2]
will do.

when I puts filename[2] it returns the 56, or 48 which I think is the
r’s character id or maybe even index for that string. Not sure…

On Jul 23, 2009, at 12:45 PM, Mmcolli00 Mom wrote:

It should also be noted that you do not need to say [2…2] if you
want
the 3rd char of a string, just [2]
will do.

when I puts filename[2] it returns the 56, or 48 which I think is the
r’s character id or maybe even index for that string. Not sure…

In Ruby 1.8, String#[] with a single Integer argument returns the
character, which as you’ve found is the ASCII code for the character.

In Ruby 1.9, String#[] returns a 1-character string (and you can apply
String#ord to it if you want the value)

So filename[2…2] will give you a 1-character string in both versions.
I’d stick with it.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Jul 23, 2009, at 12:37 PM, Mmcolli00 Mom wrote:

First, Capitalized names in Ruby are constants. You probably want a
files.partition {|filename| filename[2…2] == “R”}
PUR233_somename.txt…

Good tip- I will apply lowercase letters to the filenames. I don’t
think
the partition will help in this case though. Thanks -MC

Ah, much better description of your problem.

Dir[“directory/*”] will include the “directory/” part in the returned
name. (didn’t I see this in your code? anyway…)

Dir[“[Aa][Uu][Rr]*”] gives you exactly these files

You could also:
Dir[“[Aa][Uu]*”].partition {|file| file =~ /\A…r/i }
to get files where the third character is R or r.

In a Regexp, /\A/ is the beginning of the string, /./ is any
character, and the /r/i is an r matched insensitive to case (that’s
the i flag at the end).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]