Perl Conti.....
From DrugPedia: A Wikipedia for Drug discovery
Line 86: | Line 86: | ||
print $coins[0]; #Prints the first element | print $coins[0]; #Prints the first element | ||
<br />'''Note:''' Elements can also be indexed backwards using negative integers instead of positive numbers. | <br />'''Note:''' Elements can also be indexed backwards using negative integers instead of positive numbers. | ||
- | + | <br />print $coins[-1]; #Prints the last element<br/> | |
- | print $coins[-2]; #Prints 2nd to last element | + | print $coins[-2]; #Prints 2nd to last element<br /> |
+ | <br />'''PERL - The qw Subroutine''' | ||
+ | |||
+ | Quotations can be a hassle, especially if the array you wish to build has more than 5 elements. Use this neat little subroutine to remove the need for quotes around each element when you define an array.<br /> | ||
+ | <nowiki># DEFINE AN ARRAY WITHOUT QUOTES</nowiki><br /> | ||
+ | @coins = qw(Quarter Dime Nickel);<br /><br /> | ||
+ | '''PERL - Sequential Number Arrays''' | ||
+ | |||
+ | PERL offers a shortcut for sequential numbers and letters. Rather than typing out each element when counting to 100 for example, we can do something like this:<br /> | ||
+ | @10 = (1 .. 10);<br /> | ||
+ | @abc = (a .. z);<br /> | ||
+ | |||
+ | '''PERL - Finding the length of an Array''' | ||
+ | |||
+ | Retrieving a numerical value that represents the length of an array is a two step process. First, you need to set the array to a scalar variable, then just print the new variable to the browser as shown below. | ||
+ | |||
+ | There are two ways to set an array to scalar mode. We can use the scalar() function or we can redefine the array as a scalar variable. | ||
+ | <br />@nums = (1 .. 20);<br /> | ||
+ | print scalar(@nums); |
Revision as of 07:26, 8 September 2008
Arithmetic Operators:
Arithmetic operators are symbols used to execute general arithmetic procedures including: addition (+), subtraction (-), multiplication (*), and division (/).
7 + 7 = 14 Addition
7 - 7 = 0 Subtraction
7 * 7 = 49 Multiplication
7 / 7 = 1 Division
7 ** 7 = 823543 Exponents
7 % 7 = 0 Modulus
PERL - Assignment Operators
Addition ($x += 10)
Subtraction ($x -= 10)
Multiplication ($x *= 10)
Division ($x /= 10)
Modulus ($x %= 10)
Exponent ($x **= 10)
PERL - Logical & Relational Operators
Relationship operators compare one variable to another. (5 < 12) They are used to compare equality or inequality of two or more variables, be it a string or numeric data.
Logical operators state and/or relationships. Meaning, you can take two variables and test an either or conditional. Logical operators are used later on in conditionals and loops. For now, just be able to recognize them in the upcoming examples.
Relational
Operator | Example | Defined | Result |
==,eq | 5 == 5 5 eq 5 | Test: Is 5 equal to 5? | True |
!=,ne | 7 != 2 7 ne 2 | Test: Is 7 not equal to 2? | True |
<,lt | 7 < 4 7 lt 4 | Test: Is 7 less than 4? | False |
>,gt | 7 > 4 7 gt 4 | Test: Is 7 greater than 4? | True |
<=,le | 7 <= 11 7 le 11 | Test: Is 7 less than or equal to 11? | True |
>=,ge | 7 >= 11 7 ge 11 | Test: Is 7 greater than or equal to 11? | False |
Logical
Operator | Defined | Example |
&&,and | Associates two variables using AND | if (($x && $y) == 5)... |
||,or | Associates two variables using OR | if (($x || $y) == 5)... |
Please note that you must use each different operator depending of whether or not you are comparing strings or numbers. In the table above, the black operators are for numbers and the red ones are for strings.
PERL - Variables + Operators
Variables can be used with mathematical formulas using PERL Operators discussed in a previous lesson. Also, note that variables are case sensitive. "$myvariable," "$MYvariable," and "$Myvariable" can all be assigned different values due to case sensitivity. Numbers of course can be added, subtracted, or multiplied using operators. Strings as shown in the example below can also be used with operators.
#TWO STRINGS TO BE ADDED
$string1 = "Hello,";
$string2 = " World";
#ADD TWO STRINGS TOGETHER<br /. $string3 = "$myvariable + $Myvariable";
print $string3;
Output: Hello, + World
PERL - Array Variables
Arrays are complex variables that store list style data types. Each object of the list is termed an element and elements can either be a string, a number, or any type of scalar data including another variable. Place an array into a PERL script, using the at symbol (@).
# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");
print "@coins";
Output: Quarter Dime Nickel
PERL - Array Indexing
Each element of the array can be indexed using a scalar version of the same array. When an array is defined, PERL automatically numbers each element in the array beginning with zero. This phenomenon is termed array indexing.
# PRINT THE WHOLE ARRAY
print "@coins";
print $coins[0]; #Prints the first element
Note: Elements can also be indexed backwards using negative integers instead of positive numbers.
print $coins[-1]; #Prints the last element
print $coins[-2]; #Prints 2nd to last element
PERL - The qw Subroutine
Quotations can be a hassle, especially if the array you wish to build has more than 5 elements. Use this neat little subroutine to remove the need for quotes around each element when you define an array.
# DEFINE AN ARRAY WITHOUT QUOTES
@coins = qw(Quarter Dime Nickel);
PERL - Sequential Number Arrays
PERL offers a shortcut for sequential numbers and letters. Rather than typing out each element when counting to 100 for example, we can do something like this:
@10 = (1 .. 10);
@abc = (a .. z);
PERL - Finding the length of an Array
Retrieving a numerical value that represents the length of an array is a two step process. First, you need to set the array to a scalar variable, then just print the new variable to the browser as shown below.
There are two ways to set an array to scalar mode. We can use the scalar() function or we can redefine the array as a scalar variable.
@nums = (1 .. 20);
print scalar(@nums);