I wish I could to this without having to add the new values to a new variable.

There's no reason you can't do this with the code johngg provided:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $data = [ [ 123 ], [ 789 ], [ 'dup' ], [ 456 ], [ 123 ], [ 'dup' ], [ 543 ], ]; dd $data; ;; $data = do { my %seen; [ grep { not $seen{ $_->[0] }++ } @$data ]; }; dd $data; " [[123], [789], ["dup"], [456], [123], ["dup"], [543]] [[123], [789], ["dup"], [456], [543]]

The problem with this or any similar approach is that there will be a moment after the anonymous array
    [ grep { ... } @$data ]
is built and before its reference address is taken and assigned to  $data when two possibly very large arrays (and a hash!) will exist in memory and may exhaust your system memory. (I say "possibly" because you say nothing about your actual application.)

One way to ameliorate, but not, unfortunately, completely eliminate, this effect would be to make the input array unique "in place":

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $data = [ [ 123 ], [ 789 ], [ 'dup' ], [ 456 ], [ 'dup' ], [ 123 ], [ 543 ], ]; dd $data; ;; my %seen; my $lo = 0; for (my $hi = 0; $hi <= $#$data; ) { ++$seen{ $data->[$lo][0] = $data->[$hi][0] }; ++$lo; ++$hi while $hi <= $#$data && $seen{ $data->[$hi][0] }; } $#$data = $lo-1; dd $data; " [[123], [789], ["dup"], [456], ["dup"], [123], [543]] [[123], [789], ["dup"], [456], [543]]
This leaves you with just one array to worry about in terms of memory consumption, but the hash still consumes memory, however temporarily.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Remove duplicated data from array ref by AnomalousMonk
in thread Remove duplicated data from array ref by Maresia

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.