Hi all

I have an array with strings. I want to calculate the byte count of each element and add it to the start of the next array element. The next element should start with the sum of all the byte counts

If this was my array

Hello, my name is John How are you? blah (blah)

String1: 22 bytes. String2: 12 bytes. String3: 11 bytes. Also, I want the byte counts to be 8-digit intergers. Here is how I want my output to look

00000000 Hello, my name is John 00000022 How are you? 00000034 blah (blah)

And here is the code that I am trying:

my $counting = '00000000'; $array[0] = "$counting$array[0]\n"; my $length = scalar(@array); for(my $x=1; $x <= $length; $x++) { my $size = length($array[$x-1]); $counting = $counting + $size; $array[$x] = "$counting$array[$x]\n"; }

But somehow the byte count is not what I expect it to be (always 1 more), and after the first line of output, each line starts with a space?

Any ideas? Thanks in advance for any help


In reply to Calculating/adding byte counts of array elements by Dr Manhattan

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.