in reply to Joining data structures.

Not clear why you want to do this (less memory usage maybe?), but one way of eliminating $Record would be to store your top N results in a hash, as follows. This means you still need to record 40 entries from $Record, but that's fewer than a thousand, right? An explanation of your motivation might help…

my %results; foreach (1 .. 40) { my $hit = int rand(30); my $id = int rand(1000); $results{$id} = [ $id, $hit ]; # Store ID and hit in array } foreach (0 .. 999) { my $data = int rand(100); # Only stack data we're interested in: if(exists($results{$_})) { push @{$results{$_}}, $data; } } # Sort by descending hit (array[1] entry): my @search_results = sort { $b->[1] <=> $a->[1] } values %results; foreach (@search_results) { my ($hit, $id, $data) = @$_; print "$hit $id $data\n"; }

… and if you're into more variable elimination, @search_results can go easily too — but since it's an array of array references, it takes up next to no space so you'd lose clarity for little gain.

The [ $id, $hit ], @{$results{$_}}, $b->[1] and @$_ stuff are all explained in the Perl references documentation (“perldoc perlref”).