The_Rev has asked for the wisdom of the Perl Monks concerning the following question:

Hello again, I have a snippet of code that produces:
Foo Before: Topology IPClassA subnet_address=0 Topology IPClassA Device model_name=1 Topology IPClassB Device model_name=2
My objective is to retain any unique order after every Topology Entry. This is just an example, I actually have a lot more data I'm working with. This is my code:
use strict; # Global data structures use vars qw(@foo @bar); @bar = qw(Topology IPClassA subnet_address=0 Topology IPClassA Device model_name=1 Topology IPClassB Device model_name=2); my $i = 0; my $j = 1; print "Foo Before:\n\n"; foreach $_ (@bar){ print "$_\n";} push @foo, "Topology"; foreach $_ (@bar) { if ($bar[$i] eq $bar[$j]) { $i ++; $j ++; } else { push @foo, $bar[$j]; $i = 0; $j ++; } } print "\nFoo After:\n\n"; foreach $_ (@foo){ print "$_\n";}
This is the output:
Foo After: Topology IPClassA subnet_address=0 Device model_name=1 IPClassB Device model_name=2
It works great except for the fact that I need to push Topology on the array just before IPClassB, because this isn't the same pattern as the previous path beggining with the root Topology. Does anyone have any suggestions. Thanks

Replies are listed 'Best First'.
Re: Unique Array Data
by dws (Chancellor) on Mar 07, 2002 at 04:27 UTC
    My objective is to retain any unique order after every Topology Entry. ... It works great except for the fact that I need to push Topology on the array just before IPClassB, because this isn't the same pattern as the previous path beggining with the root Topology.

    Huh? What does "same pattern" have to do with retaining unique order after each "Topology" entry? Please say more about what "unique order" you're trying to maintain. Perhaps a more extensive set of examples (data, not code) might help.

      My goal is to keep any data from 'each' Topology entry forward that is unique from the one prior, and if the element directly after the Topology entry is unique from the one prior, I need to retain an entry for Topology....whoo that was a mouthful. I hope I made that clear. Thanks dws
        My goal is to keep any data from 'each' Topology entry forward that is unique from the one prior, and if the element directly after the Topology entry is unique from the one prior, I need to retain an entry for Topology.

        Does this mean that you're considering each Topology "block" pairwise, and that "uniqueness" only applies to the previous pair? If so, why are you discarding some (but not all) Topology entries? Or are you discarding every alternate Topology (by way of alternation)?

        The Foo Array after should look like this:
        Topology IPClassA subnet_address=0 Device model_name=1 Topology IPClassB Device model_name=2
        with Topology preceeding IPClassB