Quotes in Perl5, Arithmetic Operations and Variables #2

in blurtdevelopment •  2 years ago 

What Will I Learn?

  • Single and Double Quota
  • Arithmetic Operations
  • Character Strings
  • Numerical Variables

  • Requirements
  • Terminal or SSH
  • Linux/Unix Operating System or Linux Hosting

  • Difficulty
  • Intermediate
  • perl.png

    Single and Double Quota
    In the previous post, I gave the character strings "a" and 'a' as an example of character strings. Yes, these are character strings, but the single and double quotes used in Perl are different. Perl parses the character strings between double quotes against possible substitution and interpretation operations, and makes these placements.

    E.G.:

    "End of line \none?"
    'End of line \none?'
    

    The sequences are quite different;
    print "End of line \none?="; command to screen

    End of line
    one?
    

    print 'End of line \none?' if

    End of line \none?
    

    If you look carefully, the difference is that the double-quoted string \n is interpreted as a new line of the symbol; if it is a single-quoted string, the \n "" symbol is interpreted as "" and "n" characters. You'll see more clearly what this feature does in the future.

    Strings enclosed in double quotes are scanned by the Perl interpreter when any operations on them need to be performed, and it is checked whether or not any docking is done. Applied if placement occurs. For example, if the $a variable in the program is the "Blurt" character array;
    print "Hello" $a\n";

    assume that there is an idiom. Before the "Hello $a \n" sequence is displayed on the screen, the Perl interpreter finds $a characters in that directory, decides it needs to be placed, and places the value "Blurt" instead of $a. It will also treat the "\n" meta character as a command that specifies carriage return. So the sequence displayed on the screen will be "Hello Blurt" followed by a carriage return.

    If you use the same print command with single quotes instead of double quotes;

    print 'Hello Blurt $a\n';
    

    the line between the single quotes of the Perl interpreter will not interfere in any way, and the characters 'Hello $a \n' will be displayed exactly as shown and no carriage return will be made.

    A careful reader will respond immediately to a question that might come to mind:
    When you need to use the \ , ' and " characters in a double-quoted string, it is enough to mark these characters with a ** character.

    E.G.:

    "Sedat\'s quote"
    "Sedat\'s quotes"
    "You can use \\n for the new line"
    

    Arithmetic Operations
    There is no need to count arithmetic operations that are meaningful for numbers, but one has to go through particularly uncomplicated;

    10 / 5 = 2
    10 / 3 = 3.333333...
    

    In other words, there is no concept of integer arithmetic in Perl. If your program requires integer arithmetic, you can use the int() function. (int (10/3 -> 3).

    As for other arithmetic operations; I guess a few examples will suffice.

    ghgh.png

    The operations used for logical comparisons of numeric values are the same as for C language, ie:

    bnbn.png

    Operations for Character Strings
    Just as in every programming language, Perl has a variety of operations and functions to combine, shred, compare, and search for other arrays in a sequence. For example;

    To conclude character strings, press "." (dot) operator is used.

    "Hello"."Blurt!"              ->               "Hello Blurt!"
    "Hello"." "."Blurt"."!"     ->               "Hello Blurt!"
    

    The x operator is used to duplicate and repeat character strings.

    $b = "a" x 5; if
    $b - > "aaaaa" ok.
    

    When logical comparison of values of character strings is needed, different operations are used in arithmetic comparison operations. At first glance, this seemed unnecessarily different, but the option to compare variables sometimes with their numerical values and sometimes with character-string values provides a great deal of flexibility in programming.

    Numerical Variables
    The scalar variable names that can be used in Perl have to start with the $ character; More precisely, when Perl programmers use a scalar variable, they must declare that this variable is "scalar" by placing a $ sign at the beginning of the variable name. The dummy rules that must be observed when selecting variable names are the same as the magical C programming rules.

    E.G.:

    $blurt                                # A suitable and well-chosen scalar variable name
    $row_count                         # This is fine, too.
    $tutorials_1                        # This is...
    $tutorials-1                        # "$tutorials minus 1" Did you mean? It's invalid as it will cause a mix like this.
    $x101                                    # A badly chosen variable name.
    x101                                       # Too bad, moreover, an invalid variable name would have been valid at $, but it would still be a bad variable.
    $_k_number                       # If you are enjoying keyboarding, yes it is. lol
    

    In general, when students talk about Perl variable names, the students immediately ask: "How long can variable names be?" I said "they can be too long!" I will reply. It looks like you need a variable name of 255 characters too...
    As in all programming languages, the most common process used in Perl is assignment processing.
    All of the assignments below are suitable for the rule, and we recommend you examine it carefully.

    # You can use # to add comments to your code.
    # Everything from the "#" sign to the end of the line "explanation"
    # it is considered.
    #
    
    $a = 1;                                                                                               # Perl statements are always ";" .
    $a = $a + ";
    $a += ";                                                                                             # As in C means; $a = $a + 1
    $a *= 2;                                                                                             # $a = $a * 2;
    $a = $b = 3;                                                                                    # Both $a and $b are assigned 3 values.
    $a = ($b = 2) * 2;                                                                         # First we set $ b to 2 and then we use $ to calculate $a as 4.
    ++$a                                                                                                 # Another way to say $a = $a + 1;
    $a++;                                                                                                # Another way to say$a = $a + 1;
    $a = 2;
    $b = ++$a;                                                                                     # it becomes $a=3; $b=3 after this assignment is made.
    $a = 2;
    $b = $a++;                                                                                     # it becomes $a=3; $b=2 after this assignment is made.
    $name = "Sedat";    
    $name = $name . " ";                                                               # It could also be written as $name .= " ";
    $name = $name . "Yildiz";                                               # $name = "Sedat Yildiz" it was
    $lastname = "Yildiz";
    $name_lastname = "Sedat $lastname";                      # Beware of this statement! (double quotes)
                                                                                                # $name_lastname = "Sedat Yildiz" would.
    $name_lastname = 'Sedat $lastname';                        # Beware of this statement! (single quotes)
                                                                                                # $name_lastname = 'Sedat $lastname' would.
    

    As I mentioned earlier, Perl looks for a placement or interpretation that needs to be done for the character strings enclosed in double quotes. It does not examine the character strings enclosed in single quotes.

    $name_lastname = "Sedat $lastname"; "$lastname" will be encountered when the character string on the right side of the equation is examined before the assignment of "$lastname" is made and the value of the variable "$lastname" will be placed in the array before the assignment.

    If this assignment statement $name_lastname = 'Sedat $lastname'; Perl will not attempt to examine the inside of the directory, and will simply pass the $name_lastname variable to whatever is in single quotes.

    These two examples are; I think you'll use character strings in our program to emphasize that you have to be very careful when choosing fingernails. In order to make the code you read easy to read and clean, try to stay away from this type of quotation marks and your code:

    $name_lastname = 'Sedat ' . $lastname;
    $name_lastname = "Sedat " . $lastname;
    

    The second part of our series ends here, in the next lesson we will touch on the Undef Values, Basic Input and Output commands and Printing our Program on the Screen. Thanks for reading, good day.

    Authors get paid when people like you upvote their post.
    If you enjoyed what you read here, create your account today and start earning FREE BLURT!
    Sort Order:  

    Hi, @sedatyildiz,

    Thank you for your contribution to the Blurt ecosystem.


    Please consider voting for the witness @symbionts.
    Or delegate to @ecosynthesizer to earn a portion of the curation rewards!