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)

In reply to Re^2: finding the first unique element in an array by Anonymous Monk
in thread finding the first unique element in an array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.