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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|