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

Re:[PBML] adding trailing zeros

Expand Messages
  • Huangj
    my $value = 3; sub suffix_0 { my $c = $_[0] - length($_[1]); if ($c 0) { my $result = $value . 0 x$c; return $result; } return $_[1]; } print suffix_0(5,
    Message 1 of 6 , Nov 8, 2012
    • 0 Attachment
      my $value = 3;


      sub suffix_0 {
      my $c = $_[0] - length($_[1]);
      if ($c>0) {
      my $result = $value . '0'x$c;
      return $result;
      }
      return $_[1];
      }


      print suffix_0(5, $value);








      At 2012-11-08 07:07:53,Noah <noah-list@...> wrote:


      Hi there,

      I am hoping to make the following changes to integers.
      I would like to do something like the following sprintf command Below
      but instead of leading zeros I would like to add trailing zeros.

      $value = sprintf ("%04d", $value);

      but instead I would like

      1 to become 1000
      23 to become 2300

      is there a way to do that with the sprintf command.




      [Non-text portions of this message have been removed]
    • merlyn@stonehenge.com
      ... Huangj my $value = 3; Huangj sub suffix_0 { Huangj my $c = $_[0] - length($_[1]); Huangj if ($c 0) { Huangj my $result = $value . 0 x$c; Huangj
      Message 2 of 6 , Nov 8, 2012
      • 0 Attachment
        >>>>> "Huangj" == Huangj <redspid@...> writes:

        Huangj> my $value = 3;
        Huangj> sub suffix_0 {
        Huangj> my $c = $_[0] - length($_[1]);
        Huangj> if ($c>0) {
        Huangj> my $result = $value . '0'x$c;
        Huangj> return $result;
        Huangj> }
        Huangj> return $_[1];
        Huangj> }


        Huangj> print suffix_0(5, $value);

        Well, that's one sideways way to do it. Especially for Perl. :)

        I'd just do this:

        $num = sprintf('%4d', $num); # space pad to 4 chars
        $num =~ tr/ /0/; # turn spaces into zeroes



        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.posterous.com/ for Smalltalk discussion
      • Shlomi Fish
        Hi Huangj, a few comments on your code. On Thu, 8 Nov 2012 20:40:25 +0800 (CST) ... $value is global to the subroutine. That s not good. ... You should not use
        Message 3 of 6 , Nov 8, 2012
        • 0 Attachment
          Hi Huangj,

          a few comments on your code.

          On Thu, 8 Nov 2012 20:40:25 +0800 (CST)
          Huangj <redspid@...> wrote:

          > my $value = 3;
          >
          >
          > sub suffix_0 {

          $value is global to the subroutine. That's not good.

          > my $c = $_[0] - length($_[1]);

          You should not use positional indexes into @_:

          http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

          > if ($c>0) {
          > my $result = $value . '0'x$c;
          > return $result;
          > }
          > return $_[1];
          > }

          Why are you assigning to $result and then doing nothing with it except
          returning it? Furthermore, if $c <=0 then '0'x$c will be the empty string which
          is also OK.

          And you should not reference $value inside the loop.

          I would write it as:

          sub suffix_0 {
          my ($len, $s) = @_;

          return $s . ('0' x ($len - length($s)));
          }

          If you want to be on the safe side you can use ? : to check for
          zero-or-negative values.

          Regards,

          Shlomi Fish

          --
          -----------------------------------------------------------------
          Shlomi Fish http://www.shlomifish.org/
          "The Human Hacking Field Guide" - http://shlom.in/hhfg

          If Botticelli were alive today, he’d be working for Vogue.
          http://en.wikiquote.org/wiki/Peter_Ustinov

          Please reply to list if it's a mailing list post - http://shlom.in/reply .
        • Huangj
          I am glad that my code received so many replies. I learn from your suggestions to learn many things. Thank you. so, i will coding more code, post to the
          Message 4 of 6 , Nov 8, 2012
          • 0 Attachment
            I am glad that my code received so many replies.
            I learn from your suggestions to learn many things.
            Thank you.
            so, i will coding more code, post to the mailist.
            At 2012-11-08 21:38:45,"Shlomi Fish" <shlomif@...> wrote:
            >Hi Huangj,
            >
            >a few comments on your code.
            >
            >On Thu, 8 Nov 2012 20:40:25 +0800 (CST)
            >Huangj <redspid@...> wrote:
            >
            >> my $value = 3;
            >>
            >>
            >> sub suffix_0 {
            >
            >$value is global to the subroutine. That's not good.
            >
            >> my $c = $_[0] - length($_[1]);
            >
            >You should not use positional indexes into @_:
            >
            >http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments
            >
            >> if ($c>0) {
            >> my $result = $value . '0'x$c;
            >> return $result;
            >> }
            >> return $_[1];
            >> }
            >
            >Why are you assigning to $result and then doing nothing with it except
            >returning it? Furthermore, if $c <=0 then '0'x$c will be the empty string which
            >is also OK.
            >
            >And you should not reference $value inside the loop.
            >
            >I would write it as:
            >
            >sub suffix_0 {
            > my ($len, $s) = @_;
            >
            > return $s . ('0' x ($len - length($s)));
            >}
            >
            >If you want to be on the safe side you can use ? : to check for
            >zero-or-negative values.
            >
            >Regards,
            >
            > Shlomi Fish
            >
            >--
            >-----------------------------------------------------------------
            >Shlomi Fish http://www.shlomifish.org/
            >"The Human Hacking Field Guide" - http://shlom.in/hhfg
            >
            >If Botticelli were alive today, he’d be working for Vogue.
            > — http://en.wikiquote.org/wiki/Peter_Ustinov
            >
            >Please reply to list if it's a mailing list post - http://shlom.in/reply .
          Your message has been successfully submitted and would be delivered to recipients shortly.