Thread Array-Block speichern (3 answers)
Opened by RhoenSprudel at 2014-11-28 22:51

Raubtier
 2014-11-28 23:29
#178592 #178592
User since
2012-05-04
1077 Artikel
BenutzerIn
[default_avatar]
2014-11-28T21:51:23 RhoenSprudel
Jetzt meine Frage: Kann ich nicht einfach den kompletten Block ab "interface" bis zum nächsten in ein Array speichern?

Ja.
Quote
Der Rest der "interfaces" wäre dann in den anderen Teilen des Arrays. Mir würde es helfen, wenn ihr mir erklären könnt, wie ich diesen "Block" in ein Array speichern kann.


Naja, du liest die Datei einfach zeilenweise ein - und wenn ein neuer Blockanfang kommt, dann speicherst du das bisher gelesene in dem Array.

Mir ist aber nicht klar, warum du das brauchst. Ich hab mal ein Beispiel gemacht:

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
use 5.14.0;

sub endOfBlock {
    my ($iface, $ref_alsHoA) = @_;
    say "interface: $iface";
    for my $k (sort keys %$ref_alsHoA) {
        say "$k: ", join ', ', @{$ref_alsHoA->{$k}};
    }
    %$ref_alsHoA = ();
}

my %alsHoA;
my $iface;
while (<DATA>) {
    if (/^###### interface (.+) ######$/) {
        endOfBlock($iface, \%alsHoA) if defined $iface;
        $iface = $1;
    }
    if (/^(Information \d+)(.+)/) {
        push @{$alsHoA{$1}}, $2;
    }
}
endOfBlock($iface, \%alsHoA) if defined $iface;

__DATA__
###### interface INFORMATION1 ######
Information 1a
Information 1b
Information 1c
Information 2a
Information 3a
Information 3b
###### interface INFORMATION2 ######
Information 1a
Information 1b
Information 1c
Information 2a
Information 3a

View full thread Array-Block speichern