Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Arithmatic operation in PERL

by mkmcconn (Chaplain)
on Nov 20, 2001 at 00:31 UTC ( [id://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

Log In?
Username:
Password:

What's my password?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-28 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found