Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Report generation through formats

by ankit.tayal560 (Beadle)
on Oct 14, 2016 at 06:42 UTC ( [id://1173985]=perlquestion: print w/replies, xml ) Need Help??

ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

I want to read a file named sports.txt and generate a report in a file named blank.txt with proper formatting. I've written this code to perform the task but the format in which I want the report I am unable to get that

CODE : format DATA2= ------------------------------------------------------------ @<<<<<<<<<<< @<<<<<<<<< @######### @######## $name $format $matches $runs ------------------------------------------------------------ . format DATA2_TOP= ============================================================ Name Format of match matches played runs scored page@<< $% ============================================================ . #select(DATA2); open(DATA,"<C:/Perl/perl_tests/sports.txt"); @array=<DATA>; close(DATA); open(DATA2,">>c:/perl/perl_tests/blank.txt"); foreach(@array) { chop; ($name,$format,$matches,$runs)=(split(/!/)); write(DATA2); } open(DATA2,"<c:/perl/perl_tests/blank.txt"); while(<DATA2>) { print("$_\n"); } close(DATA2);
FORMATTED REPORT WHICH I AM GETTING IS : ============================================================ Name Format of match matches played runs scored page 1 ============================================================ ------------------------------------------------------------ sachin tendulkar ODI 434 12000 ------------------------------------------------------------ ------------------------------------------------------------ sachin tendulkar Test 246 10900 ------------------------------------------------------------ ------------------------------------------------------------ sachin tendulkar T20 189 5000 ------------------------------------------------------------ ------------------------------------------------------------ sourav ganguly ODI 334 8000 ------------------------------------------------------------ ------------------------------------------------------------ sourav ganguly Test 235 5000 ------------------------------------------------------------ ------------------------------------------------------------ sourav ganguly T20 124 1800 ------------------------------------------------------------ ------------------------------------------------------------ rahul dravid ODI 387 9000 ------------------------------------------------------------ ------------------------------------------------------------ rahul dravid Test 212 5980 ------------------------------------------------------------ ------------------------------------------------------------ rahul dravid T20 43 134 ------------------------------------------------------------
FORMATTED REPORT WHICH I WANT IS : ============================================================ Name Format of match matches played runs scored page 1 ============================================================ ------------------------------------------------------------ sachin tendulkar ODI 434 12000 sachin tendulkar Test 246 10900 sachin tendulkar T20 189 5000 ------------------------------------------------------------ ------------------------------------------------------------ sourav ganguly ODI 334 8000 sourav ganguly Test 235 5000 sourav ganguly T20 124 1800 ------------------------------------------------------------ ------------------------------------------------------------ rahul dravid ODI 387 9000 rahul dravid Test 212 5980 rahul dravid T20 43 134 ------------------------------------------------------------
sports.txt file is : sachin tendulkar!ODI!434!12000 sachin tendulkar!Test!246!10900 sachin tendulkar!T20!189!5000 sourav ganguly!ODI!334!8000 sourav ganguly!Test!235!5000 sourav ganguly!T20!124!1800 rahul dravid!ODI!387!9000 rahul dravid!Test!212!5980 rahul dravid!T20!43!1345

Any Suggestions about how to proceed with this issue?

Replies are listed 'Best First'.
Re: Report generation through formats
by Discipulus (Canon) on Oct 14, 2016 at 07:08 UTC
    hello ankit.tayal560,

    I abandoned the use of formats many years ago, and at first glance i think it is not possible to achieve what you want.

    But as side note you have many uncautious statements:

    • use strict; and use warnings; are missing. they are very very useful, dont miss them! never
    • open(DATA,"<C:/Perl/perl_tests/sports.txt"); is the worst way to open a file in 2016. you must use open my $file_handle, '<', 'c:/path/file.ext' or die $!;
    • chop cut the last char I think you want chomp that remove newline at the end if present.
    • UPDATE print("$_\n"); will provoke print interpreted as function at.. use print "$_\n"; or say $_; or if you need parens as first think after print use + sign like in print +(join "\t",@arr), "\n";
    L*

    UPDATE: i'd go with something like:

    use strict; use warnings; my $cur_pers = ''; my $sep = ('-'x70)."\n"; # print header print '=' x 70, "\n", (join "\t",('Name','Format of match', 'matches played','runs','scored','page 1')), "\n", '=' x 70, "\n"; while (<DATA>){ chomp; my @elements = split /!/,$_; # check if person change if ( $cur_pers ne $elements[0]){ print $sep; $cur_pers = $elements[0]; } print +(join "\t",@elements),"\n" } __DATA__ sachin tendulkar!ODI!434!12000 sachin tendulkar!Test!246!10900 sachin tendulkar!T20!189!5000 sourav ganguly!ODI!334!8000 sourav ganguly!Test!235!5000 sourav ganguly!T20!124!1800 rahul dravid!ODI!387!9000 rahul dravid!Test!212!5980 rahul dravid!T20!43!1345

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thanks for the suggestions . I will definitely incorporate then into the script. but apart from using formats also is there any other way to achieve the results which I want??/

        For your report, printf is plenty sufficient (See sprintf for the formatting codes). For example:

        use strict; use warnings; my $APPROX_LINES_PER_PAGE = 4; my $sep = ('-'x60)."\n"; my $header = <<HEADER; ============================================================ Name Format of match matches played runs scored page %d ============================================================ HEADER my $format = "%-18s %-8s %-15s %s\n"; my $cur_pers = ''; my $page = 0; my $lines = 0; while (<DATA>){ chomp; my @elements = split /!/,$_; if (0 == $page or $lines >= $APPROX_LINES_PER_PAGE) { print $sep if $page; print "\n";# or do whatever you want to do between pages $page++; $lines = 0; $cur_pers = ''; printf $header, $page; } # check if person change if ($cur_pers and $cur_pers ne $elements[0]) { print $sep; $lines++; } $cur_pers = $elements[0]; printf $format, @elements; $lines++; } print $sep; print "\n"; __DATA__ sachin tendulkar!ODI!434!12000 sachin tendulkar!Test!246!10900 sachin tendulkar!T20!189!5000 sourav ganguly!ODI!334!8000 sourav ganguly!Test!235!5000 sourav ganguly!T20!124!1800 rahul dravid!ODI!387!9000 rahul dravid!Test!212!5980 rahul dravid!T20!43!1345

        I've added some sample pagination - I'm not sure what you had in mind there, but you should be able to extend it to what you wanted.

        Good Day,
            Dean

        Remove perl from the equation, write down the steps to achieve this, take the steps you have and apply them to your data. Go round this loop a few times to iron out the bugs. See Pseudocode. Once you have a working process then implement it in perl (or whatever).

Re: Report generation through formats
by RonW (Parson) on Oct 14, 2016 at 16:10 UTC

    Just FYI, in your DATA2_TOP format, your headers are not aligned with your data columns in your DATA2 format.

    Perl 5's formats are designed for fixed-width characters, so you need to align the columns based on the numbers of characters from the margin to where you want each column to start. Then, of course, you need to display the output using a mono-spaced font.

    Anyway, the following should be closer to what you want:

    format DATA2= ----------------------------------------------------------------- @<<<<<<<<<<< @<<<<<<<<< @######### @######## $name $format $matches $runs ----------------------------------------------------------------- . format DATA2_TOP= ================================================================= Name Format of match matches played runs scored page@<< $% ================================================================= .
Re: Report generation through formats ( Perl6::Form )
by Anonymous Monk on Oct 14, 2016 at 07:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1173985]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found