Help with string matching

This does not work:

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”
#output is: 2007-07-30

puts “Getting log for #{logdate}”
File.open(“rdpconnect.log”).each do |line|
if line.match(logdatepattern)
puts line
end
end

However, this does:

logdate = “2007-07-30”
puts “Getting log for #{logdate}”
File.open(“rdpconnect.log”).each do |line|
if line.match(logdatepattern)
puts line
end
end

Any ideas or hints appreciated!
Dan

Hi,

At Wed, 1 Aug 2007 04:47:32 +0900,
Dan D. wrote in [ruby-talk:262706]:

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”
#output is: 2007-07-30

Isn’t “2007-7-30”?

Dan D. wrote:

end
end

end

Any ideas or hints appreciated!
Dan

What is logdatepattern, and what is its relation to logdate?

Nobuyoshi N. wrote:

Hi,

At Wed, 1 Aug 2007 04:47:32 +0900,
Dan D. wrote in [ruby-talk:262706]:

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”
#output is: 2007-07-30

Isn’t “2007-7-30”?

Yes, it logdate is returned as a string.

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”
puts “logdate is: #{logdate}”

will return:
logdate is: 2007-7-24

Dan D. wrote:

What is logdatepattern, and what is its relation to logdate?

    puts line
    puts line
end

end

The format of logdate appears the same in both instances. What am I
missing?

Thanks,

I don’t know what @calendar1 is, so I can’t reproduce this, but I
thought what Nobu meant when he replied to you is to say that the value
of that line contains 3 zeros, while the string you fed it contains 4.

Dan

Dan Z. wrote:

Dan D. wrote:

end
end

end

Any ideas or hints appreciated!
Dan

What is logdatepattern, and what is its relation to logdate?

Apologies, I tried to pear down the post and left that out accidentally.

I had also tried:

logdatepattern = logdate

and

logdatepattern = logdate.to_s

Here is the original post, fixed:

This does not work:

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”

puts “Getting log for #{logdate}”
File.open(“rdpconnect.log”).each do |line|
if line.match(logdate)
puts line
end
end

However, this does:

logdate = “2007-07-30”
puts “Getting log for #{logdate}”
File.open(“rdpconnect.log”).each do |line|
if line.match(logdate)
puts line
end
end

The format of logdate appears the same in both instances. What am I
missing?

Thanks,

On Jul 31, 2:47 pm, Dan D. [email protected] wrote:

This does not work:

logdate = “#removed_email_address@domain.invalid}-” + (@calendar1.month + 1).to_s +
[email protected]}”
#output is: 2007-07-30

You might also look at using Time#strftime to get your string:

t = Time.local(2007,7,30)
=> Mon Jul 30 00:00:00 -0500 2007
t.strftime(‘%Y-%m-%d’)
=> “2007-07-30”

Dan Z. wrote:

Dan D. wrote:

What is logdatepattern, and what is its relation to logdate?

    puts line
    puts line
end

end

The format of logdate appears the same in both instances. What am I
missing?

Thanks,

I don’t know what @calendar1 is, so I can’t reproduce this, but I
thought what Nobu meant when he replied to you is to say that the value
of that line contains 3 zeros, while the string you fed it contains 4.

Dan

You guys are geniuses! Thanks Nobu & Dan Z!

I feel SO dumb now.

Thanks!

Gordon T. wrote:

On Jul 31, 2:47 pm, Dan D. [email protected] wrote:

This does not work:

logdate = “#removed_email_address@domain.invalid}-” + (@calendar1.month + 1).to_s +
[email protected]}”
#output is: 2007-07-30

You might also look at using Time#strftime to get your string:

t = Time.local(2007,7,30)
=> Mon Jul 30 00:00:00 -0500 2007
t.strftime(‘%Y-%m-%d’)
=> “2007-07-30”

Good call, I ended up with this:

logdate = “#{@calendar1.year}-” + (@calendar1.month + 1).to_s +
“-#{@calendar1.day}”

=> “2007-7-30”
logdate = Date.parse(logdate).to_s
=> “2007-07-30”

Would Time#strftime be better?

Would Time#strftime be better?

Easier to read, I think. Or if you’re using a Date object, you could
use Date#strftime. It also looks like you’re adding a month to
@calendar1, which is easy with a Date object:

d1 = Date.civil(2007,06,30)
=> #<Date: 4908563/2,0,2299161>

d1.strftime(’%Y-%m-%d’)
=> “2007-06-30”

d2 = d1 >> 1
=> #<Date: 4908623/2,0,2299161>

d2.strftime(’%Y-%m-%d’)
=> “2007-07-30”

I see. Also easier to imagine the output when you call it as %Y-%m-%d.

@calendar1 is a gtk calendar widget, and within ruby calling
@calendar1.date
returns the date as 2007730, which caused Date.parse to crash as there
is only 1 part instead of the expected 3. That is why I used the
@calendar1.year @calendar1.month @calendar1.day, but @calendar1.month
returns 0 based months, which is why I used @calendar1.month + 1, which
works, but looks sloppy.

So, how would you parse a date formated as 2007730 into 2007-06-30? I
know that I could parse it out manually, but I’m assuming there is a
more elegant solution.

Thank you for your help.
Dan

@calendar1 is a gtk calendar widget, and within ruby calling
@calendar1.date
returns the date as 2007730, which caused Date.parse to crash as there
is only 1 part instead of the expected 3. That is why I used the
@calendar1.year @calendar1.month @calendar1.day, but @calendar1.month
returns 0 based months, which is why I used @calendar1.month + 1, which
works, but looks sloppy.

Ahh, I made some pretty bad assumptions about what you were trying to
do. I’m sorry.

So, how would you parse a date formated as 2007730 into 2007-06-30? I
know that I could parse it out manually, but I’m assuming there is a
more elegant solution.

I guess I would do this:

Date.civil(@calendar1.year, @calendar1.date + 1, @calendar1.day).to_s

Gordon T. wrote:

Ahh, I made some pretty bad assumptions about what you were trying to
do. I’m sorry.

So, how would you parse a date formated as 2007730 into 2007-06-30? I
know that I could parse it out manually, but I’m assuming there is a
more elegant solution.

I guess I would do this:

Date.civil(@calendar1.year, @calendar1.date + 1, @calendar1.day).to_s

Oh, no. Your suggestions were great. I combined your earlier suggestions
and came up with this:

logdate = Time.local(@calendar1.year, @calendar1.month +
1,@calendar1.day).strftime(’%Y-%m-%d’).to_s

which works the same as what you just mentioned:

logdate = Date.civil(@calendar1.year, @calendar1.month + 1,
@calendar1.day).to_s

Thanks a ton!

Dan

logdate = Date.parse(logdate).to_s

=> “2007-07-30”

Would Time#strftime be better?

Easier to read, I think. Or if you’re using a Date object, you could
use Date#strftime. It also looks like you’re adding a month to
@calendar1, which is easy with a Date object:

d1 = Date.civil(2007,06,30)
=> #<Date: 4908563/2,0,2299161>

d1.strftime(’%Y-%m-%d’)
=> “2007-06-30”

d2 = d1 >> 1
=> #<Date: 4908623/2,0,2299161>

d2.strftime(’%Y-%m-%d’)
=> “2007-07-30”