bryank has asked for the wisdom of the Perl Monks concerning the following question:
It is assumed that the the order represents the tree's order. The problem is that the tree has some structure errors -- parents are missing in certain locations. I want to create something that would create the missing paths where necessary. For example:Valentine's Day Valentine's Day|Cards Valentine's Day|Flowers Valentine's|Candy Valentine's|Telegrams
Valentine's Day Valentine's Day|Cards Valentine's Day|Flowers Valentine's Valentine's|Candy Valentine's|Telegrams
I realize the above structure makes no sense, but I wanted to illustrate the problem with my current script. It won't notice 'Valentine's' as a path that needs to be created, because the term exists in the previous path. I think I need to tighten my regex, but I am not sure. Can someone help me out? Thanks!
use strict; #use warnings; use Data::Dumper; while(<DATA>) { chomp; my $path = $_; my @paths = split /\|/, $path; my $parent = $paths[-2]; my $child = $paths[-1]; my $prev_path; if ($prev_path !~/^$parent$/) { my $message = 'created new path!'; my @new_paths = @paths; my $discard = pop @new_paths; my $new_path = join '|', @new_paths; print $new_path . "\t" . $message . "\n"; } print $path . "\n"; $prev_path = $path; } __DATA__ Valentine's Day Valentine's Day|Cards Valentine's Day|Flowers Valentine's|Candy Valentine's|Telegrams
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tree path analyzer regex problem (maybe other issues)?
by ikegami (Patriarch) on Jun 23, 2009 at 16:33 UTC | |
|
Re: Tree path analyzer regex problem (maybe other issues)?
by ikegami (Patriarch) on Jun 23, 2009 at 16:50 UTC | |
by bryank (Acolyte) on Jun 23, 2009 at 16:56 UTC | |
by ikegami (Patriarch) on Jun 23, 2009 at 17:03 UTC | |
|
Re: Tree path analyzer regex problem (maybe other issues)?
by kennethk (Abbot) on Jun 23, 2009 at 16:36 UTC | |
by bryank (Acolyte) on Jun 23, 2009 at 16:50 UTC | |
by kennethk (Abbot) on Jun 23, 2009 at 17:05 UTC |