Hi monks, I have another question that is probably super simple, but I keep hitting a wall. Let's say I have a list of paths that represent a tree, like so:

Valentine's Day Valentine's Day|Cards Valentine's Day|Flowers Valentine's|Candy Valentine's|Telegrams
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 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

In reply to Tree path analyzer regex problem (maybe other issues)? by bryank

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.