[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File question

Justin To

6/27/2008 4:23:00 AM

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!
--
Posted via http://www.ruby-....

10 Answers

Peña, Botp

6/27/2008 5:08:00 AM

0

RnJvbTogdGVrbWNAaG90bWFpbC5jb20gW21haWx0bzp0ZWttY0Bob3RtYWlsLmNvbV0gDQojIEkn
bSB0cnlpbmcgdG8gc2VlIGlmIHRoZSBkYXRlIGFscmVhZHkgZXhpc3RzIGluIGZpbGUudHh0LiBT
aW5jZSBpdA0KIyBhbHJlYWR5IGRvZXMsIGl0IG11c3Qgc2V0IGU9dHJ1ZSwgYnV0IHRoZSBpZihj
PT0gLi4uKSANCiMgc3RhdGVtZW50IGRvZXNuJ3Qgc2VlbSB0byBldmFsdWF0ZSB0byB0cnVlLCBz
byBlIG5ldmVyIGJlY29tZXMgdHJ1ZSEgDQoNCnBscyBzZW5kIGVycmluZyBjb2RlLg0KDQp0aGFu
a3MgLWJvdHANCg==

Justin To

6/27/2008 5:34:00 AM

0


> pls send erring code.

I'm not sure what 'erring' code is

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

Robert Klemme

6/27/2008 7:01:00 AM

0

2008/6/27 Justin To <tekmc@hotmail.com>:
> 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!
> --
> Posted via http://www.ruby-....
>
>



--
use.inject do |as, often| as.you_can - without end

Peña, Botp

6/27/2008 7:26:00 AM

0

RnJvbTogdGVrbWNAaG90bWFpbC5jb20gW21haWx0bzp0ZWttY0Bob3RtYWlsLmNvbV0gDQojIA0K
IyA+IHBscyBzZW5kIGVycmluZyBjb2RlLg0KIyANCiMgSSdtIG5vdCBzdXJlIHdoYXQgJ2Vycmlu
ZycgY29kZSBpcw0KDQpzb3JyeSwgaSBtZWFudCB0aGUgc2VjdGlvbiBvZiBjb2RlIHRoYXQgd2ls
bCBwcm92ZSB0aGUgcHJvZ3JhbSBoYXMgYSBmbGF3OyBlZywgeW91ciBzYW1wbGUgY29kZSBkb2Vz
IG5vdCBzaG93IHdoZXJlIHRoZSB2YWx1ZSB0IGNhbWUgZnJvbS4gSXQgd291bGQgdGhlbiBiZSBl
YXN5IHRvIHByaW50IG91dCB0aGUgdmFsdWUgb2YgYyBhbmQgZSwgYW5kIGNoZWNrIHRoZWlyIGRp
ZmZlcmVuY2VzLi4uDQoNCmtpbmQgcmVnYXJkcyAtYm90cA0KDQo=

Peña, Botp

6/27/2008 7:40:00 AM

0

RnJvbTogUGXDsWEsIEJvdHAgW21haWx0bzpib3RwQGRlbG1vbnRlLXBoaWwuY29tXSANCiMgdmFs
dWUgb2YgYyBhbmQgZSwgYW5kIGNoZWNrIHRoZWlyIGRpZmZlcmVuY2VzLi4uDQoNCnNvcnJ5LCBw
b3NzaWJseSBsYWNrIG9mIHNsZWVwIG9uIG15IHBhcnQsIGkgbWVhbnQgInZhbHVlIGMgYW5kIHRo
ZSBxdW90ZWQgdGV4dC4uLiINCg==

Justin To

6/27/2008 4:00:00 PM

0

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

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

Justin To

6/27/2008 4:21:00 PM

0

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
--
Posted via http://www.ruby-....

Xuan

6/27/2008 6:38:00 PM

0

On Jun 27, 6:21 pm, Justin To <te...@hotmail.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

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.

Justin To

6/27/2008 8:03:00 PM

0

Xuan wrote:
> On Jun 27, 6:21�pm, Justin To <te...@hotmail.com> wrote:
>> --
>> Posted viahttp://www.ruby-....
>
> 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!
--
Posted via http://www.ruby-....

Peña, Botp

6/28/2008 4:00:00 AM

0

From: Xuan [mailto:xuanpa@gmail.com]=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