Hmm. Interesting solution.

Turns out though that you should have developed this under strict. There is a minor bug that prevents your routine from running if you dont use your exact perparation code. The problem is that what you pass to the routine never gets used (you do $set=shift; but then iterate over @$s, which just happens to be defined as a global so it works out fine in your sample...) Anyway I fixed that and removed some of the extraneous code. See below.

My problem with this is that you have to preconstruct an array containing references to the scalars in question, which I dont really think meets the letter of my specification. Also the proconstruction is pretty cumbersome. Anyay heres your revised code, mine and some output.

use Data::Dumper; use strict; sub count_unique { print Dumper \@_; my %uniq; $uniq{0+\$_}++ foreach @_; return sprintf ( "CTU: %d unique entities (%d duplicates):\n", scalar keys %uniq, scalar ( grep { $uniq{$_} > 1 } keys %uniq ), ); } sub find_unique_items_in { my $set = shift; print Dumper $set; my (%uniq, @out) = (); for my $i (@$set) { $uniq{ $i }++; } return sprintf ( "FUI: %d unique entities (%d duplicates):\n", scalar keys %uniq, scalar ( grep { $uniq{$_} > 1 } keys %uniq ), ); } my ($x,$y,$z)=qw(a b c); print count_unique($x,$y,$z),$/; print find_unique_items_in([\$x,\$y,\$z]),$/; print count_unique($x,$y,$y),$/; print find_unique_items_in([\$x,\$y,\$y]),$/; print count_unique($x,$x,$y,$y),$/; print find_unique_items_in([\$x,\$x,\$y,\$y]),$/; print count_unique($z,$z,$z,$z),$/; print find_unique_items_in([\$z,\$z,\$z,\$z]),$/; print count_unique($x,$x,$y,$y,$z,$z),$/; print find_unique_items_in([\$x,\$x,\$y,\$y,\$z,\$z]),$/; __END__ $VAR1 = [ 'a', 'b', 'c' ]; CTU: 3 unique entities (0 duplicates): $VAR1 = [ \'a', \'b', \'c' ]; FUI: 3 unique entities (0 duplicates): $VAR1 = [ 'a', 'b', ${\$VAR1->[1]} ]; CTU: 2 unique entities (1 duplicates): $VAR1 = [ \'a', \'b', $VAR1->[1] ]; FUI: 2 unique entities (1 duplicates): $VAR1 = [ 'a', ${\$VAR1->[0]}, 'b', ${\$VAR1->[2]} ]; CTU: 2 unique entities (2 duplicates): $VAR1 = [ \'a', $VAR1->[0], \'b', $VAR1->[2] ]; FUI: 2 unique entities (2 duplicates): $VAR1 = [ 'c', ${\$VAR1->[0]}, ${\$VAR1->[0]}, ${\$VAR1->[0]} ]; CTU: 1 unique entities (1 duplicates): $VAR1 = [ \'c', $VAR1->[0], $VAR1->[0], $VAR1->[0] ]; FUI: 1 unique entities (1 duplicates): $VAR1 = [ 'a', ${\$VAR1->[0]}, 'b', ${\$VAR1->[2]}, 'c', ${\$VAR1->[4]} ]; CTU: 3 unique entities (3 duplicates): $VAR1 = [ \'a', $VAR1->[0], \'b', $VAR1->[2], \'c', $VAR1->[4] ]; FUI: 3 unique entities (3 duplicates):
but as far as I know, it's fundamentally impossible to create a parameter list that can't be stored as a data structure.

And my point is that this is wrong. The parameter list @_ is magic. It contains aliases to the items passed. A normal array contains _copies_ of the items passed. The only way that you can simulate this behaviour is to do the work around you did, which is to pass references to the items in question. But then that completely changes the data being passed and the code that must handle it. And all of this is clear from the dumper results in the code above.

Anyway, good try. :-)

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.


In reply to Re: Re2: Idea vs realization by demerphq
in thread passing arguments by mstone

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.