Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string 
Author Message
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

In version 5.004 of perl, am I doing something wrong:

My Code:

   $formatnewexternalid =  
      sprintf('%-20s',$newexternalid);
   print "FormatNewExternalID: $formatnewexternalid.\n" if $debug;

My Debug Output:

New External ID: cbsTestDupCIAM2                                 .
Updating Current External ID with New External ID.
FormatNewExternalID: cbsTestDupCIAM2                                 .

$newexternalid is a 48 character field.  The print of the
FormatNewExternalID displays a 48 character field -- not a 20
character field -- why?

If I do a sprintf that is longer than the field -- no problem -- just
seems to have a problem when the field is longer than what is defined
in the sprintf.



Fri, 13 Aug 2004 21:18:27 GMT  
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

Quote:

> $newexternalid is a 48 character field.  The print of the
> FormatNewExternalID displays a 48 character field -- not a 20
> character field -- why?

I does what you asked for, carefully re-examine output of 'perldoc -f sprintf'.

You probably want to use:
$str2 = sprintf "%-20.20s", $str1;
                   ^^^^^^

--
$^O=~s=window(s|\$)=($money)?'Solaris':'Linux/BSD'=eig and print "Use $^O !\n";



Fri, 13 Aug 2004 21:26:20 GMT  
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

Quote:

> In version 5.004 of perl, am I doing something wrong:

> My Code:

>    $formatnewexternalid =  
>       sprintf('%-20s',$newexternalid);
>    print "FormatNewExternalID: $formatnewexternalid.\n" if $debug;

[...]

Quote:
> $newexternalid is a 48 character field.  The print of the
> FormatNewExternalID displays a 48 character field -- not a 20
> character field -- why?

> If I do a sprintf that is longer than the field -- no problem -- just
> seems to have a problem when the field is longer than what is defined
> in the sprintf.

You might want to read up on how sprintf format specifiers work, in
particular

               .number "precision": digits after decimal point for
                       floating-point, max length for string, minimum length
                       for integer

is in the output from

  perldoc -tf sprintf

e.g. (in the de{*filter*})

  DB<1> $s = '123   '

  DB<2> printf 's is %-3s.', $s
s is 123   .
  DB<3> printf 's is %-.3s', $s
s is 123
  DB<4> printf 's is %-.3s.', $s
s is 123.
  DB<5>

Hope this helps,

Mike

--

http://www.*-*-*.com/ ~mike/       | GPG PGP Key      1024D/059913DA

http://www.*-*-*.com/ ;          |                  75D2 9EC4 C1C0 0599 13DA



Fri, 13 Aug 2004 21:29:47 GMT  
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

Quote:

>My Code:

>   $formatnewexternalid =  
>      sprintf('%-20s',$newexternalid);
>   print "FormatNewExternalID: $formatnewexternalid.\n" if $debug;

[...]

>If I do a sprintf that is longer than the field -- no problem -- just
>seems to have a problem when the field is longer than what is defined
>in the sprintf.

Read your system's printf manpage carefully - I believe what you want is
'%-20.20s'.

My printf manpage says:

3.  A precision that gives the minimum number of digits to appear for
    the d, i, o, u, x, or X conversions, the number of digits to appear
    after the radix character for the e and f conversions, the maximum
    number of significant digits for the g conversion, or the maximum
    number of bytes to be printed from a string in the s conversion.
    The precision takes the form of a period followed by a decimal
    digit string; a null digit string is treated as zero.

Rich

--

Technical Consultant     | I speak for me,     |   19055 Pruneridge Ave.
Development Alliances Lab|            *not* HP |                MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014



Sat, 14 Aug 2004 00:33:37 GMT  
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

Quote:

> In version 5.004 of perl, am I doing something wrong:

> My Code:

>    $formatnewexternalid =  
>       sprintf('%-20s',$newexternalid);
>    print "FormatNewExternalID: $formatnewexternalid.\n" if $debug;

> My Debug Output:

> New External ID: cbsTestDupCIAM2                                 .
> Updating Current External ID with New External ID.
> FormatNewExternalID: cbsTestDupCIAM2                                 .

> $newexternalid is a 48 character field.  The print of the
> FormatNewExternalID displays a 48 character field -- not a 20
> character field -- why?

> If I do a sprintf that is longer than the field -- no problem -- just
> seems to have a problem when the field is longer than what is defined
> in the sprintf.

Thanks for your suggestions, but %-20.20s did not work for me.  I
ended up doing substr($newexternal,0,20) to get the first 20
characters of the 48 length string and then (this is probably
overkill) the sprintf %-20s to insure the backfilling of spaces.

Maybe the %-20.20s did not work for me since the $newexternalid
contains an alphanumeric string (i.e. test123)?

Also, someone here at work says that sprintf is working the way it
should when it does not truncate the field to 20 characters.  They
said that that is how it performs in C.



Sun, 15 Aug 2004 21:03:00 GMT  
 Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string
[posted & mailed]

On Feb 27, Bulldozer said:

Quote:
>Thanks for your suggestions, but %-20.20s did not work for me.  I
>ended up doing substr($newexternal,0,20) to get the first 20
>characters of the 48 length string and then (this is probably
>overkill) the sprintf %-20s to insure the backfilling of spaces.

>Maybe the %-20.20s did not work for me since the $newexternalid
>contains an alphanumeric string (i.e. test123)?

No, that makes no difference.

  perl -e 'printf "<<%.5s>>\n", "jeffrey"'

yields "<<jeffr>>".

Quote:
>Also, someone here at work says that sprintf is working the way it
>should when it does not truncate the field to 20 characters.  They
>said that that is how it performs in C.

They're mistaken.  The C program

  int main () { printf("<<%.5s>>\n", "jeffrey"); }

yields "<<jeffr>>" as well.  The printf documentation does not lie.

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



Sun, 15 Aug 2004 21:44:16 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Workspace-1.48.tar.gz

2. make fails on libwww-perl-5.48

3. $length =@{$array_ref}; doesn't return length

4. CGI.pm: saving query values without the '%20's

5. Getting CHAR's from STRING's

6. IO-1.20 Won't install from RH7.1

7. sprintf '%.5s', 123456789

8. PL41 BUG: pack('c',0) ne sprintf("%c",0)

9. Trying to extra 1st 20 chars of line

10. Q: How to remove first N char's from string

11. What is pseudorandommy @chars=('a'..'z','A'..'Z','0'..'9','_');

12. split a string not only by a single char but also by a string

 

 
Powered by phpBB® Forum Software