Here is another way to do...

I don't think it is as fast as could be, but I believe that Corion's initial posting assumes that you are comparing with only the previous line, and also that the last line is always 'good' to keep (but what do I know, he is a Saint, and I am a chump, no wait, an Acolyte, about the same thing though).
#!perl -w use strict; open (DATAFILE,"D:/JUNK/data.txt"); my $file_content = do {local $/; <DATAFILE> }; my @fa1 = split(/\n/,$file_content); my $cnt=0; my %hv=(); #Set to hash with a count to retain order of list foreach (@fa1) { $hv{$_}=$cnt; $cnt++; } my %already_deleted=(); foreach my $v1 (@fa1) { if (exists($already_deleted{$v1})) { next; } my $len1 = length($v1); foreach my $v2 (keys %hv) { if ((exists($already_deleted{$v1})) || (exists($already_deleted{$v +1}))) { next; } my $len2 = length($v2); if (($len2 > $len1) && (substr($v2,0,$len1) eq $v1)) { #Delete the hash key value delete $hv{$v1}; #Store deleted hash values to help move through the #foreach statements quicker with next statements $already_deleted{$v1}=1; } else { #Nothing } } } #Retain the original order, then print 'em out. my @keys = sort{$hv{$a} <=> $hv{$b}} keys %hv; foreach (@keys) {print "$_ \n"}; ___DATAFILE___ Topology/IPClassA Topology/IPClassA/Device Topology/IPClassA/Device/log_ratio Topology/IPClassA/Device/poll_interval Topology/IPClassA/Device = 2 Topology/IPClassA/Device = 2/poll_interval Topology/IPClassA/Device = 2/is_managed Topology/IPClassA/Device = 2/Port Topology/IPClassA/Device = 2/Port/ifPhysAddress Topology/IPClassA/Device = 2/Port/poll_interval Topology/IPClassC Topology/IPClassC/Device Topology/IPClassC/Device/poll_interval Location

In reply to Re: Unique Array Entries by nandeya
in thread Unique Array Entries by The_Rev

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.