Dir.entries, output showing nils

M

Mmcolli00 Mom

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
 
R

Rob Biedenharn

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


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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
L

lists

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
-- 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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)

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
 
M

Mmcolli00 Mom

Rob said:
AUR123443.txt

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

end


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
 
M

Mmcolli00 Mom

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...
 
R

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Rob Biedenharn

Rob said:
AUR123443.txt

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

end

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

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,283
Latest member
SherriP988

Latest Threads

Top