Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This Meditation concerns something I have found myself needing to do several times. In various development and systems administration tasks I have found myself with lists of some kind where each entry was something like a variable value, a C pre-processor macro definition or a file path. The sorts of things that can look like this can include PATH and PATH-like variables (on *NIX, MANPATH and so forth). In such cases I have often reached a point where what I needed to do was see each list with only the first occurance of any given entry left in place, and all later repetitions of the entry removed. In other words, I have needed to have lists of things with all duplicates removed, but with the order of the entries preserved as they were (less the repetitions).

The basic algorithm for doing this is widely understood, it appears, judging from what I've turned up on Perl Monks. There are variations in opinion concerning the implementation of this in Perl, with one possible solution employing psuedohashes as the key Perl feature. I personally like the existence of psuedohashes in Perl 5 and if they are to be removed as a failed experiment (because support in the Perl internals for this feature has too many side-effects, and the performance is poor) in Perl6 I won't weep, but I will miss them.

As I've pondered this task several times it has grown in significance for me to the point that, rational or not, it seems like one of the most basic of operations - almost so basic that there ought to be an internal, built-in Perl function to do this ("idem"? - for "idempotant"). I am not seriously going to argue for this but my point is that I have had to think about how to achieve this kind of "list clean-up" just too many times. I want to have a way to just do it without thinking about it any more, and the code chosen to accomplish this goal ought to fulfill my personal preferences in these ways:

  • it ought to be succinct and short
  • it ought to be free of dependency on non-core Perl modules
  • it ought to be "primordial" enough that I can use it flexibly in a number of different circumstances without undoing all sorts of extra fancy bells-and-whistles.

The code I have posted below represents my fulfillment of this goal for myself to the best of my present knowledge. It is so short (3 lines of text) that it could be called obfuscated (the author of the posting it is derived from considered it too obfuscated to recommend, but it suits me nicely, thank-you). It relies on a feature that may not be part of Perl6, but OTOH not on any external modules (hey, things are going to break on Perl6, that's just a fact of life!). And it's certainly basic enough to be called "primordial" IMHO.

The form this code is taking here is what makes sense for direct invocation from a *nix shell (a "perl one-liner" or shell function body). Please look at my home node for a syntax-highlighted rendition.
perl -e ' $ph = [{},]; @aye =$ARGV[0]? @ARGV : (<STDIN>); chomp @aye; map{ exists $ph->{$_} ? $ph->{$_}++ : $ph->[$ph->[0]{$_}=@$ph]++ } @ +aye; print join "\n", sort{ $ph->[0]{$a} <=> $ph->[0]{$b} } keys %{$ph->[ +0]}; ' $*

As to the issue of poor performance due to psuedohash overhead, that just isn't a factor in my thinking. Because the lists of things to be processed by this code will seldom exceed, say, 20 or so, it simply doesn't seem worthwhile to get worked up over the performance aspect. Convenience and these other preferences I've listed weigh in far more for me.

To re-use it most effectively it might be best to turn it into a module. It seems to me that this code sits right on a borderline where opinions can easily fall on either side of that question. On the one hand, this code is so short that it could seem ridiculous to the point of absurdity to make a module of it. OTOH, it seems just challenging enough to commit to memory, that maybe it would be sensible to "modularize" it. Doing so would save some effort for some users; as ashamed as I should probably be to admit it, I am just not always a bright enough Perl hacker to always retain the full range of clever techniques that the rich language that is Perl affords us without having to either consult some reference documentation or else just "use" the wheel that someone else has already coded.

I'd like to know what other Monks think. Should this be a module? Or is it just absurd to contemplate that?


In reply to Order-Preserving and Unique: List Cleanup by Intrepid

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (6)
    As of 2014-09-30 23:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (386 votes), past polls