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

Seems to do what you showed. If not, please say why and provide a more extensive test case.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1140982 use Algorithm::Diff qw(traverse_sequences); use strict; use warnings; my $file1 = <<END; system security management-ip protocol internal shutdown allow entry 1 snmp ok exit END my $file2 = <<END; system security management-ip protocol internal shutdown allow entry 1 snmp failed exit END sub fullpath { my @full; my @answer; for my $line ( shift() =~ /.+\n/g ) { $line =~ /^( *)/; $#full = length($1) - 1; # truncate array push @full, $line; push @answer, join '', grep defined, @full; } return \@answer; } my $full1 = fullpath($file1); my $full2 = fullpath($file2); traverse_sequences( $full1, $full2, { DISCARD_A => sub {print $full1->[shift()], "\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 23:30 UTC

    thanks a lot Anonymous Monk.You are right, it works fine with the example I have given .
    But suppose I add another line below "snmp ok" (at the same level) :-

    ipaddress#1

    Then the code is printing the hirarechy twice for me ..once till "snmp ok" and once till "ipaddress#1".

    Second thing, I tried to open both the config files as a file handler (Becase in real world the files will be too huge to keep in the code) and tried to run the code (with minimal changes), the code didnt work .

      Ah, you changed your requirements.

      You get what you ask for, we're not mindreaders. How are we supposed to figure out what you really want when you don't give examples?

      You never did mention file sizes, did you?

        #!/usr/bin/perl # http://perlmonks.org/?node_id=1140982 use Algorithm::Diff qw(traverse_sequences); #use strict; #use warnings; open (FH , "<123") or die $!; open (FH2 , "<456") or die $!; my @file1 = <FH>; my @file2 = <FH2>; sub fullpath { my @full; my @answer; for my $line ( shift() =~ /.+\n/g ) { $line =~ /^( *)/; $#full = length($1) - 1; # truncate array push @full, $line; push @answer, join '', grep defined, @full; } return \@answer; } print @file1; print @file2; my $len1 = scalar(@file1); print "len1 : $len1\n"; my $len2 = scalar(@file2); print "len2 : $len2\n"; for (my $i==0 ; $i < $len1 ; $i++) { for (my $j==0 ; $j < $len1 ; $j++) { if ($i==$j) { my $line1 = $file1[i]; my $line2 = $file2[j]; my $full1 = fullpath($line1); my $full2 = fullpath($line2); } } } traverse_sequences( $full1, $full2, { # DISCARD_A => sub {print $full1->[shift()], "\n"}, } );

      Saying "(with minimal changes), the code didnt work " without showing what the changes were and why the code didn't work is an insult to all programmers everywhere.

      You owe me an apology.

      You also owe me test cases for the change of requirements you made.

        Hello Anonymous Monk, Apologies for the inconvenience.
        I was about to respond to you but Couldn't login today due to Wifi issues.
        Is there any way I can send attachments here because the code and examples are looking really messy when I am pasting it here.
        If there is no way to send attachment then I will the code anyhow.
        Again I would mention that I am new to this language, and have no intention to insult the monks who are helping me.