Introduction to Perl

Introduction to Perl

Lesson Four


#Script Week 4
#!usr/bin/perl

system "cls"; # use "cls" for Windows/DOS
# system "clear"; # use for Linux/UNIX

@rats = ();
@cats = ();
@pets = ();
# initial arrays declared
@rats = (Wolverine, Storm, Shadowcat, Phoenix, Chance);
@cats = (Leo, Stasia);
@pets = (finches, quail, rats, cats, mouse, rabbit, frogs);
# initial arrays defined
@current = (@rats, @cats, Lizzie);
$petCount = @current;
@drats = qw(Sammie Cubby Danny Marty Charlie Lantash Candy Janet Frankie Maggie Emly Tabitha Jinny Artemis Jack Kerry Jed Benny Susan Anastacia Cassie Bratac Catherine Tiny Darya Fawn Jolynne Kryn Monkey Davie Carol Sappho Tamia Dana CJ Abby Zoelle Simon AL Silver Becky BoySam Dazzler Lucky Gil Xavier);
@dfinches = qw(Pikachu Togepi Artemis Terpsichore Hermes Persephone Odin Freya Apollo);
@dbettas = qw(Bobsyeruncle Gabbie Emmett);
@dquail = qw(Gates Allen);
@dmice = qw(Frack Abby Pai Frick Grace);
$dcats = "Toby";
@dead = (@drats, @dfinches, @dquail, @dbettas, @dmice, $dcats);
$poem = "no need to go out the door";
@poem = (1, 2, 3, 4, $dcats, @cats, $poem);
$licky = (Sappho, Jolynne, Shadowcat);
@count = (1..5); # same as (1,2,3,4,5)
($r1, $r2, $r3, @first) = (Art, Sammie, Janet, Cassie, Tamia, Emly, Fawn, Danny, Cubby, Marty);
$title = "My Pets";
$author = "by aj";

print "$title\n\t$author\n\n";
print "Currently, I have $petCount pets, which consist of 2 cats, 5 rats, and 1 mouse.\n\n";
print "Their names are @current.\n\n";
print "My partner and I have had many pets over the 5.5 years we\'ve lived together, including @pets.\n\n";
print "Over the years, we\'ve lost several pets.  Their names are as follows\n@dead\n\n";
print "Our cats are all indoor cats who are afraid of going outside. Here\'s a poem I wrote about that.\n";
print "@poem\n\n";
print "Some of our rats have been very licky. Currently $licky is the lickiest.\n\n";
print "As the Count on Sesame Street would say, @count, 5 rats! Ahahahahah!\n\n";
print "When we got our rats, it started with $r1, $r2, and $r3, who were soon joined by @first.\n\n";

@pcats = (KJ, Midnight, Anni, Bella, Ripper, Sarafina, Leo, Toby, Harley, Stasia);
# change the first element from KJ to Midnight
$allcats[0] = Midnight;
# the second element is now set to KJ
$allcats[1] = KJ;
# prints "allcats: 1, 2";
print "allcats: $allcats[0], $allcats[1]\n";
# sets $number to 3
$next = $allcats[2] + $allcats[3];
# gets the last index of @allcats (9)
$lastIndex = $#allcats;
#swap the elements
($allcats[3], $allcats[4]) = ($allcats[4], $allcats[3]);
print "$lastIndex\n\n";

# first, initialize the array
@mymice = ();
# push one element to the array
push(@mymice, "Frick, ");
# now push three elements at once!
push(@mymice, "Frack, ", "Lizzie, ", "Abby, ");
# use qw() to push an element to the array
push(@mymice, qw(Grace, ));
# now push two elements at once!
push(@mymice, "Pai, ", "and no more");
pop(@mymice);

print "I have had 6 mice, including @mymice.\n\n";

# add "dog" to the start of the array
unshift (@mymice, "dog");
# remove the first element (dog) from the start of the array
shift(@mymice);
# Or remove the first element (dog) and store it in a variable
$firstValue = shift(@mymice);

@future = ();                   
@futureReverse = ();                 
@future = (donkey,gsd,snake,fish);
@futureReverse = reverse(@future);
@sort = sort(@future);
@sortReverse = reverse(@sort);

print "@future\n";
print "@sort\n";
print "@futureReverse\n";
print "@sortReverse\n\n";

print "@mymice\n";
chomp(@mymice);