I like this problem. Please take the following points as trying to be helpful, not as negative criticisms.

1. You say that the week "starts on Monday and ends on a Sunday" yet you provide an example which looks like Tuesday (8-19-03) through Monday (8-25-03). If you can better define what "last week" means, it would be easier to figure out if code met the spec. For example, is "today" ever part of "last week" or is it always the Mon-Sun that is completely before today?

2. You say you have "2 digits each for month, day and year" yet the examples you provide make it look like a single digit is accepted. Does the input you are trying to match have a leading zero for single digit months and days?

3. I don't think /^8\-[19-25]\-03/ does what you want it to. The character class in the middle (surrounded by square brackets) actually matches a single character of "1" or "9" through "5" or "5". The "9" through "5" is also going to be flagged as an invalid range since it starts with something higher than it ends.

4. You don't need to escape dashes (-) outside of character ranges.

5. If a regular expression is really the best approach, I think you are most likely going to end up with one that looks something like this:

$string =~ /^(7-26-03|7-27-03|7-28-03|7-29-03|7-30-03|7-31-03|8-1-03|8 +-2-03)/
This could be reduced by extrapolating the common portions, but you probably would not gain much and this format has the benefit that you can glance at it and know that it matches exactly the days you want.

6. Oh, yeah, I'd use Date::Manip to do the date calculations, but that's just because it's the only date manipulation package I've learned, it's portable (completely Perl), and it includes the kitchen sink. Be warned, though, that it's a bit tricky to understand the interface when you're starting out. Examples and testing help a lot.

-- Eric Hammond


In reply to Re: Week Algorithm by esh
in thread Week Algorithm by rupesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.