Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Calculating/adding byte counts of array elements
by choroba (Cardinal) on Aug 06, 2013 at 21:43 UTC | |
|
Re: Calculating/adding byte counts of array elements
by johngg (Canon) on Aug 06, 2013 at 22:20 UTC | |
|
Re: Calculating/adding byte counts of array elements
by ww (Archbishop) on Aug 06, 2013 at 22:29 UTC | |
by AnomalousMonk (Archbishop) on Aug 07, 2013 at 07:04 UTC | |
by Happy-the-monk (Canon) on Aug 07, 2013 at 07:41 UTC | |
|
Re: Calculating/adding byte counts of array elements
by kcott (Archbishop) on Aug 07, 2013 at 07:55 UTC |