File question

J

Justin To

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!
 
P

Peña, Botp

RnJvbTogdGVrbWNAaG90bWFpbC5jb20gW21haWx0bzp0ZWttY0Bob3RtYWlsLmNvbV0gDQojIEkn
bSB0cnlpbmcgdG8gc2VlIGlmIHRoZSBkYXRlIGFscmVhZHkgZXhpc3RzIGluIGZpbGUudHh0LiBT
aW5jZSBpdA0KIyBhbHJlYWR5IGRvZXMsIGl0IG11c3Qgc2V0IGU9dHJ1ZSwgYnV0IHRoZSBpZihj
PT0gLi4uKSANCiMgc3RhdGVtZW50IGRvZXNuJ3Qgc2VlbSB0byBldmFsdWF0ZSB0byB0cnVlLCBz
byBlIG5ldmVyIGJlY29tZXMgdHJ1ZSEgDQoNCnBscyBzZW5kIGVycmluZyBjb2RlLg0KDQp0aGFu
a3MgLWJvdHANCg==
 
R

Robert Klemme

2008/6/27 Justin To said:
Hi,

File.open('file.txt', 'r').each do |c|
c.chomp!

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!
 
P

Peña, Botp

RnJvbTogdGVrbWNAaG90bWFpbC5jb20gW21haWx0bzp0ZWttY0Bob3RtYWlsLmNvbV0gDQojIA0K
IyA+IHBscyBzZW5kIGVycmluZyBjb2RlLg0KIyANCiMgSSdtIG5vdCBzdXJlIHdoYXQgJ2Vycmlu
ZycgY29kZSBpcw0KDQpzb3JyeSwgaSBtZWFudCB0aGUgc2VjdGlvbiBvZiBjb2RlIHRoYXQgd2ls
bCBwcm92ZSB0aGUgcHJvZ3JhbSBoYXMgYSBmbGF3OyBlZywgeW91ciBzYW1wbGUgY29kZSBkb2Vz
IG5vdCBzaG93IHdoZXJlIHRoZSB2YWx1ZSB0IGNhbWUgZnJvbS4gSXQgd291bGQgdGhlbiBiZSBl
YXN5IHRvIHByaW50IG91dCB0aGUgdmFsdWUgb2YgYyBhbmQgZSwgYW5kIGNoZWNrIHRoZWlyIGRp
ZmZlcmVuY2VzLi4uDQoNCmtpbmQgcmVnYXJkcyAtYm90cA0KDQo=
 
P

Peña, Botp

RnJvbTogUGXDsWEsIEJvdHAgW21haWx0bzpib3RwQGRlbG1vbnRlLXBoaWwuY29tXSANCiMgdmFs
dWUgb2YgYyBhbmQgZSwgYW5kIGNoZWNrIHRoZWlyIGRpZmZlcmVuY2VzLi4uDQoNCnNvcnJ5LCBw
b3NzaWJseSBsYWNrIG9mIHNsZWVwIG9uIG15IHBhcnQsIGkgbWVhbnQgInZhbHVlIGMgYW5kIHRo
ZSBxdW90ZWQgdGV4dC4uLiINCg==
 
J

Justin To

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
 
J

Justin To

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
 
X

Xuan

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

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

Justin To

Xuan said:
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!
 
P

Peña, Botp

From: Xuan [mailto:[email protected]]=20
# Instead of c.chomp! as it was suggested, use c=3Dc.chomp(" ") before =
the

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

c=3D"asdf \n".chomp(" ")
#=3D> "asdf \n"

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

like,

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

c.strip!
#=3D> "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 #=3D> nil

p "asdf "
"asdf "
#=3D> 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
 

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,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top