changeset 270:f203d193963d misc

Cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 08 Mar 2015 08:45:44 +0200
parents 08e5bfa19978
children e5c0c407b019
files materials/parsealloys.pl
diffstat 1 files changed, 86 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/materials/parsealloys.pl	Sun Mar 08 08:45:04 2015 +0200
+++ b/materials/parsealloys.pl	Sun Mar 08 08:45:44 2015 +0200
@@ -17,23 +17,34 @@
 my $domark = 0;
 my $mode = shift or die("Usage: $0 <-php|-text>\n");
 
-if ($mode eq "-php") {
+if ($mode eq "-php")
+{
   $domark = 1;
 }
-elsif ($mode eq "-text") {
-} else {
+elsif ($mode eq "-text")
+{
+}
+else
+{
   die("$0: Invalid operation mode '$mode'.\n");
 }
 
-## Parse data from input, ignoring fumbled alloys
+
+###
+### Parse data from input, ignoring fumbled alloys
+###
 my %data = ();
 my $fumble = 0;
 my $nline = 0;
 
-sub addCombo($$$) {
-  if (!$fumble) {
+sub addCombo($$$)
+{
+  if (!$fumble)
+  {
     push(@{$data{$_[1]}{$_[2]}}, $_[0]);
-  } else {
+  }
+  else
+  {
     print STDERR "Ignoring fumbled combo $_[0] + $_[1] = $_[2]\n";
   }
   $fumble = 0;
@@ -41,22 +52,32 @@
 
 while (<STDIN>) {
   chomp;
-  if (/^You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of (.+?)\.?$/) {
+  if (/^You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of (.+?)\.?$/)
+  {
     addCombo($3, $1, $2);
-  } elsif (/^([a-zA-Z0-9]+)#([a-z ]+)\+([a-z ]+)=([a-z ]+)$/) {
+  }
+  elsif (/^([a-zA-Z0-9]+)#([a-z ]+)\+([a-z ]+)=([a-z ]+)$/)
+  {
     addCombo($4, $2, $3);
-  } elsif (/^(You slip up and fumble your attempt.)/) {
+  }
+  elsif (/^(You slip up and fumble your attempt)/)
+  {
     $fumble = 1;
   }
 }
 
 
-## Re-format the data into single "ordered" hash, using as
-## close to alphabetical order as possible.
+###
+### Re-format the data into single "ordered" hash, using as
+### close to alphabetical order as possible.
+###
 my %final = ();
-foreach my $min1 (keys %data) {
-  foreach my $min2 (keys %{$data{$min1}}) {
-    if (!defined($final{$min1}{$min2}) && !defined($final{$min2}{$min1})) {
+foreach my $min1 (keys %data)
+{
+  foreach my $min2 (keys %{$data{$min1}})
+  {
+    if (!defined($final{$min1}{$min2}) && !defined($final{$min2}{$min1}))
+    {
       # Get both (a, b) and (b, a) combinations results into same array
       my @tmp = ();
       if (defined($data{$min1}{$min2})) {
@@ -69,14 +90,20 @@
       # Determine if there are multiple results and which is the most likely.
       # Some alloy combinations are inherently "unstable".
       my %seen = ();
-      foreach my $mat (@tmp) { $seen{$mat}++; }
-      if ($domark) {
+      $seen{$_}++ foreach (@tmp);
+      if ($domark)
+      {
         $final{$min1}{$min2} = $final{$min2}{$min1} = \%seen;
-      } else {
+      }
+      else
+      {
 #        print STDERR "$min1 : $min2 == "; foreach my $a (keys %seen) { print STDERR "$a=$seen{$a}, "; } print STDERR "\n";
-        if ($min1 lt $min2) {
+        if ($min1 lt $min2)
+        {
           $final{$min1}{$min2} = \%seen;
-        } else {
+        }
+        else
+        {
           $final{$min2}{$min1} = \%seen;
         }
       }
@@ -87,16 +114,19 @@
 
 
 ## Output the results
-if ($mode eq "-php") {
+if ($mode eq "-php")
+{
   print "<?\n".
   "\$alloyTable = array(\n";
   
-  foreach my $min1 (sort { $a cmp $b } keys %$Materials::matTable) {
+  foreach my $min1 (sort { $a cmp $b } keys %$Materials::matTable)
+  {
     print "  \"$min1\" => ";
-    
-    if (scalar(keys %{$final{$min1}}) > 0) {
+    if (scalar(keys %{$final{$min1}}) > 0)
+    {
       print "array(";
-      foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) {
+      foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}})
+      {
         print "\"$min2\" => array(";
         foreach my $res (sort { $final{$min1}{$min2}{$a} <=> $final{$min1}{$min2}{$b} } keys %{$final{$min1}{$min2}}) {
           print "\"$res\" => ".$final{$min1}{$min2}{$res}.", ";
@@ -104,7 +134,9 @@
         print "), ";
       }
       print "),\n";
-    } else {
+    }
+    else
+    {
       print "null,\n";
     }
   }
@@ -113,16 +145,23 @@
   
   # Create 'reverse' alloy table
   my %restab = ();
-  foreach my $res (keys %$Materials::matTable) {
-    foreach my $min1 (keys %final) {
-      foreach my $min2 (keys %{$final{$min1}}) {
+  foreach my $res (keys %$Materials::matTable)
+  {
+    foreach my $min1 (keys %final)
+    {
+      foreach my $min2 (keys %{$final{$min1}})
+      {
         my $tmp = $final{$min1}{$min2};
-        my $multi = scalar(keys %$tmp) > 1;
-        if ($multi) {
+        if (scalar(keys %$tmp) > 1)
+        {
 #          print STDERR "Ignoring multi $min1 + $min2 = ".join(", ", keys %$tmp)."\n";
-        } else {
-          foreach my $key (keys %$tmp) {
-            if ($key eq $res && !defined($restab{$res}{$min1}{$min2}) && !defined($restab{$res}{$min2}{$min1})) {
+        }
+        else
+        {
+          foreach my $key (keys %$tmp)
+          {
+            if ($key eq $res && !defined($restab{$res}{$min1}{$min2}) && !defined($restab{$res}{$min2}{$min1}))
+            {
               $restab{$res}{$min1}{$min2} = $$tmp{$key};
             }
           }
@@ -132,12 +171,16 @@
   }
 
   print "\$alloyRevTable = array(\n";
-  foreach my $res (sort { $a cmp $b } keys %restab) {
+  foreach my $res (sort { $a cmp $b } keys %restab)
+  {
     print "  \"$res\" => array(\n";
-    foreach my $min1 (sort { $a cmp $b } keys %{$restab{$res}}) {
-      if (scalar(keys %{$restab{$res}{$min1}}) > 0) {
+    foreach my $min1 (sort { $a cmp $b } keys %{$restab{$res}})
+    {
+      if (scalar(keys %{$restab{$res}{$min1}}) > 0)
+      {
         print "    \"$min1\" => array(";
-        foreach my $min2 (sort { $a cmp $b } keys %{$restab{$res}{$min1}}) {
+        foreach my $min2 (sort { $a cmp $b } keys %{$restab{$res}{$min1}})
+        {
           print "\"$min2\" => ".$restab{$res}{$min1}{$min2}.",";
         }
         print "),\n";
@@ -148,11 +191,14 @@
   
   print ");\n\n".
   "?>";
-} elsif ($mode eq "-text") {
+}
+elsif ($mode eq "-text")
+{
   my $total = 0;
   my %tdata = ();
 
-  foreach my $type (sort { $a cmp $b } keys %{$Materials::matByType}) {
+  foreach my $type (sort { $a cmp $b } keys %{$Materials::matByType})
+  {
     my $subtotal = 0;
     print "\n$type alloys\n".
     "=========================================================\n";
@@ -178,7 +224,5 @@
     print "\n$subtotal $type alloys.\n\n";
   }
 
-
-  
   print "\n$total alloys total.\n";
 }