#!/usr/bin/perl use warnings; use strict; my $mode = "start"; my %hash; while() { chomp; if ( $_ =~ /file1/) { $mode = "read"; next; } if ( $_ =~ /file2/) { $mode = "add"; next; } if ( $mode eq "read" ) { $hash{$_} = 0; } elsif ( $mode eq "add" ) { my ( $key, $value ) = split " ", $_, 2; $hash{$key} = $value if ( exists($hash{$key}) ); } } for ( sort keys %hash ) { print "$_ => $hash{$_} \n" if $hash{$_}; } __DATA__ file1 Angel Baker Candy Edward Fox file2 Angel 7 Fox 10 Gigantic 29 Candy 3 #### C:\Code>perl order_by_key.pl Angel => 7 Candy => 3 Fox => 10