in reply to Finding the parent of a text in a file

You can transform the input lines into "full paths", i.e. include all the antecendents to each element. Then, you can just run standard diff (Algorithm::Diff) on the items:
#!/usr/bin/perl use warnings; use strict; use Algorithm::Diff qw{ diff }; open my $F1, '<', '1.in' or die $!; my @lines1 = <$F1>; open my $F2, '<', '2.in' or die $!; my @lines2 = <$F2>; chomp @lines1; chomp @lines2; # Transform the input lines. my @path = q(); my @indent = q(); for my $line (@lines1, @lines2) { my ($this_indent, $key) = $line =~ /^( +)(.*)/; if ($this_indent eq $indent[-1]) { $path[-1] = $key } else { if ($this_indent le $indent[-1]) { pop @indent, pop @path while $indent[-1] ge $this_indent; } push @path, $key; push @indent, $this_indent; } $line = join '/', @path; } # Compare the lines. my @diffs = diff(\@lines1, \@lines2); for my $diff (@diffs) { for my $line (@$diff) { print "@$line\n"; } }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Finding the parent of a text in a file
by ExperimentsWithPerl (Acolyte) on Sep 04, 2015 at 22:48 UTC

    Thank you Choroba.This indeed looks useful.
    However this code is not able to proceed beyond
    $line = join '/', @path;
    (just before # Compare the lines)
    Its getting stuck there.

      I ran it on the input you provided, I changed the 2nd file a bit more and got the following result:
      - 5 /system/security/management-ip/protocol/shutdown + 5 /system/security/management-ip/protocol/reboot - 7 /system/security/management-ip/protocol/entry 1 - 8 /system/security/management-ip/protocol/entry 1/snmp ok + 7 /system/security/management-ip/protocol/entry 2 + 8 /system/security/management-ip/protocol/entry 2/snmp ok
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Thanks Choroba.
        Is it taking more time to complete.Because the code is showing no progress for few minutes.
        I tried to give some print statement just outside and inside the compare loop but nothing got printed.