Thread Empfehlung Datenbank als Datei (37 answers)
Opened by bianca at 2019-10-20 19:55

rosti
 2019-11-10 11:13
#190859 #190859
User since
2011-03-19
3194 Artikel
BenutzerIn
[Homepage]
user image
Array serialisieren

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
32
33
34
35
36
37
38
39
40
41
42
use strict;
use warnings;

sub serialize_array{
    my $self = shift;
    my $aref = shift || [];
    my $io   = shift || *STDOUT;
    use bytes;
    foreach my $e(@$aref){
        if( defined $e ){
            $io->print( pack("N", length $e)."D".$e );
        }
        else{
            $io->print( pack("N", 0)."U" );
        }
    }
}

sub restore_array{
    my $self = shift;
    my $io   = shift || *STDIN;
    my $r = [];
    while( read($io, my $buf, 4) ){
        my $len = unpack "N", $buf;
        read($io, my $def, 1);  # defined//undef
        read($io, my $e, $len);
        push @$r, $def eq "D" ? $e : undef;        
    }
    return $r;
} 
1;#########################################################################

# Tests untenstehend
use IO::String;
use Data::Dumper;
my $io = IO::String->new;
my $m = bless{};
$m->serialize_array( [0,'foo','bar','bäh',undef,''], $io );
$io->seek(0,0);

my $r = $m->restore_array($io);
print Dumper $r;;

View full thread Empfehlung Datenbank als Datei