I'm slow again. This reads DATA (once), one line at a time, to populate '@movie'. Subroutine 'sum()' adds arrays of numbers. Otherwise, not much different from others.
#!/usr/bin/perl -w
use strict;
my @movie;
my $seen = 0;
my $collected;
while (<DATA>){
if (my ($film,$show) = /^\*.+Movie="(.+)"\s*show=(\w+)/){
push (@{$movie[$seen-1]},$collected) if $collected;
$movie[$seen++] = [($film,$show)];
$collected = 0;
}
else{
my @take = grep { $_ >= 20} split /\s/;
$collected = sum($collected,@take);
}
}
push (@{$movie[$seen-1]},$collected) or die $!;
print "Movie\tShow\tSumCollection>20\n";
print "$_->[0]\t$_->[1]\t$_->[2]\n" for @movie;
# use Data::Dumper;
# print "\n\@movie Array looks like this: \n";
# print Dumper(\@movie);
sub sum
{
my $x;
for (@_){
$x += $_;
}
return $x;
}
__DATA__
* Movie="ABC" show=4
10 20 30 14 90 30
21 13 11 10 09 23
22 05 22 15 19 20
* Movie="XYZ" show=4
10 20 30 14 90 30
21 13 11 10 09 23
22 05 22 15 19 10
FWIW
(changed some details after posting).
mkmcconn
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.