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!

···

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

# 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

···

From: tekmc@hotmail.com [mailto:tekmc@hotmail.com]

Hi,

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

c.chomp!

···

2008/6/27 Justin To <tekmc@hotmail.com>:

        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-forum.com/\.

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

pls send erring code.

I'm not sure what 'erring' code is

···

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

# 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..."

···

From: Peña, Botp [mailto:botp@delmonte-phil.com]

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-forum.com/.

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-forum.com/.

# 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

···

From: tekmc@hotmail.com [mailto:tekmc@hotmail.com]

#
# 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

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.

···

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-forum.com/.

Xuan wrote:

···

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

# 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

···

From: Xuan [mailto:xuanpa@gmail.com]