in reply to Re: finding the first unique element in an array
in thread finding the first unique element in an array

As an aside, I've removed your C-style loop, which is error-prone
Error-prone is in the eye of the beholder, and the C-style loop avoids the ugly $#array syntax.
Your data structures also scream for a hash, as using parallel arrays also is error prone
That would be a array of hashes then. But that has a severe drawback: it uses a lot of memory. Aggregates have a lot of overhead in Perl. The three parallel arrays only use three aggregates - regardless of the number of entities stored. Putting each entity in its own hash gives you an aggregate for each entity (that is, for each element of the original @name1). And that adds up. Fast.

Below is a program that shows the difference in memory usage . The hashes solution uses almost two and a half times the amount of memory.

#!/usr/bin/perl use strict; use warnings; use Devel::Size qw 'total_size'; my $size = 100_000; my (@name1, @name2, @percent); my @big; sub r_name { # Make a random name. my $r = ""; $r .= ('a' .. 'z')[rand 26] for 1 .. 3 + rand(5); return $r; } for (1 .. $size) { my $name1 = r_name; my $name2 = r_name; my $perc = rand(100); push @name1, $name1; push @name2, $name2; push @percent, $perc; push @big, {name1 => $name1, name2 => $name2, percent => $perc}; } my $size_3 = total_size(\@name1) + total_size(\@name2) + total_size(\@ +percent); my $size_1 = total_size(\@big); printf "Three arrays: %10d (%6.2f)\n", $size_3, $size_3 / $size; printf "One structure: %10d (%6.2f)\n", $size_1, $size_1 / $size; __END__ Three arrays: 9573279 ( 95.73) One structure: 23724662 (237.25)