extracting information from lines, put them into arrays

hi
I need a little help writing this small perl script. I'm trying to extract the values from each line in a file and find the average for example

cat school

Highschool 100, 123, 135
Middleschool 41, 67, 54
Elementary 76, 315, 384

./average.pl
highschool: 119.3
middleschool: 54
elementary: 258.3

I tried using this script to test if the array would take the values but it didnt.

#!/usr/bin/perl
$str="a few numbers are 41, 32, and 56.";
$str1=$str;
$str1=~ s/\D*(\d+)\D*(\d+)\D*(\d+)/$1--$2--$3/;
#this part didnt work out
@array=($1, $2, $3);
print $array[3];

I was wondering if I could get some helpful answers thanks! :smiley:

Here's one way:

 
$ cat avg.pl
#!/usr/bin/perl
use strict;
my $line;
my $val;
my $school;
my $grades;
my $score;
my $total;
my $avg;
my $i;
open ( INFILE, "<avg.dat" ) or
   die "ERROR:  Unable to open file:  avg.dat" ;
while ($line = <INFILE>) {
   chomp $line;
   ($school,$grades) = split(' ', $line, 2);
   $i = 0;
   $total = 0;
   foreach $score (split(',', $grades)) {
      $i++;
      $total += $score;
   }
   $avg = $total/$i;
   printf "%s:  %.1f\n", $school, $avg;
}
close (INFILE);
exit;
 
$ cat avg.dat
Highschool 100, 123, 135
Middleschool 41, 67, 54
Elementary 76, 315, 384

$ ./avg.pl
Highschool:  119.3
Middleschool:  54.0
Elementary:  258.3

thank you very much jsmithstl!! works perfectly