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

I'm trying to find a clever way of determining a list of unique string from the following array data:
$VAR1 = 'Devices_CLI_Inspection'; $VAR2 = 'Devices_CLI_Inspection_L2'; $VAR3 = 'Devices_CLI_Inspection_L2_FilterDatabase_General'; $VAR4 = 'Devices_CLI_Inspection_L2_SpanningTree'; $VAR5 = 'Devices_CLI_Inspection_L2_SpanningTree_MST'; $VAR6 = 'Devices_CLI_Inspection_L2_VLAN_General'; $VAR7 = 'Devices_CLI_Inspection_System_Config';
I would expect the output to look like this:
Devices_CLI_Inspection L2 FilterDatabase_General SpanningTree MST L2 VLAN_General System_Config
Any thoughts?

Replies are listed 'Best First'.
Re: String parsing
by toolic (Bishop) on Mar 25, 2010 at 19:23 UTC
    Tree::Builder might help:
    use strict; use warnings; use Tree::Builder; use Data::Dumper; my @items = qw( Devices_CLI_Inspection Devices_CLI_Inspection_L2 Devices_CLI_Inspection_L2_FilterDatabase_General Devices_CLI_Inspection_L2_SpanningTree Devices_CLI_Inspection_L2_SpanningTree_MST Devices_CLI_Inspection_L2_VLAN_General Devices_CLI_Inspection_System_Config ); my $top = shift @items; my @items2 = @items; for (@items2) { s/^Devices_CLI_Inspection//; s{_}{/}g; } my $tb = Tree::Builder->new(); $tb->add($_) for @items2; my %hash=$tb->getTree(); print Dumper(\%hash); __END__ $VAR1 = { '' => { 'System' => { 'Config' => {} }, 'L2' => { 'VLAN' => { 'General' => {} }, 'SpanningTree' => { 'MST' => {} }, 'FilterDatabase' => { 'General' => {} } } } };
Re: String parsing
by Jenda (Abbot) on Mar 25, 2010 at 19:28 UTC

    Have a look at Text::Trie.

    use Data::Dump qw(dump); use Text::Trie qw(Trie walkTrie); my @trie = Trie qw( Devices_CLI_Inspection Devices_CLI_Inspection_L2 Devices_CLI_Inspection_L2_FilterDatabase_General Devices_CLI_Inspection_L2_SpanningTree Devices_CLI_Inspection_L2_SpanningTree_MST Devices_CLI_Inspection_L2_VLAN_General Devices_CLI_Inspection_System_Config ); dump(\@trie); walkTrie sub {print("[",shift,"]")}, sub {print(shift)}, sub {print "->"}, sub {print ","}, sub {print "("}, sub {print ")"}, @trie;

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: String parsing
by Anonymous Monk on Mar 25, 2010 at 19:01 UTC

    You could try splitting on '_' and creating hashes of hashes of hashes of ...

    Why does VLAN_General get its own bucket. It seems as if it should go with the bucket right above it.

      ya unfortunately the order above is completely random...but I think your idea of multiple hashes might do the trick. I'll give it a try.

      Thanks!

        Hashes will sort out uniqueness independently of how random the input is.

        The only problem you'll run into is that things like System_Config would be split into two (unlike what you seem to want according to your sample), even if you stop splitting after 4 underscores (which is the minimum depth required for getting a separate L2 entry). So you'd have to invent some additional logic to determine how deep to split...