in reply to How can i catch strings matching a regex across multiple lines?

The problem is this line

my ($id) = ( $_ =~ /Logical device ID=(\w+)/ );

If you add

use Data::Dump; dd { line => $_, id => $id };
right afterwards, and run your program, you can see that $id gets reinitialized upon each iteration of the loop (with each new line read)

You want this

my $id; while.... { $id = $1 if /Logical device ID=(\w+)/ ; dd { line => $_, id => $id }; }

See Tutorials: Variable Scoping in Perl: the basics, Coping with Scoping , Mini-Tutorial: Perl's Memory Management, Lexical scoping like a fox, Basic debugging checklist , brian's Guide to Solving Any Perl Problem

Also, see Parse::Report - parse Perl format-ed reports.

Replies are listed 'Best First'.
Re^2: How can i catch strings matching a regex across multiple lines?
by NetWallah (Canon) on Jun 30, 2012 at 23:14 UTC
    I dont think the problem has anything to do with $id.

    The much bigger potential perpetrator is the "$/" setting.
    Since you expect to be reading lines, you need a record separator - otherwise, the entire stream will be "slurp"ed, and you will see only one record, which matches your symptoms.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Not quite. See perlvar, "INPUT_RECORD_SEPARATOR": setting $/ to an empty string sets the input record separator to "two or more consecutive empty lines." That's what he wants here, since his records are separated by a blank line.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Re^2: How can i catch strings matching a regex across multiple lines?
by CountZero (Bishop) on Jul 01, 2012 at 11:15 UTC
    you can see that $id gets reinitialized upon each iteration of the loop (with each new line read)
    But he is reading records, not lines and therefore it is perfectly OK --even recommended-- to re-initialize $id each time through the loop.

    The solution is to add the g modifier to the regex.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics