in reply to Re: File parsing query
in thread File parsing query

I think the OP wanted to output the count of widgets at the top of the database section which was counted. That is, something more like this:
my $widgetsFound=0; my $databaseName; my $databaseSection; while( <> ) { if( /^DATABASE:/ ) { if( $databaseName ) { print $databaseName; print "Widgets found: $widgetsFound\n"; print $databaseSection; } $databaseName = $_; $databaseSection = ""; $widgetsFound = 0; } else { $databaseSection .= $_; } if( $something ) { $widgetsFound++; } } # print the last database section that was parsed if( $databaseName ) { print $databaseName; print "Widgets found: $widgetsFound\n"; print $databaseSection; }
Updated: to correct errors noticed by Animator (who answered the original question first as well :) ).

Replies are listed 'Best First'.
Re^3: File parsing query
by Animator (Hermit) on Feb 16, 2005 at 21:03 UTC

    Some important notes:

    • $widgetsFound is not intialised at the start, which can lead to an undefined value that is printed (if there are no widget in the first database-section)
    • The printing of the last database section is always done, even if the file would be empty, which will lead to three undefined values being printed.

    An unimportant note: I posted similar code as a reply to the OP's node before you did :)