Thread Über viele(!) Boolesche Werte iterieren: Array oder Bit::Vector oder was? (14 answers)
Opened by flowdy at 2012-08-03 22:56

topeg
 2012-08-04 04:03
#160614 #160614
User since
2006-07-10
2611 articles
BenutzerIn

user image
Ich würde es ja so machen:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/perl
use strict;
use warnings;

my $bits=1_000_000;
my $val=42;

my $bin_data='';
for(my $i=0; $i<$bits; $i+=8)
{ $bin_data.=pack('C',int(rand(256))); }


my @returned;
my $str_data=unpack('B*',$bin_data);
my ($x,$bit,$last_bit,$val_neg)=(0,0,0,-$val);
for(0..length($str_data)-1)
{
  $bit=substr($str_data,$_,1);

  $x = $bit ? $val : $val_neg;
  if($bit xor $last_bit)
  { push @returned, $x }
  elsif(@returned==0)
  { push @returned, $x }
  else
  { $returned[-1] += $x }

  $last_bit=$bit;
}

print @returned+0,"\n";

View full thread Über viele(!) Boolesche Werte iterieren: Array oder Bit::Vector oder was?