#!/usr/bin/perl -w # ... # I assume @files holds the list of filenames my %data; foreach (@files) { # this will allow us to store the whole file as a string! { local $/ = undef; # open FILE, $_ or die "Couldn't open $_: $!\n"; $data{$_} = ; close FILE; } } # to print : foreach (sort keys %data) { my ($city, $id) = split /_/, $_; print "City: $city, ID : $id\n"; print "====\n\n"; print $data{$_}, "=====\n"; }