help with string matching

D

Dan Daniels

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
 
N

Nobuyoshi Nakada

Hi,

At Wed, 1 Aug 2007 04:47:32 +0900,
Dan Daniels 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"?
 
D

Dan Zwell

Dan said:
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

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

Dan Daniels

Nobuyoshi said:
Hi,

At Wed, 1 Aug 2007 04:47:32 +0900,
Dan Daniels 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
 
D

Dan Daniels

Dan said:
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,
 
D

Dan Zwell

Dan said:
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,

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
 
D

Dan Daniels

Dan said:
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!
 
D

Dan Daniels

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

=> "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?
 
G

Gordon Thiesfeld

logdate = Date.parse(logdate).to_s
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:
=> "2007-06-30"
=> "2007-07-30"
 
D

Dan Daniels

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:

=> "2007-06-30"

=> "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
 
G

Gordon Thiesfeld

@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
 
D

Dan Daniels

Gordon said:
Ahh, I made some pretty bad assumptions about what you were trying to
do. I'm sorry.


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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top