#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Data::Dump qw/pp/; use constant { MEMORY => 0, LANGUAGE => 1, CPU => 2, CORES => 3, }; my $file1 =<) { my ($app, @tokens) = split /[,\s]+/,$line; push @{$apps{$app}},@tokens } open $file, '<', \$file2 or die "$!"; while (my $line = <$file>) { my ($app, @tokens) = split /[,\s]+/,$line; push @{$apps{$app}},@tokens } close $file; # Op's requirement: # Need to print name of apps that have CPU value 2, sorted by their # memory size e.g App1, App4 and App5 # pp \%apps; ### UNCOMMENT this to see the Hash of Array # filter using grep to find the apps that use only 2 CORES my @core2Apps = grep{$apps{$_}[CORES]==2} keys %apps; # Now sort the results my @sortedAppsbyMemory = sort{$apps{$a}[MEMORY] <=> $apps{$b}[MEMORY] }@core2Apps; foreach my $application (@sortedAppsbyMemory) { print "$application @{$apps{$application}}\n"; } __END__ Prints: App4 4 PHP 2.8 2 App1 4 Perl 1.5 2 App5 8 C# 2.8 2 Note: App1 and App4 are equal in terms of CORES and MEMORY. Their order is unpredictable without further specification or considerations involving previous sorts. Above App4 sorts above App1. How to force App1 to be above App4 in all situations is an exercise left to the reader.