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

sandhya has asked for the wisdom of the Perl Monks concerning the following question:

i need coding for the below sentance 1. If the number of details records does not tally with the count given in the Total Count field at the end of the file, then 1.1. Reject the file 1.2. Record the error details in the error log

Replies are listed 'Best First'.
Re: pls help in perl
by BrowserUk (Patriarch) on May 11, 2012 at 12:23 UTC
    i need coding for ...

    If you need it, try writing it.

    Or read some books and learn how to write it.

    Or employ someone to write it for you.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: pls help in perl
by pemungkah (Priest) on May 11, 2012 at 21:27 UTC
    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.)
Re: pls help in perl
by uday_sagar (Scribe) on May 11, 2012 at 12:23 UTC
    Sandhya, Can you elaborate your question? I mean, can you pass the file which contains total count field and the error details.