Professional Web Applications Themes

Incrementing during a regexp substitution - PERL Beginners

Is there a faster and/or cleaner way to do the following: # # End date is a year after start date # $edate = $sdate; $edate =~ s/\///g; $edate++; $edate =~ s/.*(\d{4})$/01\/04\/$1/; where $sdate is a UK date of the type "dd/mm/yyyy" I've tried ($edate = $sdate) =~ s/(.*)(\d)$/$1($2++)/e; to no avail. I think I'm misunderstanding the /e modifier here? Cheers, Lee...

  1. #1

    Default Incrementing during a regexp substitution

    Is there a faster and/or cleaner way to do the following:

    #
    # End date is a year after start date
    #
    $edate = $sdate;
    $edate =~ s/\///g;
    $edate++;
    $edate =~ s/.*(\d{4})$/01\/04\/$1/;

    where $sdate is a UK date of the type "dd/mm/yyyy"

    I've tried ($edate = $sdate) =~ s/(.*)(\d)$/$1($2++)/e; to no avail. I think
    I'm misunderstanding the /e modifier here?

    Cheers,

    Lee
    Lee Guest

  2. #2

    Default Re: Incrementing during a regexp substitution

    On May 13, Lee Johnson said:
     

    Are you sure you want to just use April 1st (or January 4th) always? You
    don't want to use the day and month in $sdate?
     

    A little bit, yes. The /e modifier makes the right-hand side Perl code,
    and you should recognize that

    $1($2++)

    isn't valid Perl code. You'd need

    $1 . ($2++)

    but that won't work for two reasons. First, $x++ returns its OLD value,
    and THEN increments it, so you wouldn't get a changed value. The second
    reason is that you can't modify the $DIGIT variables.

    Finally, your code is making a mistake in the regex. What if the year is
    2009? You're only matching the LAST digit of the year, and adding one to
    it. Your string would end up being "...20010", which is wrong. Match the
    whole four-digit year, and add one to it. When it rolls around to 9999,
    then come back to me and complain about my bad code. ;)

    And your regex is doing more work than it has to. Why would you write

    s/(.*)(\d{4})$/$1 . (1 + $2)/e;

    when you could just write

    s/(\d{4})$/1 + $2/e;

    You're matching the WHOLE beginning of the string just to replace it with
    ITSELF. That's silly and inefficient.

    --
    Jeff "japhy" Pinyan com http://www.pobox.com/~japhy/
    RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
    CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

    Jeff Guest

  3. #3

    Default Re: Incrementing during a regexp substitution

    Jeff 'Japhy' Pinyan wrote: 
    >
    > Are you sure you want to just use April 1st (or January 4th) always? You
    > don't want to use the day and month in $sdate?

    >
    > A little bit, yes. The /e modifier makes the right-hand side Perl code,
    > and you should recognize that
    >
    > $1($2++)
    >
    > isn't valid Perl code. You'd need
    >
    > $1 . ($2++)
    >
    > but that won't work for two reasons. First, $x++ returns its OLD value,
    > and THEN increments it, so you wouldn't get a changed value. The second
    > reason is that you can't modify the $DIGIT variables.
    >
    > Finally, your code is making a mistake in the regex. What if the year is
    > 2009? You're only matching the LAST digit of the year, and adding one to
    > it. Your string would end up being "...20010", which is wrong. Match the
    > whole four-digit year, and add one to it. When it rolls around to 9999,
    > then come back to me and complain about my bad code. ;)[/ref]

    Also, if the current date is 29/02/2004 and you increment the year to
    2005...


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. incrementing a date by one day ??
    By nowB@ webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: January 25th, 11:31 PM
  2. [PHP] incrementing string value
    By Carl Furst in forum PHP Development
    Replies: 1
    Last Post: July 28th, 09:38 AM
  3. incrementing string value
    By Jeremy in forum PHP Development
    Replies: 5
    Last Post: July 26th, 12:14 AM
  4. Incrementing Dates
    By Daniel G. Emilio in forum FileMaker
    Replies: 10
    Last Post: July 14th, 06:35 AM
  5. Incrementing a value on a rolling basis
    By Jim in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 10th, 02:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139