Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | Nächste Überarbeitung Beide Seiten der Revision | ||
perl_array_of_hashes [2018/12/20 10:16] admin [Array of Hashes] |
perl_array_of_hashes [2018/12/20 10:23] admin [Erstellung eines "Array of Hashes"] |
||
---|---|---|---|
Zeile 27: | Zeile 27: | ||
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" }; | push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" }; | ||
</code> | </code> | ||
+ | |||
+ | ===== Generation eines Array of Hashes ===== | ||
+ | |||
+ | Einlesen einer Datei im Format "husband=fred friend=barney" mit einer der folgenden Schleifen: | ||
+ | |||
+ | <code bash> | ||
+ | while ( <> ) { | ||
+ | $rec = {}; | ||
+ | for $field ( split ) { | ||
+ | ($key, $value) = split /=/, $field; | ||
+ | $rec->{$key} = $value; | ||
+ | } | ||
+ | push @AoH, $rec; | ||
+ | } | ||
+ | |||
+ | while ( <> ) { | ||
+ | push @AoH, { split /[\s=]+/ }; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Innerhalb einer Subroutine 'get_next_pair', das eine key/value Paar liefert. | ||
+ | <code bash> | ||
+ | while ( @fields = get_next_pair() ) { | ||
+ | push @AoH, { @fields }; | ||
+ | } | ||
+ | |||
+ | while (<>) { | ||
+ | push @AoH, { get_next_pair($_) }; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Anhängen neuer Einträge an ein existierendes hash: | ||
+ | <code bash> | ||
+ | $AoH[0]{pet} = "dino"; | ||
+ | $AoH[2]{pet} = "santa's little helper"; | ||
+ | </code> | ||
+ | |||
+ | |||
===== Quellen ===== | ===== Quellen ===== | ||
* O'REILLY - Programming Perl (https://docstore.mik.ua/orelly/perl4/prog/ch09_03.htm) | * O'REILLY - Programming Perl (https://docstore.mik.ua/orelly/perl4/prog/ch09_03.htm) |