#!/usr/bin/env perl use strict; use warnings; use Data::Dump; opendir(my $dh, '.') or die $!; my @files = map { /(.*)\.ctl$/ }readdir $dh; my %hashSlice; @hashSlice{@files} = undef; my $srss; $srss->{files} = { %hashSlice } ; for (@files) { # $_ is set to each element in @files for my $extension(qw(ctl log)) { my $file = "$_.$extension"; open (my $fh, $file) or die $!; while (my $line = <$fh>) { while($line =~ /SRS[_\s]?(\d+)/g) { $srss->{files}{$_}{$extension}{$1} = 'Found'; } } } } dd $srss; { files => { "1a" => { log => { 111 => 1, 2222 => 1, 3333 => 1 } }, "1b" => undef, "1c" => { ctl => { 222 => 1 }, log => { 333 => 1 } }, "2a" => undef, "2b" => undef, "2c" => undef, "3a" => undef, "3b" => undef, "3c" => undef, }, }