[LON-CAPA-cvs] cvs: loncom /interface/spreadsheet Spreadsheet.pm

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 12 May 2005 22:42:58 -0000


albertel		Thu May 12 18:42:58 2005 EDT

  Modified files:              
    /loncom/interface/spreadsheet	Spreadsheet.pm 
  Log:
  - converting everything to use &get_values instead which right now does the grep
  
  
Index: loncom/interface/spreadsheet/Spreadsheet.pm
diff -u loncom/interface/spreadsheet/Spreadsheet.pm:1.43 loncom/interface/spreadsheet/Spreadsheet.pm:1.44
--- loncom/interface/spreadsheet/Spreadsheet.pm:1.43	Fri Apr 29 14:13:07 2005
+++ loncom/interface/spreadsheet/Spreadsheet.pm	Thu May 12 18:42:57 2005
@@ -1,5 +1,5 @@
 #
-# $Id: Spreadsheet.pm,v 1.43 2005/04/29 18:13:07 albertel Exp $
+# $Id: Spreadsheet.pm,v 1.44 2005/05/12 22:42:57 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -328,6 +328,7 @@
       $safeeval->deny(":base_io");
       $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&Apache::lonnet::EXT');
       $safehole->wrap(\&mask,$safeeval,'&mask');
+      $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&logthis');
       $safeeval->share('$@');
       my $code=<<'ENDDEFS';
 # ---------------------------------------------------- Inside of the safe space
@@ -412,8 +413,8 @@
 
 #-------------------------------------------------------
 sub NUM {
-    my $mask=&mask(@_);
-    my $num= $#{@{grep(eval("/$mask/"),keys(%sheet_values))}}+1;
+    my $values=&get_values(@_);
+    my $num= scalar(@$values);
     return $num;   
 }
 
@@ -428,10 +429,10 @@
 #-------------------------------------------------------
 sub BIN {
     my ($low,$high,$lower,$upper)=@_;
-    my $mask=&mask($lower,$upper);
+    my $values=&get_values($lower,$upper);
     my $num=0;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        if (($sheet_values{$_}>=$low) && ($sheet_values{$_}<=$high)) {
+    foreach (@$values) {
+        if (($_>=$low) && ($_<=$high)) {
             $num++;
         }
     }
@@ -450,10 +451,10 @@
 
 #-------------------------------------------------------
 sub SUM {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $sum=0;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        $sum+=$sheet_values{$_};
+    foreach (@$values) {
+        $sum+=$_;
     }
     return $sum;   
 }
@@ -470,11 +471,11 @@
 
 #-------------------------------------------------------
 sub MEAN {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $sum=0; 
     my $num=0;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        $sum+=$sheet_values{$_};
+    foreach (@$values) {
+        $sum+=$_;
         $num++;
     }
     if ($num) {
@@ -496,17 +497,17 @@
 
 #-------------------------------------------------------
 sub STDDEV {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $sum=0; my $num=0;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        $sum+=$sheet_values{$_};
+    foreach (@$values) {
+        $sum+=$_;
         $num++;
     }
     unless ($num>1) { return undef; }
     my $mean=$sum/$num;
     $sum=0;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        $sum+=($sheet_values{$_}-$mean)**2;
+    foreach (@$values) {
+        $sum+=($_-$mean)**2;
     }
     return sqrt($sum/($num-1));    
 }
@@ -523,10 +524,10 @@
 
 #-------------------------------------------------------
 sub PROD {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $prod=1;
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        $prod*=$sheet_values{$_};
+    foreach (@$values) {
+        $prod*=$_;
     }
     return $prod;   
 }
@@ -543,12 +544,11 @@
 
 #-------------------------------------------------------
 sub MAX {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $max='-';
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        unless ($max) { $max=$sheet_values{$_}; }
-        if (($sheet_values{$_}>$max) || ($max eq '-')) { 
-            $max=$sheet_values{$_}; 
+    foreach (@$values) {
+        if (($_>$max) || ($max eq '-')) { 
+            $max=$_; 
         }
     } 
     return $max;   
@@ -566,12 +566,11 @@
 
 #-------------------------------------------------------
 sub MIN {
-    my $mask=&mask(@_);
+    my $values=&get_values(@_);
     my $min='-';
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-        unless ($max) { $max=$sheet_values{$_}; }
-        if (($sheet_values{$_}<$min) || ($min eq '-')) { 
-            $min=$sheet_values{$_}; 
+    foreach (@$values) {
+        if (($_<$min) || ($min eq '-')) { 
+            $min=$_; 
         }
     }
     return $min;   
@@ -591,12 +590,8 @@
 #-------------------------------------------------------
 sub SUMMAX {
     my ($num,$lower,$upper)=@_;
-    my $mask=&mask($lower,$upper);
-    my @inside=();
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-	push (@inside,$sheet_values{$_});
-    }
-    @inside=sort {$a <=> $b} (@inside);
+    my $values=&get_values($lower,$upper);
+    my @inside=sort {$a <=> $b} (@$values);
     my $sum=0; my $i;
     for ($i=$#inside;(($i>$#inside-$num) && ($i>=0));$i--) { 
         $sum+=$inside[$i];
@@ -618,12 +613,8 @@
 #-------------------------------------------------------
 sub SUMMIN {
     my ($num,$lower,$upper)=@_;
-    my $mask=&mask($lower,$upper);
-    my @inside=();
-    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
-	$inside[$#inside+1]=$sheet_values{$_};
-    }
-    @inside=sort {$a <=> $b} (@inside);
+    my $values=&get_values($lower,$upper);
+    my @inside=sort {$a <=> $b} (@$values);
     my $sum=0; my $i;
     for ($i=0;(($i<$num) && ($i<=$#inside));$i++) { 
         $sum+=$inside[$i];
@@ -679,6 +670,31 @@
     return $max;
 }
 
+sub get_values {
+    my ($lower,$upper)=@_;
+    my $mask=&mask(@_);
+    my @values;
+    foreach (grep eval("/$mask/"),keys(%sheet_values)) {
+	push(@values,$sheet_values{$_});
+    }
+    return \@values;
+    if (0) {
+	# perhaps creating a list of possible cells and looking if they exist
+        # would be faster somtimes?
+	&logthis("mask is ".$mask);
+	my @alpha;
+	if (($la eq '*') || ($ua eq '*')) {
+	    @alpha=('A'..'z');
+	} else {
+	    if ($la gt $ua) {
+		my $tmp = $ua;
+		$ua = $la;
+		$la = $ua;
+	    }
+	    $alpha=($la..$ua);
+	}
+    }
+}
 
 sub calc {
     %sheet_values = %t;