rlm@0: #!/usr/bin/perl rlm@0: rlm@0: $a = "/home/r/coderloop-test/reversesequence-a.in"; rlm@0: $b = "/home/r/coderloop-test/reversesequence-b.in"; rlm@0: $c = "/home/r/coderloop-test/reversesequence-c.in"; rlm@0: rlm@0: %test_map = ("a" => "b", "b" => "c"); rlm@0: rlm@0: sub create_map{ rlm@0: ## creates a "linked map" from a string representing rlm@0: ## the input file. rlm@0: (my $file) = @_; rlm@0: my %map; rlm@0: open FILE, "<".$file or die $!; rlm@0: for (){ chomp; /^(\w+)\W+(\w+)$/; $map{$1} = $2;} rlm@0: close FILE; rlm@0: return %map;} rlm@0: rlm@0: sub find_beginning{ rlm@0: ## takes a map representing a linked list and rlm@0: ## returns the beginning of that list. rlm@0: (my %map) = @_; rlm@0: my %val_set = map {$_ => 1} (values %map); rlm@0: my $start; rlm@0: foreach (keys %map){ rlm@0: if (!($val_set{$_})){$start = $_;last}} rlm@0: return $start;} rlm@0: rlm@0: sub trace_map{ rlm@0: ## crawls through a map representing a linked-list rlm@0: ## and converts it into an actual array structure. rlm@0: (my %map) = @_; rlm@0: my $position = &find_beginning(%map); rlm@0: my @array = ($position); rlm@0: while(my $new_position = $map{$position}){ rlm@0: push (@array, $new_position); rlm@0: $position = $new_position;} rlm@0: return @array;} rlm@0: rlm@0: rlm@0: rlm@0: if (defined @ARGV){ rlm@0: map {print;print"\n";} reverse(trace_map(create_map($ARGV[0])));}