#!/usr/bin/perl use strict; use warnings; use Text::Table; use Tie::IxHash; use Fcntl qw( :flock ); use List::Util qw( max ); use POSIX qw( strftime ); use Data::Dumper qw( Dumper ); use constant ARGUMENTS => scalar 1; $| = 1; my ($value , $table); 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; foreach my $i (0 .. $max_idx) { $tb->add( $i + 1 . ' ' , map $hash{$_}[$i] , sort keys %hash ); } return $tb; } $table = hash_sub(@ARGV); print $table; #### Line_1:Line_1_1:Line_1_2:Line_1_3:Line_1_4 Line_2:Line_2_1:Line_2_2:Line_2_3:Line_2_4 #### Line_3:Line_3_1:Line_3_2:Line_3_3:Line_3_4 #### Line_4:Line_4_1:Line_4_2:Line_4_3:Line_4_4 Line_5:Line_5_1:Line_5_2:Line_6_3:Line_5_4 Line_6:Line_6_1:Line_6_2:Line_6_3:Line_6_4 Line_7:Line_7_1:Line_7_2:Line_7_3:Line_7_4 #### 1 Line_1_3 Line_3_3 Line_4_3 2 Line_2_3 Line_5_3 3 Line_6_3 4 Line_7_3 #### 1 Line_3_3 Line_4_3 Line_1_3 2 Line_5_3 Line_2_3 3 Line_6_3 4 Line_7_3 #### my $t = Tie::IxHash->new(%hash); $t->SortByKey; print Dumper(\$t);