In your example, 7-26-03 (the beginning of your example regex range) is a Saturday, so I don't really understand if it's relevant that weeks for you go from Monday to Sunday. Anyway, it's not that hard to generate a list of valid dates and then combine those into a regex string that you can plop right into your matching code. Here's a little bit of code to get you started:
use Time::Local 'timelocal_nocheck'; my ($m, $d, $y) = (8, 1, 2003); ## list dates between 0 and 6 days ago: my @dates; for my $days_ago (0 .. 6) { my @date_info = localtime timelocal_nocheck(0, 0, 0, $d-$days_ago, $ +m-1, $y-1900); push @dates, sprintf("%s-%s-%s", $date_info[4]+1, $date_info[3], $date_info[5]+1900); } ## convert list of dates to a regex my $regex = join "|", map quotemeta, @dates; print "$regex\n"; ## now you can do stuff like; ## if ($line =~ /^($regex)/) { ## print "this happened last week ($1)\n"; ## } __END__ 8\-1\-2003|7\-31\-2003|7\-30\-2003|7\-29\-2003|7\-28\-2003|7\-27\-2003 +|7\-26\-2003
This seems to output pretty much what you wanted, at least for 8-1-2003. If you're not familiar with Time::Local's timelocal_nocheck function, you should really take a look at it to see what the heck is going on here.. And if you really need these ranges to be aligned to your Mon-Sun weeks, you can calculate how many days to back up based on what day of the week today is (info you can get from localtime).

Unlike the other repliers, I prefer using Time::Local's timelocal_nocheck for date calculations whenever I can, because Time::Local is a standard module, and much much less overhead than Date::Calc or Date::Manip. It sometimes takes a little bit more work on your end to use Time::Local for date calculations. But I don't think you will find a pre-packaged solution for your problem anywhere, so elbow grease can't be avoided here. In this case, just subtracting days is a piece of cake for timelocal_nocheck. Rarely do I ever need to do computations that are whacked-out enough (or thousands of years away from the present) to require the heavy-duty modules.

blokhead


In reply to Re: Week Algorithm by blokhead
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.