Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Help with date format

Expand Messages
  • Andrew Johnson
    ! OK.... ran it through the debugger and it says there are errors in line #14 ! not just one but several..... You are missing the closing brace on your while
    Message 1 of 21 , Sep 3, 2000
    • 0 Attachment
      ! OK.... ran it through the debugger and it says there are errors in line #14
      ! not just one but several.....

      You are missing the closing brace on your while loop, and you are
      introducing new variables without using my() (which you should be
      doing since you are using 'strict' ... which is a good thing). You
      are also using '/' in the character class and as the regex delimiter.

      Here's a slightly different version of Tom's that doesn't require
      the extra variables -- it will also simply leave the date as it is
      if it doesn't match the regex (we are using s<><> rather than s///):

      while (<IN>) {
      chomp;
      my @data = split /,/;
      $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>;
      print OUT join(',',@data),"\n";
      }


      Is that what you intended?

      regards,
      andrew

      --
      Andrew L. Johnson http://members.home.net/perl-epwp/
      It may be that your sole purpose in life is simply to
      serve as a warning to others.
    • joach@direct.ca
      ... YES........!!! This copy of the script come through without errors........ and does the job very nicely. This sort of format does not appear in the texts
      Message 2 of 21 , Sep 3, 2000
      • 0 Attachment
        >
        >
        > while (<IN>) {
        > chomp;
        > my @data = split /,/;
        > $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>;
        > print OUT join(',',@data),"\n";
        > }
        >
        > Is that what you intended?

        YES........!!!

        This copy of the script come through without errors........ and does the job very
        nicely.
        This sort of format does not appear in the texts I've read....or ....maybe just
        like now I$data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>; still don't
        understand it...... would you have the time to document what is happening in line

        $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>; I would like to
        apply what I'm learning here to other date formats like 2000-09-03 or
        2000/09/03........

        Thank you for taking the time to post .....much appreciate it

        John
      • Greg Webster
        Personally... To convert 20000803 (and be able to do cool things with the values individually), I d do something like the following: $wholedate = 20000803 ;
        Message 3 of 21 , Sep 3, 2000
        • 0 Attachment
          Personally...

          To convert 20000803 (and be able to do cool things with the values
          individually), I'd do something like the following:

          $wholedate = '20000803';
          @alldigits = split(//, $wholedate);
          $year = join('', @alldigits[0..3]);
          $month = join('', @alldigits[4..5]);
          $day = join('', @alldigits[6..7]);

          print "Year: $year : Month: $month : Day: $day\n";

          Then you can easily deal with all of the dates in bits. There's really no
          reason to complicate matters here.

          BTW, I'm sure is Awk type formats and $1, $2, $3 etc...this could be simplified
          further.

          Greg

          --
          < >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >
          | | | GREG WEBSTER - greg@... | | | |
          <_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>
        • Andrew Johnson
          ! the time to document what is happening in line ! ! $data[0] =~ s ; Firstly, you do not have to use / as the
          Message 4 of 21 , Sep 3, 2000
          • 0 Attachment
            ! the time to document what is happening in line
            !
            ! $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>;

            Firstly, you do not have to use / as the delimiters for m// and s///,
            you are free to use other single characters -- common choices are:
            m#pattern# and m|pattern| (same for s### and s|||) -- also, you can
            use paired delimiters like: m{pattern} and m<pattern> (or s{}{}, and
            s<><>). Using alternate delimiters makes things easier when your
            pattern contains / characters.

            Here is the regex with comments:

            $data[0] =~ s<
            ^ # match start of string
            ( # open $1 capture parens
            \d{4} # grab 4 digits into $1
            ) # close $1
            [ /-]? # optionally match a space, slash, or dash
            ( # open $2 capture
            \d\d # grab 2 digits into $2
            ) # close $2
            [ /-]? # optionally match a space, slash, or dash
            ( # open $3 capture
            \d\d # grab 2 digits into $3
            ) # close $3
            $ # match end of string
            >
            <$3$2$1>x;# replace with "$3$2$1", the 'x' modifier
            # allows these comments


            ! I would like to apply what I'm learning here to other date formats
            ! like 2000-09-03 or 2000/09/03........

            As you can see, the above already handles these formats by allowing
            for an optional space, slash, or dash between the groups -- this was
            inherent in Tom's posted reply.

            Other resources for learning about regular expressions are:

            perldoc perlre

            Mark Kvale has a couple of regex tutorials available:

            http://keck.ucsf.edu/~kvale/perlrequick.pod
            http://keck.ucsf.edu/~kvale/perlretut.pod

            The first is an introductory tutorial, the latter covers some more
            advanced features -- I also have one similar to Kvale's advanced one
            that I mostly finished before hearing about his (if I knew he was
            doing it I could have spent my time doing other things :-). You can
            find it at:

            http://members.home.net/andrew-johnson/perl/regextut.pod

            Lastly, I might as well mention that one of the two sample chapters
            for my book is on regular expressions and might be of use (it is a
            PDF file, free) -- grab chapter 10 at:

            http://www.manning.com/Johnson/Chapters.html

            Hope it helps.

            regards,
            andrew

            --
            Andrew L. Johnson http://members.home.net/perl-epwp/
            Doing linear scans over an associative array is like
            trying to club someone to death with a loaded Uzi.
            -- Larry Wall
          • joach@direct.ca
            Thank you for the detailed explanation ...... and the reading material.... There is more to the perl than one thinks....I like the way i covers the space,
            Message 5 of 21 , Sep 3, 2000
            • 0 Attachment
              Thank you for the detailed explanation ...... and the reading material....
              There is more to the perl than one thinks....I like the way i covers the
              space, slash, or dash .....all options are covered.

              John


              > $data[0] =~ s<
              > ^ # match start of string
              > ( # open $1 capture parens
              > \d{4} # grab 4 digits into $1
              > ) # close $1
              > [ /-]? # optionally match a space, slash, or dash
              > ( # open $2 capture
              > \d\d # grab 2 digits into $2
              > ) # close $2
              > [ /-]? # optionally match a space, slash, or dash
              > ( # open $3 capture
              > \d\d # grab 2 digits into $3
              > ) # close $3
              > $ # match end of string
              > >
              > <$3$2$1>x;# replace with "$3$2$1", the 'x' modifier
              > # allows these comments
              >
              > ! I would like to apply what I'm learning here to other date formats
              > ! like 2000-09-03 or 2000/09/03........
              >
            • Maisha Walker
              Andrew - that was a beautiful and suprisingly simple explanation of what was going on in that regex. Thank you!! maisha ... From: Andrew Johnson
              Message 6 of 21 , Sep 4, 2000
              • 0 Attachment
                Andrew - that was a beautiful and suprisingly simple explanation of what was
                going on in that regex.

                Thank you!!

                maisha
                -----Original Message-----
                From: Andrew Johnson <andrew-johnson@...>


                ! the time to document what is happening in line
                !
                ! $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>;

                Firstly, you do not have to use / as the delimiters for m// and s///,
                you are free to use other single characters -- common choices are:
                m#pattern# and m|pattern| (same for s### and s|||) -- also, you can
                use paired delimiters like: m{pattern} and m<pattern> (or s{}{}, and
                s<><>). Using alternate delimiters makes things easier when your
                pattern contains / characters.

                Here is the regex with comments:

                $data[0] =~ s<
                ^ # match start of string
                ( # open $1 capture parens
                \d{4} # grab 4 digits into $1
                ) # close $1
                [ /-]? # optionally match a space, slash, or dash
                ( # open $2 capture
                \d\d # grab 2 digits into $2
                ) # close $2
                [ /-]? # optionally match a space, slash, or dash
                ( # open $3 capture
                \d\d # grab 2 digits into $3
                ) # close $3
                $ # match end of string
                >
                <$3$2$1>x;# replace with "$3$2$1", the 'x' modifier
                # allows these comments


                ! I would like to apply what I'm learning here to other date formats
                ! like 2000-09-03 or 2000/09/03........

                As you can see, the above already handles these formats by allowing
                for an optional space, slash, or dash between the groups -- this was
                inherent in Tom's posted reply.

                Other resources for learning about regular expressions are:

                perldoc perlre

                Mark Kvale has a couple of regex tutorials available:

                http://keck.ucsf.edu/~kvale/perlrequick.pod
                http://keck.ucsf.edu/~kvale/perlretut.pod

                The first is an introductory tutorial, the latter covers some more
                advanced features -- I also have one similar to Kvale's advanced one
                that I mostly finished before hearing about his (if I knew he was
                doing it I could have spent my time doing other things :-). You can
                find it at:

                http://members.home.net/andrew-johnson/perl/regextut.pod

                Lastly, I might as well mention that one of the two sample chapters
                for my book is on regular expressions and might be of use (it is a
                PDF file, free) -- grab chapter 10 at:

                http://www.manning.com/Johnson/Chapters.html

                Hope it helps.

                regards,
                andrew

                --
                Andrew L. Johnson http://members.home.net/perl-epwp/
                Doing linear scans over an associative array is like
                trying to club someone to death with a loaded Uzi.
                -- Larry Wall
              • Maisha Walker
                just curious everyone, but why use d d instead of d{2} - even just for consistency sake? is there a particular reason for using d d instead? maisha
                Message 7 of 21 , Sep 4, 2000
                • 0 Attachment
                  just curious everyone, but why use \d\d instead of \d{2} - even just for
                  consistency sake?
                  is there a particular reason for using \d\d instead?

                  maisha

                  $data[0] =~ s<^(\d{4})[ /-]?(\d\d)[ /-]?(\d\d)$><$3$2$1>;
                • Maisha Walker
                  hey, isn t the problem with this though, that you cannot as simply account for the addition of spaces, dashes or slashes to the date? that was one of the
                  Message 8 of 21 , Sep 4, 2000
                  • 0 Attachment
                    hey, isn't the problem with this though, that you cannot as simply account
                    for the addition of spaces, dashes or slashes to the date? that was one of
                    the prerequisites of the person who had the question.

                    for example, what if your date looked like this:
                    2000-08-23

                    or if it sometimes looked like the above and sometimes looked like this:
                    20000823

                    the program below would not work anymore.

                    i'm a beginner, so let me know if i've missed something!

                    maisha
                    -----Original Message-----
                    From: Greg Webster <greg@...>
                    To: perl-beginner@egroups.com <perl-beginner@egroups.com>
                    Date: Sunday, September 03, 2000 10:26 PM
                    Subject: Re: [PBML] Help with date format




                    Personally...

                    To convert 20000803 (and be able to do cool things with the values
                    individually), I'd do something like the following:

                    $wholedate = '20000803';
                    @alldigits = split(//, $wholedate);
                    $year = join('', @alldigits[0..3]);
                    $month = join('', @alldigits[4..5]);
                    $day = join('', @alldigits[6..7]);

                    print "Year: $year : Month: $month : Day: $day\n";

                    Then you can easily deal with all of the dates in bits. There's really no
                    reason to complicate matters here.

                    BTW, I'm sure is Awk type formats and $1, $2, $3 etc...this could be
                    simplified
                    further.

                    Greg

                    --
                    < >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >
                    | | | GREG WEBSTER - greg@... | | | |
                    <_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>
                  • Oliver Manickum
                    Hey Maisha, I would try to compensate for both.. ie. if a user sometimes used 2000-05-03 or sometimes used 20000503 then i would first do this $date =
                    Message 9 of 21 , Sep 4, 2000
                    • 0 Attachment
                      RE: [PBML] Help with date format

                      Hey Maisha,

                              I would try to compensate for both.. ie.        if a user sometimes used 2000-05-03 or sometimes used 20000503 then i would first do this

                              $date = '2000/05/03';
                              $date =~ s/\-\/\\//; # Accomodate for a - or a \ or a / or whateva else the user might separate the date by.....

                             
                              then u can play with it in the form of 20000505

                      - Olly
                      Oliver Manickum
                      ____________________________
                      ePages Internet & Multimedia
                      http://www.epages.net/
                      Tel : +27 (0)31 2651281

                      -----Original Message-----
                      From: Maisha Walker [mailto:maisha@...]
                      Sent: Monday, September 04, 2000 4:00 PM
                      To: perl-beginner@egroups.com
                      Subject: Re: [PBML] Help with date format


                      -------------------------- eGroups Sponsor -------------------------~-~>
                      The Five Secrets Of A Successful Product Launch
                      FREE right now at:
                      http://click.egroups.com/1/8592/15/_/12898/_/968076180/
                      ---------------------------------------------------------------------_->

                      hey, isn't the problem with this though, that you cannot as simply account
                      for the addition of spaces, dashes or slashes to the date?  that was one of
                      the prerequisites of the person who had the question.

                      for example, what if your date looked like this:
                      2000-08-23

                      or if it sometimes looked like the above and sometimes looked like this:
                      20000823

                      the program below would not work anymore.

                      i'm a beginner, so let me know if i've missed something!

                      maisha

                    • Maisha Walker
                      oh yeah - that *is* simple! Thank you! maisha ... From: Oliver Manickum To: perl-beginner@egroups.com Date:
                      Message 10 of 21 , Sep 4, 2000
                      • 0 Attachment
                        RE: [PBML] Help with date format
                        oh yeah - that *is* simple!
                         
                        Thank you!
                        maisha
                        -----Original Message-----
                        From: Oliver Manickum <oliver@...>
                        To: perl-beginner@egroups.com <perl-beginner@egroups.com>
                        Date: Monday, September 04, 2000 10:17 AM
                        Subject: RE: [PBML] Help with date format


                        Hey Maisha,

                                I would try to compensate for both.. ie.        if a user sometimes used 2000-05-03 or sometimes used 20000503 then i would first do this

                                $date = '2000/05/03';
                                $date =~ s/\-\/\\//; # Accomodate for a - or a \ or a / or whateva else the user might separate the date by.....

                               
                                then u can play with it in the form of 20000505

                        - Olly
                        Oliver Manickum
                        ____________________________
                        ePages Internet & Multimedia
                        http://www.epages.net/
                        Tel : +27 (0)31 2651281

                        -----Original Message-----
                        From: Maisha Walker [mailto:maisha@...]
                        Sent: Monday, September 04, 2000 4:00 PM
                        To: perl-beginner@egroups.com
                        Subject: Re: [PBML] Help with date format


                        -------------------------- eGroups Sponsor -------------------------~-~>
                        The Five Secrets Of A Successful Product Launch
                        FREE right now at:
                        http://click.egroups.com/1/8592/15/_/12898/_/968076180/
                        ---------------------------------------------------------------------_->

                        hey, isn't the problem with this though, that you cannot as simply account
                        for the addition of spaces, dashes or slashes to the date?  that was one of
                        the prerequisites of the person who had the question.

                        for example, what if your date looked like this:
                        2000-08-23

                        or if it sometimes looked like the above and sometimes looked like this:
                        20000823

                        the program below would not work anymore.

                        i'm a beginner, so let me know if i've missed something!

                        maisha

                      • Andrew Johnson
                        ! just curious everyone, but why use d d instead of d{2} - even just ! for consistency sake? is there a particular reason for using d d ! instead? To be
                        Message 11 of 21 , Sep 4, 2000
                        • 0 Attachment
                          ! just curious everyone, but why use \d\d instead of \d{2} - even just
                          ! for consistency sake? is there a particular reason for using \d\d
                          ! instead?

                          To be honest, here is why I did it that way:

                          \d{4} is shorter than \d\d\d\d
                          \d\d is shorter than \d{2}

                          :-)

                          andrew

                          --
                          Andrew L. Johnson http://members.home.net/andrew-johnson/
                          The generation of random numbers is too
                          important to be left to chance.
                        • Greg Webster
                          Sorry, you re right...but this is easily accomplished with one line. $wholedate = 20000803 ; $wholedate =~ s/[ /- .//g; #removes / - and . Add as
                          Message 12 of 21 , Sep 4, 2000
                          • 0 Attachment
                            Sorry, you're right...but this is easily accomplished with one line.

                            $wholedate = '20000803';
                            $wholedate =~ s/[\/-\\\.//g; #removes / - \ and . Add as needed
                            @alldigits = split(//, $wholedate);
                            $year = join('', @alldigits[0..3]);
                            $month = join('', @alldigits[4..5]);
                            $day = join('', @alldigits[6..7]);

                            print "Year: $year : Month: $month : Day: $day\n";

                            Greg

                            On Mon, 04 Sep 2000, you wrote:
                            >
                            > hey, isn't the problem with this though, that you cannot as simply account
                            > for the addition of spaces, dashes or slashes to the date? that was one of
                            > the prerequisites of the person who had the question.
                            >
                            > for example, what if your date looked like this:
                            > 2000-08-23
                            >
                            > or if it sometimes looked like the above and sometimes looked like this:
                            > 20000823
                            >
                            > the program below would not work anymore.
                            >
                            > i'm a beginner, so let me know if i've missed something!
                            >
                            > maisha
                            > -----Original Message-----
                            > From: Greg Webster <greg@...>
                            > To: perl-beginner@egroups.com <perl-beginner@egroups.com>
                            > Date: Sunday, September 03, 2000 10:26 PM
                            > Subject: Re: [PBML] Help with date format
                            >
                            >
                            >
                            >
                            > Personally...
                            >
                            > To convert 20000803 (and be able to do cool things with the values
                            > individually), I'd do something like the following:
                            >
                            > $wholedate = '20000803';
                            > @alldigits = split(//, $wholedate);
                            > $year = join('', @alldigits[0..3]);
                            > $month = join('', @alldigits[4..5]);
                            > $day = join('', @alldigits[6..7]);
                            >
                            > print "Year: $year : Month: $month : Day: $day\n";
                            >
                            > Then you can easily deal with all of the dates in bits. There's really no
                            > reason to complicate matters here.
                            >
                            > BTW, I'm sure is Awk type formats and $1, $2, $3 etc...this could be
                            > simplified
                            > further.
                            >
                            > Greg
                            >
                            > --
                            > < >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >
                            > | | | GREG WEBSTER - greg@... | | | |
                            > <_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>
                            --
                            < >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >
                            | | | GREG WEBSTER - greg@... | | | |
                            <_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>
                          • Andrew Johnson
                            ! Sorry, you re right...but this is easily accomplished with one line. ! ! $wholedate = 20000803 ; ! $wholedate =~ s/[ /- .//g; #removes / - and . Add
                            Message 13 of 21 , Sep 4, 2000
                            • 0 Attachment
                              ! Sorry, you're right...but this is easily accomplished with one line.
                              !
                              ! $wholedate = '20000803';
                              ! $wholedate =~ s/[\/-\\\.//g; #removes / - \ and . Add as needed

                              Ouch! Greg, besides forgetting the closing ] on the character class,
                              you do realize that your regex will remove a whole lot more than you
                              think it does (removing all upper case letters and all digits, and
                              several punctuation characters -- and it won't remove the '-'
                              character).

                              I think you meant: [\/\\\.-] , placement of the '-' is crucial inside
                              of character classes.

                              regards,
                              andrew

                              --
                              Andrew L. Johnson http://members.home.net/andrew-johnson/
                              I drink to make other people interesting.
                              -- George Jean Nathan
                            • Greg Webster
                              Heh, I really should test what I write. Forgive me, it s been a hell of a week. You are absolutely right about the placement and order. Greg ... -- -
                              Message 14 of 21 , Sep 4, 2000
                              • 0 Attachment
                                Heh, I really should test what I write. Forgive me, it's been a hell of a week.
                                You are absolutely right about the placement and order.

                                Greg

                                On Mon, 04 Sep 2000, you wrote:
                                > Ouch! Greg, besides forgetting the closing ] on the character class,
                                > you do realize that your regex will remove a whole lot more than you
                                > think it does (removing all upper case letters and all digits, and
                                > several punctuation characters -- and it won't remove the '-'
                                > character).
                                >
                                > I think you meant: [\/\\\.-] , placement of the '-' is crucial inside
                                > of character classes.
                                >
                                > regards,
                                > andrew
                                >
                                > --
                                > Andrew L. Johnson http://members.home.net/andrew-johnson/
                                > I drink to make other people interesting.
                                > -- George Jean Nathan
                                --
                                < >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >-< >< >
                                | | | GREG WEBSTER - greg@... | | | |
                                <_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>-<_><_>
                              • Maisha Walker
                                ha ha ha - that s great! maisha ... From: Andrew Johnson ! just curious everyone, but why use d d instead of d{2} - even just ! for
                                Message 15 of 21 , Sep 4, 2000
                                • 0 Attachment
                                  ha ha ha - that's great!

                                  maisha
                                  -----Original Message-----
                                  From: Andrew Johnson <andrew-johnson@...>

                                  ! just curious everyone, but why use \d\d instead of \d{2} - even just
                                  ! for consistency sake? is there a particular reason for using \d\d
                                  ! instead?

                                  To be honest, here is why I did it that way:

                                  \d{4} is shorter than \d\d\d\d
                                  \d\d is shorter than \d{2}

                                  :-)

                                  andrew
                                • Smith, Sheldon
                                  That s right. Allowing for optional delimiters was exactly what Andrew Johnson s regex did (with the pattern [ /-]? ). (See earlier post.) Simple substring
                                  Message 16 of 21 , Sep 6, 2000
                                  • 0 Attachment
                                    That's right. Allowing for optional delimiters was exactly what Andrew
                                    Johnson's regex did (with the pattern "[ /-]?"). (See earlier post.) Simple
                                    substring extractions just can't get the job done.

                                    Shel Smith

                                    > -----Original Message-----
                                    > From: Maisha Walker [mailto:maisha@...]
                                    > Sent: Monday, September 04, 2000 9:00 AM
                                    > To: perl-beginner@egroups.com
                                    > Subject: Re: [PBML] Help with date format
                                    >
                                    > hey, isn't the problem with this though, that you cannot as
                                    > simply account
                                    > for the addition of spaces, dashes or slashes to the date?
                                    > that was one of
                                    > the prerequisites of the person who had the question.
                                    >
                                    > for example, what if your date looked like this:
                                    > 2000-08-23
                                    >
                                    > or if it sometimes looked like the above and sometimes looked
                                    > like this:
                                    > 20000823
                                  Your message has been successfully submitted and would be delivered to recipients shortly.