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

[PBML] Prepending a value

Expand Messages
  • joachim@uniserve.com
    I am working with data that come as yymmdd...... but I would like to have a script that will rewrite the data as yyyymmdd This script is sort of no quite
    Message 1 of 6 , Feb 21, 2000
    • 0 Attachment
      I am working with data that come as yymmdd...... but I would like to
      have a script that will rewrite the data as yyyymmdd This script is
      sort of no quite doing that...... it just prepends 20 to all the strings
      in the data. Any ideas....???

      John

      #!/usr/bin/perl
      $infile = shift;
      $outfile = shift;
      $prepend = shift;
      $validator=$_;

      # usage: perl prepend.pl infile outfile prepend_string
      # example: perl prepend.pl msft.dat msft.csv 20
      open(IN, $infile) || die("Can't open input file $infile - $!");
      open(OUT, ">$outfile") || die("Can't open output file $outfile - $!");
      while (<IN>) {

      $count = 0;
      while (<IN>) {
      if ($count > 0) {

      if ( $validator > 500000)
      {$pre= '19';}

      elsif ( $validator >= 0 and $validator < 900000 )
      {$pre= '20';}

      else {die ("Program error. \$validator is a negative number.");}

      $_ = $pre.$_;
      print OUT $_;
      }
      $count++;
      }
      }
      close IN;
      close OUT;
      __END__
    • Jeff Boes
      ... No problem. ... I m really unsure why you need this. ... Personally, I d replace all this validator stuff with the following block: die Invalid input:
      Message 2 of 6 , Feb 21, 2000
      • 0 Attachment
        > -----Original Message-----
        > From: joachim@... [mailto:joachim@...]
        > Sent: Monday, February 21, 2000 5:03 AM
        > To: perl-beginner@egroups.com
        > Subject: [PBML] Prepending a value
        >
        >
        > I am working with data that come as yymmdd...... but I would like to
        > have a script that will rewrite the data as yyyymmdd This script is
        > sort of no quite doing that...... it just prepends 20 to all the strings
        > in the data. Any ideas....???

        No problem.

        >
        > John
        >
        > #!/usr/bin/perl
        > $infile = shift;
        > $outfile = shift;
        > $prepend = shift;
        > $validator=$_;

        I'm really unsure why you need this.

        >
        > # usage: perl prepend.pl infile outfile prepend_string
        > # example: perl prepend.pl msft.dat msft.csv 20
        > open(IN, $infile) || die("Can't open input file $infile - $!");
        > open(OUT, ">$outfile") || die("Can't open output file $outfile - $!");
        > while (<IN>) {
        >
        > $count = 0;
        > while (<IN>) {
        > if ($count > 0) {
        >
        > if ( $validator > 500000)
        > {$pre= '19';}
        >
        > elsif ( $validator >= 0 and $validator < 900000 )
        > {$pre= '20';}
        >
        > else {die ("Program error. \$validator is a negative number.");}
        >
        > $_ = $pre.$_;
        > print OUT $_;
        > }

        Personally, I'd replace all this 'validator' stuff with the following block:

        die "Invalid input: must start with at least 6 digits"
        unless /(\d\d\d\d\d\d)(..)/;

        # Now $1 contains the first 6 digits, while $2 contains the next two
        characters.
        # If $2 is also digits, then this MAY be a 'yyyymmdd' date already, and we
        shouldn't
        # 'fix' it. Otherwise, prepend as long as the result is a valid date.

        if ($2 =~ /\d\d/) {
        # Probably already 'yyyymmdd'. Optionally, you could check to make sure
        here.
        next;
        }
        else {
        $maybe_month = 0 + substr($1, 2, 2);
        if ($maybe_month <= 0 || $maybe_month > 12) {
        warn "Invalid date in line '$_'";
        next;
        }
        $maybe_day = 0 + substr($1, 4, 2);
        if ($maybe_day <= 0 || $maybe_day > 31 # Note: no Feb, 30-day-month, or
        leap year testing!
        ) {
        warn "Invalid date in line '$_';
        next;
        }
        $_ = '20' . $_;
        ++$count;
        }
        continue {
        print OUT $_;
        }


        Or something like this. I don't have time to test this thoroughly, but I've
        done most of the work here...


        > $count++;
        > }
        > }
        > close IN;
        > close OUT;
        > __END__
        >
        >
        >
        > ------------------------------------------------------------------------
        > Registering a domain name is quick and easy.
        > http://click.egroups.com/1/1611/1/_/12898/_/951127394/
        >
        > eGroups.com Home: http://www.egroups.com/group/perl-beginner/
        > http://www.egroups.com - Simplifying group communications
        >
        >
        ----
        If you saw it from a satellite/With its green and its blue and white
        The beauty of the curve of the earth/And its oceans below
        You might think it was paradise/If you didn't know
        You might think that it's turning/But it's turning so slow.
        --Jackson Browne, "How Long"
        ___________
        Jeff Boes <>< jboes@...
        Mur Consulting http://www.qtm.net/~jboes/
      • joachim@uniserve.com
        Thank you Jeff....... this is very informative and helpful. But only one thing...... the data comes a yymmdd........ as in 991212 through to 000221 My
        Message 3 of 6 , Feb 21, 2000
        • 0 Attachment
          Thank you Jeff....... this is very informative and helpful.    But only one thing...... the data comes a yymmdd........ as in 991212  through to 000221    My attempt would be to have these dates returned as 19991212 throught to 20000221   So we need to look at the logic as to when to prepend 19 or when to prepend 20
           

          John

          Jeff Boes wrote:

          > -----Original Message-----
          > From: joachim@... [mailto:joachim@...]
          > Sent: Monday, February 21, 2000 5:03 AM
          > To: perl-beginner@egroups.com
          > Subject: [PBML] Prepending a value
          >
          >
          > I am working with data that come as yymmdd...... but I would like to
          > have a script that will rewrite the data as yyyymmdd    This script is
          > sort of no quite doing that...... it just prepends 20 to all the strings
          > in the data.    Any ideas....???

          No problem.

          >
          > John
          >
          > #!/usr/bin/perl
          > $infile = shift;
          > $outfile = shift;
          > $prepend = shift;
          > $validator=$_;

          I'm really unsure why you need this.

          >
          > # usage: perl prepend.pl infile outfile prepend_string
          > # example: perl prepend.pl msft.dat msft.csv 20
          > open(IN, $infile) || die("Can't open input file $infile - $!");
          > open(OUT, ">$outfile") || die("Can't open output file $outfile - $!");
          > while (<IN>) {
          >
          > $count = 0;
          > while (<IN>) {
          >  if ($count > 0) {
          >
          >  if ( $validator > 500000)
          >          {$pre= '19';}
          >
          >  elsif ( $validator >= 0 and $validator < 900000 )
          >         {$pre= '20';}
          >
          >  else {die ("Program error.  \$validator is a negative number.");}
          >
          >         $_ = $pre.$_;
          >         print OUT $_;
          >    }

          Personally, I'd replace all this 'validator' stuff with the following block:

          die "Invalid input: must start with at least 6 digits"
           unless /(\d\d\d\d\d\d)(..)/;

          # Now $1 contains the first 6 digits, while $2 contains the next two
          characters.
          # If $2 is also digits, then this MAY be a 'yyyymmdd' date already, and we
          shouldn't
          # 'fix' it. Otherwise, prepend as long as the result is a valid date.

          if ($2 =~ /\d\d/) {
            # Probably already 'yyyymmdd'. Optionally, you could check to make sure
          here.
            next;
          }
          else {
            $maybe_month = 0 + substr($1, 2, 2);
            if ($maybe_month <= 0 || $maybe_month > 12) {
              warn "Invalid date in line '$_'";
              next;
            }
            $maybe_day = 0 + substr($1, 4, 2);
            if ($maybe_day <= 0 || $maybe_day > 31    # Note: no Feb, 30-day-month, or
          leap year testing!
              ) {
              warn "Invalid date in line '$_';
              next;
            }
            $_ = '20' . $_;
            ++$count;
            }
          continue {
            print OUT $_;
          }

          Or something like this. I don't have time to test this thoroughly, but I've
          done most of the work here...

          >  $count++;
          > }
          > }
          > close IN;
          > close OUT;
          > __END__
          >
          >
          >
          > ------------------------------------------------------------------------
          > Registering a domain name is quick and easy.
          > http://click.egroups.com/1/1611/1/_/12898/_/951127394/
          >
          > eGroups.com Home: http://www.egroups.com/group/perl-beginner/
          > http://www.egroups.com - Simplifying group communications
          >
          >
          ----
          If you saw it from a satellite/With its green and its blue and white
          The beauty of the curve of the earth/And its oceans below
          You might think it was paradise/If you didn't know
          You might think that it's turning/But it's turning so slow.
          --Jackson Browne, "How Long"
          ___________
          Jeff Boes  <><  jboes@...
          Mur Consulting  http://www.qtm.net/~jboes/

          ------------------------------------------------------------------------
          Registering a domain name is quick and easy.
          http://click.egroups.com/1/1611/1/_/12898/_/951140288/

          -- Check out your group's private Chat room
          -- http://www.egroups.com/ChatPage?listName=perl-beginner&m=1

        • Jeff Boes
          Ah, I didn t pick that up from the original message. All you need to do then, is to capture the first two digits from the date: $the_date = $1; $the_date =~
          Message 4 of 6 , Feb 21, 2000
          • 0 Attachment
            Ah, I didn't pick that up from the original message. All you need to do then, is to capture the first two digits from the date:
             
            $the_date = $1;
            $the_date =~ /(\d\d)/;
            $the_year = $1;
             
            And then examine $the_year to see if it's greater than THRESHOLD, where that threshold value is the 'split' between prepending with 19 and with 20. If you know the dates are always in the past, then THRESHOLD can be '00'.
             
             
            -----Original Message-----
            From: joachim@... [mailto:joachim@...]
            Sent: Monday, February 21, 2000 9:31 AM
            To: perl-beginner@egroups.com
            Subject: [PBML] Re: Prepending a value

            Thank you Jeff....... this is very informative and helpful.    But only one thing...... the data comes a yymmdd........ as in 991212  through to 000221    My attempt would be to have these dates returned as 19991212 throught to 20000221   So we need to look at the logic as to when to prepend 19 or when to prepend 20
             

            John

            Jeff Boes wrote:

            > -----Original Message-----
            > From: joachim@... [mailto:joachim@...]
            > Sent: Monday, February 21, 2000 5:03 AM
            > To: perl-beginner@egroups.com
            > Subject: [PBML] Prepending a value
            >
            >
            > I am working with data that come as yymmdd...... but I would like to
            > have a script that will rewrite the data as yyyymmdd    This script is
            > sort of no quite doing that...... it just prepends 20 to all the strings
            > in the data.    Any ideas....???

            No problem.

            >
            > John
            >
            > #!/usr/bin/perl
            > $infile = shift;
            > $outfile = shift;
            > $prepend = shift;
            > $validator=$_;

            I'm really unsure why you need this.

            >
            > # usage: perl prepend.pl infile outfile prepend_string
            > # example: perl prepend.pl msft.dat msft.csv 20
            > open(IN, $infile) || die("Can't open input file $infile - $!");
            > open(OUT, ">$outfile") || die("Can't open output file $outfile - $!");
            > while (<IN>) {
            >
            > $count = 0;
            > while (<IN>) {
            >  if ($count > 0) {
            >
            >  if ( $validator > 500000)
            >          {$pre= '19';}
            >
            >  elsif ( $validator >= 0 and $validator < 900000 )
            >         {$pre= '20';}
            >
            >  else {die ("Program error.  \$validator is a negative number.");}
            >
            >         $_ = $pre.$_;
            >         print OUT $_;
            >    }

            Personally, I'd replace all this 'validator' stuff with the following block:

            die "Invalid input: must start with at least 6 digits"
             unless /(\d\d\d\d\d\d)(..)/;

            # Now $1 contains the first 6 digits, while $2 contains the next two
            characters.
            # If $2 is also digits, then this MAY be a 'yyyymmdd' date already, and we
            shouldn't
            # 'fix' it. Otherwise, prepend as long as the result is a valid date.

            if ($2 =~ /\d\d/) {
              # Probably already 'yyyymmdd'. Optionally, you could check to make sure
            here.
              next;
            }
            else {
              $maybe_month = 0 + substr($1, 2, 2);
              if ($maybe_month <= 0 || $maybe_month > 12) {
                warn "Invalid date in line '$_'";
                next;
              }
              $maybe_day = 0 + substr($1, 4, 2);
              if ($maybe_day <= 0 || $maybe_day > 31    # Note: no Feb, 30-day-month, or
            leap year testing!
                ) {
                warn "Invalid date in line '$_';
                next;
              }
              $_ = '20' . $_;
              ++$count;
              }
            continue {
              print OUT $_;
            }

            Or something like this. I don't have time to test this thoroughly, but I've
            done most of the work here...

            >  $count++;
            > }
            > }
            > close IN;
            > close OUT;
            > __END__
            >
            >
            >
            > ------------------------------------------------------------------------
            > Registering a domain name is quick and easy.
            > http://click.egroups.com/1/1611/1/_/12898/_/951127394/
            >
            > eGroups.com Home: http://www.egroups.com/group/perl-beginner/
            > http://www.egroups.com - Simplifying group communications
            >
            >
            ----
            If you saw it from a satellite/With its green and its blue and white
            The beauty of the curve of the earth/And its oceans below
            You might think it was paradise/If you didn't know
            You might think that it's turning/But it's turning so slow.
            --Jackson Browne, "How Long"
            ___________
            Jeff Boes  <><  jboes@...
            Mur Consulting  http://www.qtm.net/~jboes/

            ------------------------------------------------------------------------
            Registering a domain name is quick and easy.
            http://click.egroups.com/1/1611/1/_/12898/_/951140288/

            -- Check out your group's private Chat room
            -- http://www.egroups.com/ChatPage?listName=perl-beginner&m=1


            Click Here
            eGroups.com Home: http://www.egroups.com/group/perl-beginner
            www.egroups.com - Simplifying group communications
          • Sheldon E. Smith
            ... So what years are you getting, and where (when?) should the threshold be? If 70 and later versus 69 and before, then: $validator =~ /^( d{2}) d{4}$/; #
            Message 5 of 6 , Mar 22, 2000
            • 0 Attachment
              >Thank you Jeff....... this is very informative and helpful. But only
              >one thing...... the data comes a yymmdd........ as in 991212 through to
              >000221 My attempt would be to have these dates returned as 19991212
              >throught to 20000221 So we need to look at the logic as to when to
              >prepend 19 or when to prepend 20

              So what years are you getting, and where (when?) should the threshold be?
              If '70 and later versus '69 and before, then:

              $validator =~ /^(\d{2})\d{4}$/; # make sure it's six digits.
              $validator = (($1 >= 70) ? '19' : '20') . $validator;


              ------------------------------------------------------------------------------
              Sheldon E. Smith
              Enterprise Management Services
              OpenVMS & Digital Unix Management & Performance

              Compaq Services (612) 837-4826 Telephone
              (formerly Digital Equipment Corp.) (520) 396-8349 Fax
              7831 Glenroy Rd, # 250 Bloomington, MN 55439-3132
              ------ PGP keyid 1024/48710315 1996/05/07 <Sheldon.Smith@...> --------
              PGP Key fingerprint = B9 A6 6F 00 24 C0 5E B8 DB 8A 9A DE D3 DA 2B B4
            • obrienta@iserv.net
              Kinda late but.... use the Date::Manip module available from CPAN. Tommy O ... is ... strings ... $! );
              Message 6 of 6 , Feb 6, 2001
              • 0 Attachment
                Kinda late but....
                use the Date::Manip module available from CPAN.

                Tommy O

                --- In perl-beginner@y..., joachim@u... wrote:
                > I am working with data that come as yymmdd...... but I would like to
                > have a script that will rewrite the data as yyyymmdd This script
                is
                > sort of no quite doing that...... it just prepends 20 to all the
                strings
                > in the data. Any ideas....???
                >
                > John
                >
                > #!/usr/bin/perl
                > $infile = shift;
                > $outfile = shift;
                > $prepend = shift;
                > $validator=$_;
                >
                > # usage: perl prepend.pl infile outfile prepend_string
                > # example: perl prepend.pl msft.dat msft.csv 20
                > open(IN, $infile) || die("Can't open input file $infile - $!");
                > open(OUT, ">$outfile") || die("Can't open output file $outfile -
                $!");
                > while (<IN>) {
                >
                > $count = 0;
                > while (<IN>) {
                > if ($count > 0) {
                >
                > if ( $validator > 500000)
                > {$pre= '19';}
                >
                > elsif ( $validator >= 0 and $validator < 900000 )
                > {$pre= '20';}
                >
                > else {die ("Program error. \$validator is a negative number.");}
                >
                > $_ = $pre.$_;
                > print OUT $_;
                > }
                > $count++;
                > }
                > }
                > close IN;
                > close OUT;
                > __END__
              Your message has been successfully submitted and would be delivered to recipients shortly.