use strict; use warnings; use Devel::Size qw( total_size ); my $file = join '', map pack('NN', @$_), ( [ 273, 1234 ], [ 273, 5678 ], [ 274, 1234 ], [ 275, 5678 ], [ 276, 1234 ], [ 277, 5678 ], [ 278, 1234 ], [ 278, 5678 ], ); my %ValArrayByID; while ($file =~ /(.{8})/g) { my ($ID, $Val) = unpack('NN', $1); push @{$ValArrayByID{$ID}}, $Val; } print("File size: ", length($file), " bytes\n"); print("Memory usage: ", total_size(\%ValArrayByID), " bytes\n"); #### File size: 64 bytes Memory usage: 922 bytes