#! /usr/bin/perl # These are examples of numeric values $num1 = 78937294; $num2 = 3424780; print ("The sum of $num1 and $num2 is ", $num1+$num2, "\n"); print ("The difference of $num1 and $num2 is ", $num1-$num2, "\n\n"); # These are examples of perl strings $name = "Debra Sue Bartlett"; $address = "272 Computer Science, Fort Collins, CO, 80523-1873"; $phone = "970-481-4191"; $sample_string = "a_aphabet_z"; #These are examples of string manipulations # Print a string print ("My name is $name \n"); # Select a subset of a string # first parameter is the string name, second value is number of characters to skip, third value is number of characters to keep $sample_substring = substr($name, 6, 3); print ("The value of my sample substring is $sample_substring \n"); # Split a string into separate pieces # first parameter is the character that separates each piece of the string, second parameter is the string to be divided @name_elements = split(/ /, $name); print ("My first name is $name_elements[0], my middle name is $name_elements[1], my last name is $name_elements[2] \n"); @address_elements = split(/,/, $address); print ("My building address is $address_elements[0], my city is $address_elements[1], my state is $address_elements[2], my zip code is $address_elements[3] \n"); # Search for a pattern in a string if ($phone =~ /970/) {print ("I have a local phone number \n\n")}; if ($phone !~ /970/) {print ("I do not have a local phone number \n\n")}; # Search for a character at the beginning or end of a string if ($sample_string =~ /z$/) {print ("I found z at the end of the string, $sample_string \n")}; if ($sample_string =~ /^a/) {print ("I found a at the beginning of the string, $sample_string \n\n")}; # Search for word and replace with a different word $my_name = "Debra Sue Bartlett"; print ("My formal name is $my_name\n"); $my_name =~ s/Debra/Debbie/; print ("My preferred name is $my_name\n");