yitzle wrote: > On 6/22/08, Pad <[EMAIL PROTECTED]> wrote: >> Need your help again! >> >> I have a file that contains several _begin and _end classes with >> _begin is that start of the block and _end being the end of block. >> Sometimes we miss either _begin or _end. I am trying to write a >> script that find every _begin should contain _end. If for reasons >> _end is missing, it should error out. Likewise, if you have _end, you >> should expect to have _begin in the previous lines. Can you help me >> how to check this condition in my script? >> >> thanks >> pad > > More detail about the format would help. > Assuming the expected format is something like: > > _begin CLASS_A > ... > _end CLASS_A > ... > _begin CLASS_B > ... > _end CLASS_B > > > > == CODE == > > #!/usr/bin/perl > > use strict; > use warnings; > > my $in_class; > > while ( <> ) { > if ( /^_begin (.*)$/ ) { > if ( defined $in_class ) { > print "Err. Found begin $1 while in class > $in_class\n"; > } > > $in_class = $1; > > } elsif ( /^_end (.*)$/ ) { > if ( not defined $in_class ) { > print "Err. End class $1 found without a begin.\n"; > } > > $in_class = undef; > } > } > > > == CODE == > > You can deal with nested classes using an array instead of a scalar $in_class.
This is a fairly sound reply, but I think could be improved by - Saying what your assumptions are. Because the OP was very vague, it is fair to assume that the tags appear at the beginning of the records, but you should have documented that assumption. I don't think it's at all reasonable to require exactly one space after the _begin and _end tags - Always writing a solution to the question asked. Elaborate it with more information about the same problem if you wish, but don't augment the problem from your imagination. Eighty percent of your code does nothing relevant to the question - Try not to use abbreviations unless they make your post more readable, so print "Error: end class $1 found without a begin\n"; - Reading perldoc perlstyle which recommends (amongst other things) that you a) Don't write cuddled elses or elsifs b) use a four-character indent HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/