... 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: <%-{-{-{-<
-
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.
|