[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

help with string matching

Dan Daniels

7/31/2007 7:48:00 PM

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

12 Answers

Nobuyoshi Nakada

7/31/2007 8:09:00 PM

0

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

--
Nobu Nakada

Dan Zwell

7/31/2007 8:17:00 PM

0

Dan Daniels wrote:
> 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?

Dan Daniels

7/31/2007 8:58:00 PM

0

Nobuyoshi Nakada wrote:
> 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

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

Dan Daniels

7/31/2007 9:03:00 PM

0

Dan Zwell wrote:
> Dan Daniels 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,
--
Posted via http://www.ruby-....

Dan Zwell

7/31/2007 9:14:00 PM

0

Dan Daniels wrote:
> Dan Zwell wrote:
>> Dan Daniels 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,

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 Daniels

7/31/2007 9:23:00 PM

0

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

Gordon Thiesfeld

7/31/2007 9:39:00 PM

0

On Jul 31, 2:47 pm, Dan Daniels <sdsurfg...@gmail.com> wrote:
> This does not work:
>
> logdate = "#...@calendar1.year}-" + (@calendar1.month + 1).to_s +
> "-...@calendar1.day}"
> #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 Daniels

7/31/2007 9:49:00 PM

0

Gordon Thiesfeld wrote:
> On Jul 31, 2:47 pm, Dan Daniels <sdsurfg...@gmail.com> wrote:
>> This does not work:
>>
>> logdate = "#...@calendar1.year}-" + (@calendar1.month + 1).to_s +
>> "-...@calendar1.day}"
>> #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?
--
Posted via http://www.ruby-....

Gordon Thiesfeld

7/31/2007 10:17:00 PM

0

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

Dan Daniels

7/31/2007 10:55:00 PM

0


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