Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
... add ("\0" x $n) to the original string.

Shouldn't that be ("\0" x ($n-1)) if $n is the boundary width in bytes?

Win8 Strawberry 5.8.9.5 (32) Sat 08/20/2022 15:46:20 C:\@Work\Perl\monks >perl use strict; use warnings; use bytes; use Data::Dump qw(pp); use constant BYTE_BOUND => 4; use constant PAD => "\0" x (BYTE_BOUND-1); my $s = 'TheRainInSpainFalls'; while (length $s) { my $p = $s . PAD; $p = bytes::substr $p, 0, bytes::length($p) - bytes::length($p)%BY +TE_BOUND; printf "%4s - %-28s \n", bytes::length($p), pp($p); chop $s; } ^Z 20 - "TheRainInSpainFalls\0" 20 - "TheRainInSpainFall\0\0" 20 - "TheRainInSpainFal\0\0\0" 16 - "TheRainInSpainFa" 16 - "TheRainInSpainF\0" 16 - "TheRainInSpain\0\0" 16 - "TheRainInSpai\0\0\0" 12 - "TheRainInSpa" 12 - "TheRainInSp\0" 12 - "TheRainInS\0\0" 12 - "TheRainIn\0\0\0" 8 - "TheRainI" 8 - "TheRain\0" 8 - "TheRai\0\0" 8 - "TheRa\0\0\0" 4 - "TheR" 4 - "The\0" 4 - "Th\0\0" 4 - "T\0\0\0"


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Align string on a 32-bit boundary with padding by AnomalousMonk
in thread Align string on a 32-bit boundary with padding by Lucas Rey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found