next up previous contents index practicapracticaPP2moodleLHPmoodlepserratacpanmodulospauseperlgoogleetsiiullpcgull
Sig: Copia y sustitución simultáneas Sup: Expresiones Regulares Ant: Expresiones Regulares Err: Si hallas una errata ...


Un ejemplo sencillo

Reescriba el siguiente ejemplo para que sea una función de conversión de temperaturas. Utilize la estrategia del hash para tener parámetros con nombres.

   1 #!/usr/bin/perl -w
   2 print "Enter a temperature (i.e. 32F, 100C):\n";
   3 $input = <STDIN>;
   4 chop($input);
   5 
   6 if ($input !~ m/^([-+]?[0-9]+(\.[0-9]*)?)\s*([CF])$/i) {
   7   print "Expecting a temperature, so don't understand \"$input\".\n";
   8 }
   9 else {
  10   $InputNum = $1;
  11   $type = $3;
  12   if ($type eq "C" or $type eq "c") {
  13     $celsius = $InputNum;
  14     $fahrenheit = ($celsius * 9/5)+32;
  15   }
  16   else {
  17     $fahrenheit = $InputNum;
  18     $celsius = ($fahrenheit -32)*5/9;
  19   }
  20   printf "%.2f C = %.2f F\n", $celsius, $fahrenheit;
  21 }
  22


next up previous contents index practicapracticaPP2moodleLHPmoodlepserratacpanmodulospauseperlgoogleetsiiullpcgull
Sig: Copia y sustitución simultáneas Sup: Expresiones Regulares Ant: Expresiones Regulares Err: Si hallas una errata ...
Casiano Rodríguez León
2006-02-21