File question

Hi,

File.open(‘file.txt’, ‘r’).each do |c|
if(c=="__#{t.month}/#{t.mday}/#{t.year} (#{t.zone})")
e=true
end
end

E.g. of date in file
__6/26/2008 (Pacific Daylight Time)

I’m trying to see if the date already exists in file.txt. Since it
already does, it must set e=true, but the if(c== …) statement doesn’t
seem to evaluate to true, so e never becomes true! =(

Thanks for the help!

From: [email protected] [mailto:[email protected]]

I’m trying to see if the date already exists in file.txt. Since it

already does, it must set e=true, but the if(c== …)

statement doesn’t seem to evaluate to true, so e never becomes true!

pls send erring code.

thanks -botp

pls send erring code.

I’m not sure what ‘erring’ code is

2008/6/27 Justin To [email protected]:

Hi,

File.open(‘file.txt’, ‘r’).each do |c|

c.chomp!

From: [email protected] [mailto:[email protected]]

> pls send erring code.

I’m not sure what ‘erring’ code is

sorry, i meant the section of code that will prove the program has a
flaw; eg, your sample code does not show where the value t came from. It
would then be easy to print out the value of c and e, and check their
differences…

kind regards -botp

From: Peña, Botp [mailto:[email protected]]

value of c and e, and check their differences…

sorry, possibly lack of sleep on my part, i meant “value c and the
quoted text…”

I have another problem also:

File.open(“genver.txt”, ‘r’).each do |x|
puts x
end

The file contains:
v4

But ‘puts x’ outputs ÿþv Nul4 Nul

t=Time.new
e=false

if(File.size(‘ManageLists_Changelog.txt’)==0)
e=true
end

File.open(‘ManageLists_Changelog.txt’, ‘r’).each do |c|
if(c=="__#{t.month}/#{t.mday}/#{t.year} (#{t.zone})")
puts “TESTING”
e=true
end
end

if(e)
    File.open('ManageLists_Changelog.txt', 'a') do |c|
      c.puts "__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)"
    end
end

On Jun 27, 6:21 pm, Justin To [email protected] wrote:


Posted viahttp://www.ruby-forum.com/.

Hello,

Instead of c.chomp! as it was suggested, use c=c.chomp(" ") before the
"if"statement. The problem seems to be a white space at the end of
“c”… if you use print instead of puts you’ll see it.

About your second problem I just tested it and works fine for me.
Maybe txt file encoding is wrong…be sure to save it with utf-8 enc.

Xuan wrote:

On Jun 27, 6:21�pm, Justin To [email protected] wrote:


Posted viahttp://www.ruby-forum.com/.

Hello,

Instead of c.chomp! as it was suggested, use c=c.chomp(" ") before the
"if"statement. The problem seems to be a white space at the end of
“c”… if you use print instead of puts you’ll see it.

About your second problem I just tested it and works fine for me.
Maybe txt file encoding is wrong…be sure to save it with utf-8 enc.

Thanks, works great!

From: [email protected] [mailto:[email protected]]

t=Time.new

e=false

ok

#if(File.size(‘ManageLists_Changelog.txt’)==0)

e=true

end

i would prefer,

e = File.size(‘ManageLists_Changelog.txt’)==0

File.open(‘ManageLists_Changelog.txt’, ‘r’).each do |c|

if(c==“__#{t.month}/#{t.mday}/#{t.year} (#{t.zone})”)

puts “TESTING”

e=true

end

end

hmmm, if e is already true, why would i let it go through that code wc
just sets it to true again??

maybe, you wanted test e first like,

if not e
File.open(‘ManageLists_Changelog.txt’, ‘r’).each do |c|
if c==“__#{t.month}/#{t.mday}/#{t.year} (#{t.zone})”
e=true
end
end
end

(note, i’m just following the logic of your code)

now, when comparing strings, string#size usually catches the diff. and
when debugging/printing values, it would be better if you use #p instead
of #puts since #p quotes the whole string expression and displays
invisible characters too (by escaping them w a backslash on display)

eg,

p “asdf\n\t”
=>“asdf\n\t”

vs.

puts “asdf\n\t”
=>asdf

print “asdf\n\t”
asdf
#=> nil

ergo, i would then do my test loop like,

File.open(‘ManageLists_Changelog.txt’, ‘r’).each do |c|
d=“__#{t.month}/#{t.mday}/#{t.year} (#{t.zone})”
p c
p d
p c == d
end

hth.
kind regards -botp

From: Xuan [mailto:[email protected]]

Instead of c.chomp! as it was suggested, use c=c.chomp(" ") before the

that is dangerous since chomp(" ") only chomps the ending space, but not
line endings like newline.

c=“asdf \n”.chomp(" ")
#=> “asdf \n”

in his case, i would prefer #strip wc will strip out all whitespace
before and after the string

like,

c=" asdf \t \r\n"
#=> " asdf \t \r\n"

c.strip!
#=> “asdf”

"if"statement. The problem seems to be a white space at the end of

“c”… if you use print instead of puts you’ll see it.

using p is better/catching to the eye

print "asdf "
asdf #=> nil

p "asdf "
"asdf "
#=> nil

About your second problem I just tested it and works fine for me.

Maybe txt file encoding is wrong…be sure to save it with utf-8 enc.

i agree.

kind regards -botp