Perl
use Data::Dumper;
my %hash;
while(<DATA>){
next if /^$/;
my @array = split("<--",$_);
my $array_length = $#array + 1;
while($#array != 0){
my $key = pop @array;
if(! exists($hash{$key})){
$hash{$key} = [ $array[-1] ];
} else {
next if ( grep { $_ eq $array[-1]} @{ $hash{$key} } );#if the item already exists in the array, skip it
push @{ $hash{$key} }, $array[-1]; # put the item into the array if it does not exist in the array
}
}
}
print OFH Dumper(\%hash);
__DATA__
tt1 <-- tt2 <-- tt3 <-- tt4
tt1 <-- tt2 <-- tt3 <-- tt4
|