Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > help with string matching

Reply
Thread Tools

help with string matching

 
 
Dan Daniels
Guest
Posts: n/a
 
      07-31-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      07-31-2007
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

 
Reply With Quote
 
 
 
 
Dan Zwell
Guest
Posts: n/a
 
      07-31-2007
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?

 
Reply With Quote
 
Dan Daniels
Guest
Posts: n/a
 
      07-31-2007
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-forum.com/.

 
Reply With Quote
 
Dan Daniels
Guest
Posts: n/a
 
      07-31-2007
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-forum.com/.

 
Reply With Quote
 
Dan Zwell
Guest
Posts: n/a
 
      07-31-2007
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

 
Reply With Quote
 
Dan Daniels
Guest
Posts: n/a
 
      07-31-2007
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-forum.com/.

 
Reply With Quote
 
Gordon Thiesfeld
Guest
Posts: n/a
 
      07-31-2007
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"

 
Reply With Quote
 
Dan Daniels
Guest
Posts: n/a
 
      07-31-2007
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-forum.com/.

 
Reply With Quote
 
Gordon Thiesfeld
Guest
Posts: n/a
 
      07-31-2007
> 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"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with string matching algorythm Tomislav Kralj Ruby 5 08-14-2007 10:56 AM
Help: Matching last character of a string! Uwe Lammers Ruby 1 08-04-2007 03:25 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
compilation error: "error: no matching function for call to 'String::String(String)' =?ISO-8859-1?Q?Martin_J=F8rgensen?= C++ 5 05-06-2006 03:48 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments