http://www.perlmonks.org?node_id=970105


in reply to pls help in perl

Enter Perl's English-like syntax:
# "if..." if ( # the number of details records $details_record_count # does not tally with (is not equal to) != # the count given in the total count fie +ld at the end of the file # (we'll assume you've read the file and + have it already) $total_count) # then { # 1.1 Reject the file push @rejected_files, @filename; # 1.2 Record error details in the error log print $error_log "$file total count field does not agree ($details +_record_count actual, $total_count in file)\n"; }
Most of the time Perl does a really good job of expressing things in very close to the original English statement. Note that I've left out the reading of the file, extraction of the count, and setup of the error log, because I'm giving you the opportunity to learn how to do that part. Note also that you'll need to print the rejects at the bottom of your program (hint: join("\n",@rejected_files)). I'm providing this to encourage you to go ahead and try stuff and see if it works (writing tests for your program will help with this) because Perl really is very close to a natural language in terms of expressing things. Let's pull those comments out and see how it reads without them:
# if the number of actual detail records doesn't match the count in th +e file, mark this file as rejected and print a message to the error l +og. if ( $details_record_count != $total_count) { push @rejected_files, @filename; print $error_log "$file total count field does not agree ($details +_record_count actual, $total_count in file)\n"; }
See? Not hard at all! You can do it. Go ahead and put the comments in to help you think it out if necessary. You can always remove or shorten them when you're done. (Use a revision control system; it'll save you incredible amounts of work.)