06-29-2010, 01:21 PM
Perl #3 : Strings and Numbers
Perl only distinguishes between two types of data, those are scalar data and list data. Scalar data, is for single things like numbers or strings of characters. List data is for collective things like arrays. In this tutorial we will only talk of scalar data, ignore list data for now...
Here is a list of valid numbers in Perl.
1
2.1
.2134123
2.
1_122_231
10E2
45e-4
0xbeef
022
Integers and floating point numbers are both represented as you think they would be. The integers represented by whole numbers, then floating with integer then decimal part. Comma's arent allowed so you use _ as you can see in 1_122_231 The Underscores represent comma's
Exponents you use a lower or upper letter case e
Hexadecimal and octal numbers are represented by 0x or 0
Strings
A string a collection of letters. They are a form of Scalar data in Perl.
To represent a string you either use ' ' or " "
'String'
"String"
"Hello World!\n"
Ok now back onto strings, strings can use any kind of ASCII data, that includes things like binary!
Ok here would be a good place to explain the whole double quotes/ single quotes thing. Ok when using double quotes ("") you are allowed to use variables and other non "plain text". You also can use normal typing but double quotes allows you to call on a Variable like "My name is $name "
While single quotes ('') You cant call on things like variables. You cant use things like \n (New line) (Those are called Escape characters), you only can type "Hello my name is austin"
LINE--------------------------------OUTPUT
"Hello $ownername"------------------------Hello Wade
'Hello $ownername'------------------------Hello $ownername
[color="#FF8C00"]
Some Escape characters[/color]
\n New Line
\r Carriage Return
\f Formfeed
\t Tab
\a Bell
\b Backspace
\e Escape
\u Makes next letter Uppercase
\l Makes next letter Lowercase
\U Makes all following letters Uppercase
\L Makes all following letters Lowercase
\E Ends \U, \L, or \Q expressions
Empty Strings, is a string with no characters. Empty strings look like this "". Its not an empty string if it has a space in it like " ". The space is what no longer makes it an empty string.
Here is a bit on variables!
Variables must have $ at the beginning
Set a variable with the assignment operator which is the equals sign ( = )
So like $a = 1 would set the variable as 1
Then there is $a = $b = 1 would set $a to 1 and $b to 1
Thats all for now