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

Re:Re: [PBML] adding trailing zeros

Expand Messages
  • 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 1 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.