Perl Monk, Perl Meditation PerlMonks

### Re: Arithmatic operation in PERL

by mkmcconn (Chaplain)
 on Nov 20, 2001 at 00:31 UTC ( #126378=note: print w/replies, xml ) Need Help??

in reply to Arithmatic operation in PERL

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

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://126378]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-03-28 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (69 votes). Check out past polls.

Notices?