### Calculating/adding byte counts of array elements

 on Aug 06, 2013 at 21:30 UTC Need Help??
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 (Bishop) on Aug 06, 2013 at 21:43 UTC
You are changing the array and using the changed value to count the number. Do not store the output in the array you are using to count the length:
```#!/usr/bin/perl
use warnings;
use strict;

my @array = ('Hello, my name is John',
'How are you?',
'blah (blah)',
);

my \$count = 0;
for my \$string (@array) {
printf "%08d %s\n", \$count, \$string;
\$count += length \$string;
}
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Calculating/adding byte counts of array elements
by johngg (Abbot) on Aug 06, 2013 at 22:20 UTC

Make sure you get the length of the array element before you modify it and accumulate the byte count afterwards.

```\$ perl -Mstrict -Mwarnings -E '
my @array = (
q{Hello, my name is John},
q{How are you?},
q{blah (blah)},
);
my \$byteCt = 0;
do {
my \$len = length;
s{^}{ sprintf q{%08d }, \$byteCt }e;
\$byteCt += \$len;
} for @array;
say for @array;'
00000000 Hello, my name is John
00000022 How are you?
00000034 blah (blah)
\$

Cheers,

JohnGG

Re: Calculating/adding byte counts of array elements
by ww (Archbishop) on Aug 06, 2013 at 22:29 UTC
TIMTOWTDI (and not quite redundant):

This alternate also works:

```#!/usr/bin/perl
use 5.016;
use warnings;

# 1048190

my @arr = ("Hello, my name is John",
"How are you?",
"blah (blah)",
"",
"blivitz is in the last line.",
);

my \$count = 0;

for my \$line(@arr) {
say \$count . " " . \$line;
\$count += length(\$line);
}

Producing:

```C:\>1048190.pl
0 Hello, my name is John
22 How are you?
34 blah (blah)
45
45 blivitz is in the last line.

Use of printf or sprintf is left as an exercise for the OP... altho the initialization of \$counting = '00000000'; should be satisfactory too.

If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
... initialization of  \$counting = '00000000'; should be satisfactory ...

Initializing  \$count as a numeric string with a bunch of leading zeros will see the leading zeros vanish after the first numeric operation 'numifies' the scalar — even if it's an operation with zero!.

```>perl -wMstrict -le
"my \$count = '00000000';
print qq{'\$count'};
;;
\$count += 42;
print qq{'\$count'};
"
'00000000'
'42'

Initializing \$count as a numeric string with a bunch of leading zeros will see the leading zeros vanish after the first numeric operation 'numifies' the scalar — even if it's an operation with zero!

So do not numify it but use Perl's little quirks to do the trick:

```perl -wMstrict -le 'my \$count = "00000000";
print qq{"\$count"};

++\$count for 1 .. 42;

print qq{"\$count"};'

Cheers, Sören

Créateur des bugs mobiles - let loose once, run everywhere.
(hooked on the Perl Programming language)

Re: Calculating/adding byte counts of array elements
by kcott (Chancellor) on Aug 07, 2013 at 07:55 UTC

G'day Dr Manhattan,

I see ++choroba has provided the solution to your posted problem.

```\$ perl -Mstrict -Mwarnings -e '
use Scalar::Util qw{dualvar};

my @string_array = (
"Hello, my name is John",
"How are you?",
"blah (blah)",
);

my @count_array;
my \$last_count = 0;

for (@string_array) {
push @count_array => dualvar \$last_count, \$_;
\$last_count += length;
}

printf "%08d %s\n" => \$_, \$_ for @count_array;
'
00000000 Hello, my name is John
00000022 How are you?
00000034 blah (blah)

-- Ken

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1048190]
Approved by ww
help
Chatterbox?
 [choroba]: The issue created

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-04-27 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (97 votes). Check out past polls.

Notices?