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

RE: [PBML] Keeping leading zeros

Expand Messages
  • Eric Thibodeau
    Here seems to be a cleaner way to do this: printf( %010d ,$num); ... Eric Thibodeau ... From: Mike Payne [mailto:theseus@telocity.com] Sent: Friday, March 30,
    Message 1 of 11 , Mar 30, 2001
    • 0 Attachment
      Here seems to be a cleaner way to do this:

      printf("%010d",$num);

      :oP

      Eric Thibodeau

      -----Original Message-----
      From: Mike Payne [mailto:theseus@...]
      Sent: Friday, March 30, 2001 11:36 AM
      To: perl-beginner@yahoogroups.com
      Subject: RE: [PBML] Keeping leading zeros


      You could do it with a series of if statements, although I'm sure someone
      will come up with a more efficient way...

      if ($num < 1000000000) { $num = "0" . $num; }
      if ($num < 100000000) { $num = "0" . $num; }
      if ($num < 10000000) { $num = "0" . $num; }

      etc...

      Not tested, but I've seen code like this work in counter scripts, you could
      download Matt's counter to see it.

      -Mike


      -----Original Message-----
      From: Denny Malloy [mailto:denny.malloy@...]
      Sent: Friday, March 30, 2001 11:25 AM
      To: perl-beginner@yahoogroups.com
      Subject: [PBML] Keeping leading zeros


      Hi,

      I have a slight problem. I need to append a continuous sequence number to
      each line of a file.

      The sequence number must be 10 byes long and start out at 1.
      So my first sequence is "0000000001". My problem is when I add 1 to it I
      get "2" not "0000000002". I need to know how to keep the leading zeros. I
      got around this for now by adding 1 to "10000000001" them cutting off the
      first "1" with substr before appending to the line. Is there an easier way?

      Thanks:)

      Denny Malloy





      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/







      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    • Kelly White
      ... my bad here, pack won t work, but sprintf will. ... __________________________________________________ Do You Yahoo!? Get email at your own domain with
      Message 2 of 11 , Mar 30, 2001
      • 0 Attachment
        > $num = 1;
        > $num = sprintf '%010d', ++$num;
        >
        > The value in $num at the end of the two lines is '0000000002'. You could do
        > the same thing with pack:
        >

        my bad here, pack won't work, but sprintf will.

        > $num = pack'A10', ++$num;


        >
        > Hope that helps,
        > mckhendry
        >
        >
        > --- Denny Malloy <denny.malloy@...> wrote:
        > > Hi,
        > >
        > > I have a slight problem. I need to append a continuous sequence number to
        > > each line of a file.
        > >
        > > The sequence number must be 10 byes long and start out at 1.
        > > So my first sequence is "0000000001". My problem is when I add 1 to it I
        > > get "2" not "0000000002". I need to know how to keep the leading zeros.
        > I
        > > got around this for now by adding 1 to "10000000001" them cutting off the
        > > first "1" with substr before appending to the line. Is there an easier
        > way?
        > >
        > > Thanks:)
        > >
        > > Denny Malloy
        > >
        > >
        >
        >
        > __________________________________________________
        > Do You Yahoo!?
        > Get email at your own domain with Yahoo! Mail.
        > http://personal.mail.yahoo.com/?.refer=text
        >


        __________________________________________________
        Do You Yahoo!?
        Get email at your own domain with Yahoo! Mail.
        http://personal.mail.yahoo.com/?.refer=text
      • Mike Payne
        I checked out the perldoc for printf, but it doesn t show what the actual formats have to be? Like you used %010d, how did you come up with that formula? I
        Message 3 of 11 , Mar 30, 2001
        • 0 Attachment
          I checked out the perldoc for printf, but it doesn't show what the actual
          formats have to be? Like you used %010d, how did you come up with that
          formula? I didn't see it anywhere in the documentation.

          -Mike


          -----Original Message-----
          From: jandrew [mailto:jandrew]On Behalf Of Andrew Johnson
          Sent: Friday, March 30, 2001 11:57 AM
          To: perl-beginner@yahoogroups.com
          Subject: Re: [PBML] Keeping leading zeros



          The sprintf() or printf() are designed for this kind of thing:

          for my $i (1 .. 10) {
          printf("%010d\n", $i);
          }

          Use sprintf() in the same way but when you want to store it
          in a variable for later use:

          my $i = sprintf("%010d", 2137);
          print $i;

          See:
          perldoc -f sprintf
          perldoc -f printf

          for further info.

          regards,
          andrew


          --
          Andrew L. Johnson http://members.home.net/andrew-johnson/
          The generation of random numbers is too
          important to be left to chance.




          Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        • Andrew Johnson
          ... Check out perldoc -f sprintf it has all the format codes (which are the same between printf and sprintf). andrew -- Andrew L. Johnson
          Message 4 of 11 , Mar 30, 2001
          • 0 Attachment
            > I checked out the perldoc for printf, but it doesn't show what the
            > actual formats have to be? Like you used %010d, how did you come up
            > with that formula? I didn't see it anywhere in the documentation.

            Check out

            perldoc -f sprintf

            it has all the format codes (which are the same between printf and
            sprintf).

            andrew

            --
            Andrew L. Johnson http://members.home.net/andrew-johnson/
            Some people, when confronted with a problem, think 'I know,
            I'll use regular expressions.' Now they have two problems.
            -- Jamie Zawinski, on comp.lang.emacs
          Your message has been successfully submitted and would be delivered to recipients shortly.