in reply to Re^4: Printing first and last line
in thread Printing first and last line

Hello again, maheshkumar,

If I understand correctly, the format of your input data has now changed, with blank lines inserted and a line reading “Trace complete.” following each list of traceroutes. And you have tried to adapt the code I gave by changing 2 of the regular expressions.

Well, your change to $line2_re is fine, but $destn_re now has (/*/) which only matches a forward slash followed immediately by an asterisk followed immediately by another forward slash — which doesn’t match your data. I think you want something like this (untested):

# Example lines: # 1 1 ms * 1 ms 192.168.1.1 # 2 14 ms 8 ms 9 ms 73.220.38.1 my $destn_re = qr! ^ # start of line \s* \d+ \s+ ms # 14 ms (?: # EITHER (?: \d+ ms ) # 8 ms | # OR \* # * ) \s+ \d+ \s+ ms # 9 ms \s+ (?: \d{1,3} \. ){3} \d{1,3} # IP address: 192. +168.1.1 $ # end of line !x;

You should study perlretut and then perlre to get a solid understanding of regular expressions.

However, the real problem is that the algorithm needs to be changed to match the new input format. Hint: add a CASE for blank lines, and change CASE default to match lines beginning Trace complete. (But there might be other approaches which would work just as well.) First get the algorithm clear: work it out on paper, going through the input data, and verify that the steps in the new algorithm lead to the desired result. Once you have the algorithm worked out, you will find adapting the Perl code is quite straightforward.

Keep at it, you’re making progress!

Update 1: Fixed and annotated regex in response to OP’s question, below.

Update 2: ++hbm for the solution below using Perl’s “flip-flop” operator.

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^6: Printing first and last line
by maheshkumar (Sexton) on Aug 14, 2012 at 15:01 UTC

    There will be 3 (ms) right as ms appears thrice so are you missing one in the one that you have put?

      Can you advice something to read on the errors i keep on getting, like uninitialised or sometimes global variable and all

        It is useful to begin every script with:

        use strict; use warnings; use diagnostics;

        The diagnostics pragma puts extra information into warning messages, which can help in figuring what the warnings are really about.

        As a rule, concentrate on the first error or warning message and fix it. Often, this will fix the other warnings or errors too.

        Here are some helpful references:

        “Uninitialised” warnings generally mean your variables don’t contain the data you think they do. Get in the habit of printing out the contents of variables. Here is an easy way:

        use Data::Dumper; my %hash; # Fill %hash with data print Dumper(\%hash);

        Data::Dumper is a core module, so you have it already. See Data::Dumper for more information.

        That’s the best advice I can think of at the moment. Hope it helps,

        Athanasius <°(((><contra mundum