use strict;
use Data::Dumper;

my $dir = 'c:/perl/beispiele/strassen';


use Storable;

my  $store_file = 'plz_store';
my $plz = {};

if (-f $store_file) {
  $plz = retrieve($store_file)
} else {
  my $gkz_file = "$dir/gkz_plz.txt";
  open (FH, "<$gkz_file");
  my @gkz = <FH>;
  close FH;
  foreach my $line (@gkz) {
  	$line =~ /(\d{5}) (\d{4})/;
  	#print "$1 $2";
  	$plz->{$2} = get_strassen($dir, $1);
  }
  store $plz, $store_file;
}

my $postleitzahl;
my $strasse;
while ($postleitzahl ne 'end') {
	print "PLZ : ";
	$postleitzahl = <STDIN>;
	print "Strasse : ";
  $strasse      = <STDIN>;
  chomp($strasse);
  chomp($postleitzahl);
  print $plz->{$postleitzahl}->{$strasse} ."\n";
}

## print Dumper $plz;

sub get_strassen {
	my ($dir, $gkz) = @_;
	open (FH, "<$dir/$gkz.txt");
	my @strassen = <FH>;
	close FH;

	my $strassennamen = {};
	foreach my $strasse (@strassen) {
		## print "$strasse \n";
### 10101Eisenstadt                            096416Ahorngasse
		$strasse =~ /^\d{5}.*?(\d{6})(.*?)\s{2,}/;
		# print "$2\n";
		$strassennamen->{$2} = $1;
	  }
	  return $strassennamen;
}