in reply to Re^5: Text file processing question
in thread Text file processing question

The re appears not to work for this set of data: i.e if each field can have single words/multiple words except the amount field
Nancy Elm Street 600.00 New service Single phase Stella Barry Petersburg 1000.00 Replacement Single Julie Palm Street 400.00 Replacement Three phase Debbie Brown Bakerville 800.00 New service Three
perl_seeker

Replies are listed 'Best First'.
Re^7: Text file processing question
by BrowserUk (Patriarch) on Dec 21, 2009 at 11:33 UTC

    #! perl -slw use strict; print "Name of consumer Subdivision Amount Type of work Phase"; printf "%-14s %14s %7.2f %-14s %-s\n", m[ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) $ ]x while <DATA>; __DATA__ Nancy Elm Street 600.00 New service Single phase Stella Barry Petersburg 1000.00 Replacement Single Julie Palm Street 400.00 Replacement Three phase Debbie Brown Bakerville 800.00 New service Three

    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.
      Thank u, works wonderfully :)

      perl_seeker
      Hello

      I was trying this script
      #! perl -slw use strict; open DATA, "test2.txt" or die "Cant open file"; print "Receiptno. Consumer name Consumer no. Subdivision + Type of work Phase Cheque no. Amount"; printf "%-11s %-23s %-12s %-14s %-16s %-10s %-11s %-14.2f\n", m[ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ]x while <DATA>;
      with this set of data
      127 Stella Somethinglong 2345 Petersburg New service Single + 23456 1330.00 128 Sandra 4567 Elm Street Replacement Three 23445 4800 +.00 129 Tessa Barry 2345 Bakersville New service Single 37456 + 1330.00
      The third record does not display in the output. Why is that so?

      Also how can we calculate the total of the last column (amount column)?

      perl_seeker

        Hm. I put the code and data from your post together into a single file and it worked perfectly:

        #! perl -slw use strict; #open DATA, "test2.txt" or die "Cant open file"; print "Receiptno. Consumer name Consumer no. Subdivision + Type of work Phase Cheque no. Amount"; printf "%-11s %-23s %-12s %-14s %-16s %-10s %-11s %-14.2f\n", m[ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ]x while <DATA>; __DATA__ 127 Stella Somethinglong 2345 Petersburg New service Single + 23456 1330.00 128 Sandra 4567 Elm Street Replacement Three 23445 4800 +.00 129 Tessa Barry 2345 Bakersville New service Single 37456 + 1330.00

        Output:

        C:\test>816705 Receiptno. Consumer name Consumer no. Subdivision Type o +f work Phase Cheque no. Amount 127 Stella Somethinglong 2345 Petersburg New se +rvice Single 23456 1330.00 128 Sandra 4567 Elm Street Replac +ement Three 23445 4800.00 129 Tessa Barry 2345 Bakersville New se +rvice Single 37456 1330.00

        So I suspect that your data file does not contain a final newline.

        If you want to do something with the values captured, other than just print them, you'll need to assign them to some variables. A minor reworking of the code--which was posted as an example for you to learn from , not to be used verbatim--and you get:

        #! perl -slw use strict; #open DATA, "test2.txt" or die "Cant open file"; print "Receiptno. Consumer name Consumer no. Subdivision + Type of work Phase Cheque no. Amount"; my $total = 0; while( <DATA> ) { my @caps = m[ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ )\s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ( [0-9\.]+ ) \s+ ( \S+ (?: \s \S+ )? ) \s+ ]x; printf "%-11s %-23s %-12s %-14s %-16s %-10s %-11s %-14.2f\n", @cap +s; $total += $caps[ -1 ]; } print "Total: $total"; __DATA__ 127 Stella Somethinglong 2345 Petersburg New service Single + 23456 1330.00 128 Sandra 4567 Elm Street Replacement Three 23445 4800 +.00 129 Tessa Barry 2345 Bakersville New service Single 37456 + 1330.00

        Output:

        C:\test>816705 Receiptno. Consumer name Consumer no. Subdivision Type o +f work Phase Cheque no. Amount 127 Stella Somethinglong 2345 Petersburg New se +rvice Single 23456 1330.00 128 Sandra 4567 Elm Street Replac +ement Three 23445 4800.00 129 Tessa Barry 2345 Bakersville New se +rvice Single 37456 1330.00 Total: 7460

        Which may or may not be what you want, but even if it is, please try to understand what it is doing and modify it for your purposes rather than just coming back each time you need a change. You really should have been able to work that out for yourself. I'm unlikely to be so accomodating next time.


        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.