Главная страница


ru.perl

 
 - RU.PERL ----------------------------------------------------------------------
 From : Artem Chuprina                       2:5020/371.32  13 May 2000  18:37:49
 To : Serge Cherpatyuk
 Subject : Re: studing the example
 -------------------------------------------------------------------------------- 
 
 <Serge_Cherpatyuk@p39.f44.n4625.z2.fidonet.org> wrote:
 
 SC> Доброе время суток!
 
 SC> Узрел в одном скрипте следующую конструкцию
 
 SC> $b = $a eq "somestring"?"vol1":"vol2";
 
 SC> разобравшись с ней, понял, что это запись в одну строку
 SC> выражения:
 
 SC> if ($a eq "somestring") {
 SC>     $b = "vol1";
 SC> } else {
 SC>     $b = "vol2";
 SC> }
 
 SC> Красиво, но где ж такому научиться? Имея книгу "Изучаем Perl"
 SC> Рэндала Л. Шварца и Тома Кристиансена, я такого примера не
 SC> увидел. Там, так скромненько, в главе "приоритет и ассоциативность
 SC> операций" сказано - ?: (операция выбора, if-then-else), а как ею
 SC> пользоваться - догадайся сам.
 
 А. бъясняю.
 
 Из вышепроцитированного догадайся сам, что ?: - оператор. Далее догадайся сам,
 если man perl не помогает, что описание операторов содержится в perldoc
 perlop. Догадавшись сам поискать там сочетание символов ?:, получаешь
 
 ==== 8< [!perldoc perlop] ====
        Ternary "?:" is the conditional operator, just as in C.
        It works much like an if-then-else.  If the argument
        before the ? is true, the argument before the : is
        returned, otherwise the argument after the : is returned.
        For example:
 
            printf "I have %d dog%s.\n", $n,
                    ($n == 1) ? '' : "s";
 
        Scalar or list context propagates downward into the 2nd or
        3rd argument, whichever is selected.
 
            $a = $ok ? $b : $c;  # get a scalar
            @a = $ok ? @b : @c;  # get an array
            $a = $ok ? @b : @c;  # oops, that's just a count!
 
        The operator may be assigned to if both the 2nd and 3rd
        arguments are legal lvalues (meaning that you can assign
        to them):
 
            ($a_or_b ? $a : $b) = $c;
 
        This is not necessarily guaranteed to contribute to the
        readability of your program.
 
 ==== >8 [!perldoc perlop] ====
 
 Hа каком этапе рассуждений возникли проблемы?
 
 -- 
 Счастливо!
   Ран.
 --- ifmail v.2.14.os-p7-tma3
  * Origin: MemoNet (2:5020/371.32@fidonet)
 
 

Вернуться к списку тем, сортированных по: возрастание даты  уменьшение даты  тема  автор 

 Тема:    Автор:    Дата:  
 Re: studing the example   Artem Chuprina   13 May 2000 18:37:49 
Архивное /ru.perl/3473b18dd454.html, оценка 1 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional