The subroutine needs some way of knowing (or, at least, guessing) whether it has been passed an array or an array reference.

First thought is to test whether the first argument is an array reference:

if (ref($_[0]) eq 'ARRAY') ...

See ref. The problem with that approach is that it won’t work if the array passed into the subroutine happens to have an array reference as its first element.

A better approach is to test whether the subroutine is called in list context:

my @array_with_duplicates = wantarray ? @_ : @{ $_[0] };

See wantarray. If the sub is called in list context (i.e., the caller is expecting it to return a list), then from the specification we can assume the array was passed in as a list. Otherwise, we assume the array was passed in as a reference.

Here is how I would solve the problem:

#! perl use strict; use warnings; use Data::Dump; my @array; while (<DATA>) { chomp; push @array, $_; } dd \@array; @array = clean(@array); # or clean(\@array); dd \@array; sub clean { my @array_with_duplicates = wantarray ? @_ : @{$_[0]}; my (@array_no_duplicates, %hash); for (@array_with_duplicates) { unless (exists $hash{$_}) { push @array_no_duplicates, $_; $hash{$_} = undef; } } return @array_no_duplicates if wantarray; @{ $_[0] } = @array_no_duplicates; } __DATA__ Frieda Adele Briony Hermione Cathy Dorothy Cathy Erin Ida Frieda Frieda Gertrude Hermione

Output:

22:14 >perl 890_SoPW.pl [ "Frieda", "Adele", "Briony", "Hermione", "Cathy", "Dorothy", "Cathy", "Erin", "Ida", "Frieda", "Frieda", "Gertrude", "Hermione", ] [ "Frieda", "Adele", "Briony", "Hermione", "Cathy", "Dorothy", "Erin", "Ida", "Gertrude", ] 22:14 >

Note: this approach does not rely on sorting the list, but outputs the no-duplicates version in its original order. The specification as given does not mention sorting.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: How to combine these 2 subroutines in one? by Athanasius
in thread How to combine these 2 subroutines in one? 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.