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

SCRIPT TO GENERATE REPORT : use warnings; format DATA2= ------------------------------------------------------------ @<<<<<<<<<<<<< @<<<<<<<<< @######### @######## $name $format $matches $runs ------------------------------------------------------------ . format DATA2_TOP= Records/Data of the trio ============================================================ Name Format of match matches played runs scored ============================================================ . 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); }

My sports.txt file is as follows:

sports.txt file : 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

The formatted report which I am getting in blank.txt is the final report I want but here in this script the heading of the report i.e. "records/data of the trio" is directly given. how can I pass a variable in place of that title.?Any Help is appreciated!

Replies are listed 'Best First'.
Re: How to pass a Format Heading from a variable?
by GrandFather (Saint) on Oct 20, 2016 at 05:33 UTC

    Use a field for the title:

    use strict; use warnings; my ($title, $name, $format, $matches, $runs); format DATA2_TOP= @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| $title ============================================================ Name Format of match matches played runs scored ============================================================ . format DATA= @<<<<<<<<<<<<<<<< @<<<<<<<<< @######### @######## $name, $format, $matches, $runs . my @array = <DATA>; $~ = 'DATA2_TOP'; $title = 'Records/Data of the trio'; write; $~ = 'DATA'; foreach (@array) { chop; ($name, $format, $matches, $runs) = (split(/!/)); write ; } __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

    Prints:

    Records/Data of the trio ============================================================ Name Format of match matches played runs scored ============================================================ 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
    Premature optimization is the root of all job security
Re: How to pass a Format Heading from a variable?
by GrandFather (Saint) on Oct 20, 2016 at 05:56 UTC

    Having never used Perl's format facility before I decided to have a little play. It turns out you can call functions to generate picture data so it's easy to generate a header for each page:

    use strict; use warnings; my ($title, $name, $format, $matches, $runs); format DATA2_TOP= @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| title() ============================================================ Name Format of match matches played runs scored ============================================================ . format DATA= @<<<<<<<<<<<<<<<< @<<<<<<<<< @######### @######## $name, $format, $matches, $runs . my @array = <DATA>; $^L = "--------- page break\n"; $^ = 'DATA2_TOP'; $~ = 'DATA'; $= = 10; # 10 line pages to see page numbering working foreach (@array) { chop; ($name, $format, $matches, $runs) = (split(/!/)); write ; } sub title { return "Records/Data of the trio (Page $%)"; } __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

    Prints:

    Records/Data of the trio (Page 1) ============================================================ Name Format of match matches played runs scored ============================================================ 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 --------- page break Records/Data of the trio (Page 2) ============================================================ Name Format of match matches played runs scored ============================================================ rahul dravid ODI 387 9000 rahul dravid Test 212 5980 rahul dravid T20 43 1345

    Note that I've changed the page length to 10 to get two pages generated. I've also changed the page format form feed string from Perl's default form feed character to make if clear where the page breaks are.

    Premature optimization is the root of all job security
Re: How to pass a Format Heading from a variable?
by kcott (Archbishop) on Oct 20, 2016 at 08:03 UTC