Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2
multihash-flatfile.pl
#!/usr/local/bin/perl -w
# multihash-flatfile.pl -- Multihashes in einer flachen Textdatei
# speichern
open(INFILE, "< " . shift()) or die "can't open inputfile: $!\n";
my $listp = read_em(\*INFILE);
close(INFILE);
use Data::Dumper;
print Dumper($listp);
open(OUTFILE, "> " . shift()) or die "can't open outputfile: $!\n";
write_em(\*OUTFILE, $listp);
close(OUTFILE);
sub read_em {
my $fh = shift; # Offenes Filehandle
local $/ = ''; # Absatz-Modus. Wichtig: local
my @records;
while (<$fh>) {
my $hashp = {}; # my wichtig!
foreach my $entry (split /\n/) {
my ($key, $value) = ($entry =~ /^(.*?):\s*(.*)$/);
push(@{ $hashp->{$key} }, $value);
}
push(@records, $hashp);
}
return [ @records ];
}
sub write_em {
my $fh = shift;
my $reclistp = shift;
my ($key, $val);
foreach my $recordp ( @{ $reclistp } ) {
while (($key,$val) = each(%{ $recordp })) {
print $fh join("\n", map { "$key: $_" } @{ $val }), "\n";
}
print $fh "\n";
}
}
[Prev] [Up] [Relevant Chapter] [Next]
[Alte Quelle]
| Last modified: $Date: 2006/05/18 12:56:01 $ FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy |
|