### association_approaches.pl
### hydrotron (Jon G.) 2015-04-14
### Script that looks at diffrent data types to register associations.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper; 


my $key1 = 'city1';
my $key2 = 'city2';
my $key3 = 'city3';

my $val1 = 'zip1';
my $val2 = 'zip2';
my $val3 = 'zip3';
my $val4 = 'zip1';

#approach 1 hash of string-tokens
my @assoc = ($val1,$val3,$val4);
my $hashref ;
foreach my $value (@assoc) {
  if ($hashref->{$key1}) {
	my $current_value = $hashref->{$key1};
	$hashref->{$key1} = $current_value . "," . $value;
  }else{
	$hashref->{$key1} =  $value;
  }
}
# print Dumper $hashref;

#approach 2 key hash - assoc-cnt hash
foreach my $value (@assoc) {
  if ($hashref->{$key2}->{$value}) {
	$hashref->{$key2}->{$value} ++;
  }else{
	$hashref->{$key2}->{$value} = 1;
  }
}
# print Dumper $hashref;	

#approach 3 hash - array associations

foreach my $value (@assoc) {
  if ($hashref->{$key3}) {
	push( @{$hashref->{$key3}} , $value);
  }else{
	$hashref->{$key3} =  [ $value ];
  }
}
print Dumper $hashref;

