#!usr/bin/perl -w use strict; ##### Defining array of numbers my@numbers = (1,2,3); print "Array of numbers: @numbers\n\n"; ##### Defining array of strings my@animals = ("cat", "dog", "bird"); print "Array of strings: @animals\n\n"; ##### quoted words is a shorthand way to create an array of strings without using quotes @animals = qw(cat dog bird); print "Without quotes: @animals\n\n"; ##### Print the first element of the array animals ##### Elements of arrays are indexed starting with the value 0 print "First element of array \@animals: $animals[0]\n\n"; ##### $# in front of the name of an array returns the last index value of an array ##### This value is one less than number of elements in the array since the first index value is 0 print "Last index value: $#animals\n\n"; ##### scalar returns the number of elements in an array print "Number of elements: "; print scalar(@animals), "\n\n"; ##### Negative values reference elements in the array starting from the last position print "Last element of array: $animals[-1]\n\n"; ##### Create array of numbers incrementing by 1 @numbers = (1..5); print "Counting up by 1: @numbers\n\n"; ##### Reverse the order of an array ##### One way to generate an array of decreasing numbers my@num_rev = reverse(@numbers); print "Reversed array: @num_rev\n\n"; ##### Pop removes the last element from an array ##### This value can be assigned to a different variable if you want to keep it my$var1 = pop(@numbers); print "Popped variable: $var1\n\n"; print "Remaining values in array: @numbers\n\n"; ##### Push adds a new element onto the end of an array push(@numbers, 8); print "Array after push: @numbers\n\n"; ##### Shift removes the first element from an array ##### Here it is not being stored shift(@numbers); print "Array after shifting: @numbers\n\n"; ##### Unshift adds a new element to the front of an array unshift(@numbers, 10); print "Array after unshift: @numbers\n\n"; ##### Sorting values in array ##### Use $a <=> $b to compare values ##### Use $a cmp $b for strings my@sorted = sort {$a <=> $b} (@numbers); print "Sorted array: @sorted\n\n"; ##### Sorting the array numbers by ASCII value ##### Since all numbers in the array are not two digit, 10 is sorted before 2 since 1 is less than 2 ##### For 2 to be sorted before 10 using this format, 2 would have to be written as 02 my@sorted2 = sort(@numbers); print "Sorted by ASCII: @sorted2\n\n"; ##### Creating string of characters my$char = "alksjdflkaskjlfd"; print "Random string: "; print $char, "\n\n"; ##### Creating array of characters from a string by splitting between each character my@chars = split(//,$char); print "After split: @chars\n\n"; ##### Splitting a string by the character 'a' ##### This removes 'a' and generates an array ##### A string can be split by any character or regular expression you want such as a comma or a tab ##### In this 'a' is the first character in the string so a null value is generated at index position 0 my@chars2 = split(/a/,$char); print "Split by 'a': @chars2\n\n"; ##### The foreach loop steps through a list of values executing the function in the loop for each value ##### This loop will print each element of the array chars2 ##### Here the variable $var130 is used as a control variable. It takes on a new value for each iteration foreach my $var130 (@chars2) { print "HELLO >>$var130<<\n"; } print "\n"; print "Max Index:", $#chars2, "\tNumber of Elements:", scalar(@chars2), "\n"; ##### FOR LOOP #1 ##### This for loop performs the same function as the foreach loop used previously - printing each element of the array chars2 ##### The for loop differs because it requires 3 components: ##### 1) my $count=0 initializes the expression ##### 2) $count < scalar(@chars2) is the test expression. As long as this expression is true, the code will continue to be executed ##### 3) $count++ resets the initial expression each time the loop is executed ##### $count is being used to define the index value of the @chars2 array for (my $count=0; $count < scalar(@chars2); $count++) { print "HI1 :$count>", $chars2[$count], "<:\n"; } print "\n"; ##### FOR LOOP #2 ##### In this for loop, the initial position is the last index position of the chars2 array and the loop counts down to the first index position for (my $count=$#chars2; $count >= 0 ; $count--) { print "Hello :$count>", $chars2[$count], "<:\n"; } print "\n"; ##### FOR LOOP #3 ##### This for loop does the same thing as FOR LOOP #1. However, the test expression is defined by the last index position rather than the number of elements in the array. for (my $count=0; $count <= $#chars2; $count++) { print "HI2 :$count>", $chars2[$count], "<:\n"; } print "\n"; ##### The join function allows you to combine all elements of an array into one string print "After join: "; print join(",", @chars2), "\n\n"; ##### Defining two dimensional array my @Dim2 = ([1,2,3], [4,5,6], [7,8,9]); ##### Using a foreach loop to go through each row of the 2D array and print out the row foreach my $row (@Dim2) { print "BAD CONSTRUCTION: $row\n"; #Equivalent methods to print the row print "PERL ARRAY PRINT:", "@$row\n"; print "JOIN PRINT :", join (" ", @$row), "\n"; print "JOIN TAB :", join ("\t", @$row), "\n"; foreach my $col (@$row) { print "Element = $col\t"; } print "\n\n"; }