in reply to Re: Finding the parent of a text in a file
in thread Finding the parent of a text in a file

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 .

  • Comment on Re^2: Finding the parent of a text in a file

Replies are listed 'Best First'.
Re^3: Finding the parent of a text in a file
by Anonymous Monk on Sep 05, 2015 at 00:01 UTC

    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"}, } );
        { my $line1 = $file1[i]; my $line2 = $file2[j]; my $full1 = fullpath($line1); my $full2 = fullpath($line2); }

        "my"s are local to the enclosing block - these six lines do nothing.

        Please *never* comment out "use strict" and "use warnings"

Re^3: Finding the parent of a text in a file
by Anonymous Monk on Sep 05, 2015 at 17:39 UTC

    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.

        Is there any way I can send attachments ...

        I don't know about attachments per se, but you do have a public scratchpad where you can post something temporarily for viewing or pickup by others. However, it's public, so if you don't want it potentially seen by all and sundry, best find another way. (You also have a private scratchpad, but I think that's for you alone; you cannot grant access unless you give someone your password, allowing them to sign on as you.) I say the scratchpads are "temporary," but I don't really know the policy on this. I do know I've had some stuff on my scratchpads for quite a while and it's never been touched by the PsTB, nor has there ever been any suggestion about any sort of cleanup. Of course, if you start posting mega- or gigabytes of stuff, you may attract the attention of janitors (see What do Janitors do?) or even Gods!


        Give a man a fish:  <%-{-{-{-<

        Answers needed:

        1) how large are your input files (in lines)?

        2) What OS are you running on?

        3) Are your input files always the same length (in lines)? With only some words changed? Or with whole new lines added?