use warnings; use strict; my %std_data; my $std_names = 'names.txt'; my $std_grades = 'grades.txt'; ## call subroutrine read_file, passing different file name @std_data{ @{ read_file($std_names) } } = @{ read_file($std_grades) }; print $_,q{,},$std_data{$_},$/ for sort { $a cmp $b } keys %std_data; sub read_file { my ($file) = @_; my $data = []; open my $fh, '<', $file or die "can't open file:$!"; while ( defined( my $line = <$fh> ) ) { chomp $line; push @{$data}, $line; } close $fh or die "can't close file:$!"; return $data; }