• 🏠 Home
  • 🧰 Online Generators
  • 📰 Blog
  • 🔎 Search
  • 📧 Contact
  • 📘 Guestbook
  • 🗞️ News
  • 💰 Support
  • ℹ️ Info: The website has been modernized to improve readability and smartphone compatibility. Feel free to report any bugs using the contact form
     

    Sorry, but this page is not yet available in english.

    Clic here to send a translate request


    Programmation Perl - Simuler l'instruction switch


    Perl n'inclut pas, par défaut d'instruction switch. Il est cependant très facile de simuler cette instruction.
    Pour cela, il faut utiliser une simple boucle au format suivant:

    Le code

          SWITCH: {
      
            if ( $p_nb == 2 )
            {
              my ($filesystem,$capacity,$used,$available,$percent,$mount) = split(/ +/,$ligne);
              print "$filesystme;$capacity;$used;$available;$percent;$mount\n";
              last SWITCH;
            }
      
            if ( $p_nb == 7 )
            {
              my($user,$nblines,$alert,$max,$pctocc) = split (/#/,$ligne);
              print "$user;$nblines;$alert;$max;$pctocc\n";
              last SWITCH;
            }
            
            my($valeur1;$valeur2;$valeur3) = split (/#/,$ligne);
            print "$valeur1;$valeur2;$valeur3\n";
      
          }
      
      

    Explications

    • Si la variable $p_nb vaut 2, on va exéter les instructions du premier if.
    • Si la variable $p_nb vaut 7, on va exéter les instructions du second if.
    • Dans tous les autres cas, on exécutera les instructions en fin de boucle.

     

    Retour


    (c) 2005-2025 Jérôme DESMOULINS
    Data privacy