• Publicidad

if y else (error)

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

if y else (error)

Notapor Mono_Ezpacial » 2012-10-06 03:44 @197

Saludos. Bueno, como verán estaba programando e hice un script así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!usr/bin/perl
  2. $animals = "animals";
  3. $help    = "help";
  4. print "~>";
  5. chomp( $usr = <> );
  6. if ( $usr eq $animals ) {
  7.     print "here animals!\n";
  8. }
  9. if ( $usr eq $help ) {
  10.     print "Here the help\n";
  11. }
  12. else {
  13.     print "$usr:command not found\n";
  14. }
  15.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Todo me funciona bien, pero me falla una cosa: al escribir "animals" pasa esto:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
~ $ perl script.pl

~>animals
here animals!
animals:command not found
 
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


Como ven se ejecuta bien, pero sale abajo "animals:command not found", algo que no debería pasar porque es correcto lo que escribió el usuario. Por lo contrario con "help" sí funciona a la perfección.

Agradecería la ayuda de ustedes. ¡¡Gracias!! :D
Wake up, Neo... The Matrix has you... Follow the white rabbit. Knock, Knock, Neo.
Mono_Ezpacial
Perlero nuevo
Perlero nuevo
 
Mensajes: 17
Registrado: 2012-09-23 18:09 @798

Publicidad

Re: if y else (error)

Notapor explorer » 2012-10-06 12:42 @571

Cuando el usuario escribe "animals", se activa el if() de la línea 6, y por ello se ejecuta la línea 7... y el programa continúa: llega a la línea 9 donde, efectivamente, $usr no es igual a 'help', por lo que entonces se dispara la parte del else{}.

Hay varias soluciones:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. if ( $usr eq $animals ) {
  2.     print "here animals!\n";
  3. }
  4. elsif ( $usr eq $help ) {
  5.     print "Here the help\n";
  6. }
  7. else {
  8.     print "$usr:command not found\n";
  9. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use feature 'switch';  # a partir de Perl v5.10
  2. given ($usr) {
  3.     when ($animals) {
  4.         print "here animals!\n";
  5.     }
  6.     when ($help) {
  7.         print "Here the help\n";
  8.     }
  9.     default {
  10.         print "$usr:command not found\n";
  11.     }
  12. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: if y else (error)

Notapor Mono_Ezpacial » 2012-10-06 18:34 @815

Gracias brother, sí me funcionó. Esta web es de lo mejor, la recomendaré ¡Saludos! :D :D
Wake up, Neo... The Matrix has you... Follow the white rabbit. Knock, Knock, Neo.
Mono_Ezpacial
Perlero nuevo
Perlero nuevo
 
Mensajes: 17
Registrado: 2012-09-23 18:09 @798


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 41 invitados

cron