#!/usr/bin/perl -w use strict; use warnings; my %Hash_filenames=(); my %Hash_filematches=(); my $plf; my $start_dir; my $start_dir = \\server1\tools ; my $file_name = "load.c"; opendir(DIR, "$start_dir"); my @plf_files = grep(/\.plf$/,readdir(DIR)); print "PLF FILES\n"; print "@plf_files\n"; foreach my $plf (@plf_files) { chomp($plf); open my $match, '<',"$start_dir\\$plf" or die "could not open '$plf' $!"; my @file_lines = <$match>; #Save the filename as key and the lines in the file as value in a hash $Hash_filenames{$plf}=@file_lines; } closedir(DIR); if ($file_name) { foreach my$filename(keys %Hash_filenames) { #for every key(filename) in the above hash namely #$Hash_filenames #grep for $file_name in the #corresponding values (lines in the file)of the file #and save the match as value and key as filename #in another hash $Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filenames{$plf} ); } } { local $, = "\n"; print "PRINTING MATCHED HASHES"; foreach my $key ( keys %Hash_filenames ) { my $value = $Hash_filematches{$key}; print "$key => $value\n"; } }