#!/usr/bin/perl -d # use strict; use warnings 'all'; # initialize filenames my $erste_datei = 'addr_allowed.lis'; my $andere_datei = 'postfix.lis'; # read files my $data_to_append = &datei_lesen($erste_datei); my $full_data = &datei_lesen($andere_datei); # append data foreach my$key (keys %{$data_to_append}) { # check whether we can append the data next if !exists($full_data->{$key}); # append data push(@{$full_data->{$key}->[0]}, @{shift(@{$data_to_append->{$key}})}); push(@{$full_data->{$key}}, @{$data_to_append->{$key}}); } # output &datei_schreiben($full_data, $andere_datei); # NAME: datei_schreiben() # USE: Datei anhand des musters in eine Datei schreiben # PARAMETERS: 1. Data # 2. Filename # RETURNS: void sub datei_schreiben($$) { # pick parameters my($data, $file) = @_; # open file open(my$fhOUT, '>', $file) or die "open failed: $!"; # iterate through the data foreach my$key (keys %{$data}) { # create line my $line = $key . '#' . join(',', map { join('#', @{$_}) } @{$data->{$key}}); # write out line print $fhOUT "$line\n"; } # close file close($fhOUT) or die "close failed: $!"; } # datei_schreiben # NAME: datei_lesen() # USE: Datei anhand des musters in eine komplexe Datenstruktur einlesen. # PARAMETERS: Filename # RETURNS: Hash reference sub datei_lesen($) { # pick parameters my($file) = @_; # open file open(my$fhIN, '<', $file) or die "open failed: $!"; # read data my %data; while (my$line=<$fhIN>) { chomp($line); # split line my @tracts = split('#', $line); # key equals the first tract my $key = shift @tracts; my $val = [@tracts]; # push data $data{$key} = [] unless exists $data{$key}; push(@{$data{$key}}, $val); } # close file close($fhIN); # return data reference return \%data; } # datei_lesen