#!/usr/bin/perl use strict; use warnings; use Text::Table; use Data::Dumper; use List::Util qw{max}; use Fcntl qw(:flock); # import LOCK_* and SEEK_END constants $| = 1; my $value = (); #create anonymous string_ref my %hash = (); sub hash_sub { foreach my $arg (@_) { open (READ, "<" , $arg) or die ("Could not open: ".$arg." - $!\n"); flock(READ, LOCK_EX) or die "Could not lock '".$arg."' - $!\n"; if (-z "".$arg."") { print "File '".$arg."' is empty!\n"; # -z File has zero size (is empty). } my @doc_read = ; chomp @doc_read; foreach $_ (@doc_read) { my @result = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain spaces or tabs next; } push (@$value , $result[3]); } $hash{$arg} = $value; $value = (); # emptying the string_ref for the next ARGV close (READ) or die ("Could not close: ".$arg." - $!\n"); } my $max_idx = max map $#$_, values %hash; my $tb = Text::Table->new; for my $i (0 .. $max_idx) { $tb->add( map $hash{$_}[$i], sort keys %hash); } return $tb; } my $h_table = hash_sub(@ARGV); print $h_table;