Quick Navigation Bar introduction :: data structures :: program control and functions [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/perl/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
2**3Powerful? Yes.
"hello" . "world"The above would result in a single word, "helloworld". You can string multiple concatenations into one operation.
x
. For example:
"w00t" x 3would yield "w00tw00tw00t".
Comparison | Numeric | String |
Equal | == | eq |
Not Equal | != | ne |
Less Than | < | lt |
Greater Than | > | gt |
Less Than or Equal To | <= | le |
Greater Than or Equal To | >= | ge |
++, --, !, &&, ||, ?:, *= += -=,
->, \
. See Schwartz pgs. 38-39.(4**2 + 3 / 2) . " is the result of the expression 4^2 + 3/2"This would result in a string "17.5 is the result of the expression 4^2 + 3/2".
$result = (4**2 + 3 / 2) . " is the result of the expression 4^2 + 3/2";Now the string is stored in the variable
result
.($x, $y) = ($y, $x);swaps variables x and y. A far cry from C...
chop
or chomp
. chop
always
removes the last character from the string, while chomp
only removes newlines. So be safe and use chomp
to
remove newlines from input (more on input later).@
character.@arr_1 = (1, 2, 3); @str_arr = ("hello", "cruel", "world"); @arr_42 = qw(the meaning of life doesn\'t exist); @arr_gen = (0 .. 5);
str_arr
is an array of strings. What is arr_42
?
It is the shorthand method of writing a list of strings using the
qw
or quote word function. arr_gen
is the
same as the list (0, 1, 2, 3, 4, 5).@arr_gen = (-1, @arr_gen, 6); # adds -1 to the front, 6 to the back of list ($a, @arr_gen) = @arr_gen; # removes -1 from @arr_gen to $a $length = @arr_gen; # length is 7 ($first) = @arr_gen;These aren't the only ways to add or remove from a list (see below). What does the fourth statement do? Whenever you set an array to a scalar, Perl automatically takes the length of the array and sets that value to the scalar. The last statement gets the first element of
arr_gen
or 0. But how do we access different
elements in the array?$arr_gen[3] = 42; @arr_gen[0,1] = (10, 11);In the first line, we access the fourth element in the array and change the value to 42. In the second line, we change the first two elements in the array to 10 and 11 by slicing. You can easily reverse lists using slicing also (see Schwartz pg. 53), but there is an even easier method below for list reversals.
@arr_gen = (0 .. 5); $a = $arr_gen[-2]; # $a gets assigned 4 $last = $#arr_gen; # last == 4What does the last statement do? It gives
last
the
last index value of the array (not the length).@arr = (1 .. 3); $new_rvalue = 4; $new_lvalue = 0; push(@arr, $new_rvalue); # @arr == (1, 2, 3, 4) unshift(@arr, $new_lvalue); # @arr == (0, 1, 2, 3, 4) $x = pop(@arr); # $x == 4, @arr == (0, 1, 2, 3) $y = shift(@arr); # $y == 0, @arr == (1, 2, 3)Instant stacks and queues. Nice!
@rev_a = reverse(@a);Simple enough... how about sorting a list?
@mylist = (1, 2, 4, 0, 32, 22, 17, 53, 42); @sorted = sort(@mylist);
@sorted
is 0, 1, 17, 2, 22, 32, 4, 42, 53. Wait! That
isn't sorted... well technically it is. The sort
function
performs an ASCII sort, not a numeric sort. More on sorting numerically
later.%
. Elements in the
hash are referenced by $key
. For example:
$myhash{"key1"} = "555-111-29391-secret-key!" $myhash{"key2"} = "333-123-33904-secret-key!" $key = "key2"; print "$myhash{$key}"; # Prints the value in the hash of key2
keys
function call is to loop through each key in the hash perhaps:
foreach $key (keys (%myhash)) { print "$myhash{$key}\n"; }This would loop through the
%myhash
hash and print
each key. More on foreach
later...each
works much like keys does,
but returns a list of both the key and the hash value.delete $myhash{"key1"}; # Deletes the key key1 and it's value
@keys = qw(key3 key4 key5); @myhash{@keys} = ("665-345-09284-secret-key!", "958-544-94950-secret-key!", "448-39403-secret-key!");
@myhash{@keys}
is the same as @myhash{"key3", "key4",
"key5"} = ("665-345-09284-secret-key!", "958-544-94950-secret-key!",
"448-39403-secret-key!");
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.