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

Re: padding string with white space and leading 0's

Expand Messages
  • a_z0_9_blah
    ... 0 s ... 0 s to ... my ... $pad_len, ... If my previous reply sounded curt, I apologize :-) Sometimes, what you mean to express in a helpful way doesn t
    Message 1 of 4 , Aug 27, 2008
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "Jeff Shu" <santa98bn@...>
      wrote:
      >
      > hello everyone:
      >
      > I am trying to use sprintf function to add white space or leading
      0's
      > to a string(padding spaces to the right of the string, padding
      0's to
      > the left of the string)
      >
      > if the string is less than certain lengh, padding will take place.
      > After the padding spaces or zeros
      > the length should change as well.
      >
      > My code doesn't append the space(s) to the string. Please review
      my
      > code and give me some advises on what went wrong.
      > I appreciate your help. Thanks!
      >
      > #!/usr/bin/perl
      >
      > $name ="jason";
      >
      > #padding space to the right of the string
      > $pad_len = 3;
      > $name_right = sprintf("%-${3}s", $name); # sprintf("%-*s",
      $pad_len,
      > $name)
      >
      > print "\$name is: $name_right \n\n";
      > print "length of \$name is " . length($name_right) ."\n";
      >

      If my previous reply sounded curt, I apologize :-)
      Sometimes, what you mean to express in a helpful way doesn't always
      come across that way.

      Your commented sprintf:

      # sprintf("%-*s", $pad_len, $name)

      should work fine.

      Chris
    Your message has been successfully submitted and would be delivered to recipients shortly.