help with string matching

Discussion in 'Ruby' started by Dan Daniels, Jul 31, 2007.

  1. Dan Daniels

    Dan Daniels Guest

    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/.
     
    Dan Daniels, Jul 31, 2007
    #1
    1. Advertising

  2. 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
     
    Nobuyoshi Nakada, Jul 31, 2007
    #2
    1. Advertising

  3. Dan Daniels

    Dan Zwell Guest

    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 Zwell, Jul 31, 2007
    #3
  4. Dan Daniels

    Dan Daniels Guest

    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/.
     
    Dan Daniels, Jul 31, 2007
    #4
  5. Dan Daniels

    Dan Daniels Guest

    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/.
     
    Dan Daniels, Jul 31, 2007
    #5
  6. Dan Daniels

    Dan Zwell Guest

    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 Zwell, Jul 31, 2007
    #6
  7. Dan Daniels

    Dan Daniels Guest

    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/.
     
    Dan Daniels, Jul 31, 2007
    #7
  8. On Jul 31, 2:47 pm, Dan Daniels <> wrote:
    > This does not work:
    >
    > logdate = "#}-" + (@calendar1.month + 1).to_s +
    > "}"
    > #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"
     
    Gordon Thiesfeld, Jul 31, 2007
    #8
  9. Dan Daniels

    Dan Daniels Guest

    Gordon Thiesfeld wrote:
    > On Jul 31, 2:47 pm, Dan Daniels <> wrote:
    >> This does not work:
    >>
    >> logdate = "#}-" + (@calendar1.month + 1).to_s +
    >> "}"
    >> #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/.
     
    Dan Daniels, Jul 31, 2007
    #9
  10. > 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"
     
    Gordon Thiesfeld, Jul 31, 2007
    #10
  11. Dan Daniels

    Dan Daniels Guest


    >> 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-forum.com/.
     
    Dan Daniels, Jul 31, 2007
    #11
  12. > @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 Thiesfeld, Aug 1, 2007
    #12
  13. Dan Daniels

    Dan Daniels Guest

    Gordon Thiesfeld 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Dan Daniels, Aug 1, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,338
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  2. Uwe Lammers
    Replies:
    1
    Views:
    88
    Hermann Martinelli
    Aug 4, 2007
  3. Tomislav Kralj

    Help with string matching algorythm

    Tomislav Kralj, Aug 9, 2007, in forum: Ruby
    Replies:
    5
    Views:
    141
    Han Holl
    Aug 14, 2007
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    260
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    263
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page