Ummm… this thing on?

It appears so. Bit of technical difficulty at our end, what with the schooling and the traveling and the squids and such.

We now rejoin your regularly scheduled DogmawBlog, already in progress…

August 25, 2010 · Shopmonkey Chris · 2 Comments
Posted in: Uncategorized

Perl multiplication table with nested for loops

Print out a multiplication table from 0 to 9.


for ($count1 = 0; $count1 <= 9; $count1++)
{
for ($count2 = 0; $count2 <= 9; $count2++)
{
$num=$count1*$count2;   #multiply the numbers together
print “$num “;
if ($num<10)
{
print ” “; #print an extra space if needed so the table lines up
}
}
print “\n”; # go to the next line of the table
}

July 24, 2010 · johoff · One Comment
Tags: , ,  Â· Posted in: Code

Perl – display file contents

Write a program to read in a filename from STDIN, then open that file and display its contents with each line preceded by the filename and a colon. For example, if fred was read in, and the file fred consisted of the three lines aaa, bbb, and ccc, you would see fred:aaa,fred:bbb, and fred:ccc.
print “Enter filename for input: “;
chomp($input_name = <STDIN>);
open (INPUT, “$input_name”) or die “Can’t Open Input File: $input_name\n”;        #open input file

@input_file = <INPUT>;  #read the input file into an array
foreach $line (@input_file) #go through it line by line
{
print “$input_name: $line”;   # print file_name: line_from_file
}

close (input_file);  # close the input file

July 23, 2010 · johoff · No Comments
Tags: , ,  Â· Posted in: Code

Perl – string replacement in a file

Write a program that prompts for an input filename, an output filename, a search pattern, and a replacement string, and replaces all occurrences of the search pattern with the replacement string while copying the input file to the output file.
print “Enter filename for input: “;
chomp($input_name = <STDIN>);
print “Enter filename for output: “;
chomp($output_name = <STDIN>);

open (OUTPUT, “>>$output_name”) or die “Can’t Open Output File: $output_name\n”;
#open the output file
open (INPUT, “$input_name”) or die “Can’t Open Input File: $input_name\n”;        #open input file

print “Enter word you are looking for: “;
chomp($word1 = <STDIN>);
print “Enter what you want to replace it with: “;
chomp($word2 = <STDIN>);

@input_file = <INPUT>;  #read the input file into an array
foreach $line (@input_file) #go through it line by line
{
$line =~ s/$word1/$word2/gi; #do a replacement in the line of word2 for word1
print OUTPUT $line;   # put the replaced line in the output file
}

close (input_file);  # close the input file
close (output_file);  # close the output file

July 22, 2010 · johoff · No Comments
Tags: , , ,  Â· Posted in: Code

Perl – word count in a file

Write a program that reads a series of words (with one word per line) until end-of-input, then print a summary of how many times each word was seen. (Hint: remember that when an undefined value is used as if it were a number, Perl automatically converts it to 0. If the input words were apple, pear, grape, plum, apple, pear, and apple (all on separate lines), the output should tell us that apple was seen 3 times.

print “This will count the number of instances of a word \n”;
print “Enter one word per line. \n”;
print “When you are done, hit cntrl-D TWICE \n”;

my %count=();
while (read(STDIN, $_, 4095) and $_.=<STDIN>)
{
tr/A-Za-z/ /cs;
++$count{$_} foreach split(‘ ‘, lc $_);
}
my @lines=();
my ($w, $c);
print “Here is the list of words with the number of times it appears: \n”;
push(@lines, sprintf(“%7d\t%s\n”, $c, $w)) while (($w, $c) = each(%count));
print sort { $b cmp $a } @lines;

July 21, 2010 · johoff · One Comment
Tags: , ,  Â· Posted in: Code

Perl – a simple Hash Table

Store your important phone numbers in a hash table.  Write a program to look up numbers by the person’s name.
%phoneNumbers = ();     #set up the hash table empty
%phoneNumbers = (“Anne”, “432566″, “Paul”, “231275″, “Marie”,
“299302″);  # put phone numbers in the hash table
print “Enter the name you wish to look for: “;  #get the name you want to look for
chomp($name=<STDIN>);
$found=0;
while (($key, $value) = each %phoneNumbers)  # look through the hash table
{
if ($key eq $name)    # if the name matches one in the table, print it out along with
# the phone number
{
$found=1;
print “$key has the phone number $phoneNumbers{$key}\n”;
}
}
if ($found==0) { print “$name was not found in the list”;}     # if the name wasn’t found, say so

July 20, 2010 · johoff · No Comments
Tags: , ,  Â· Posted in: Code

Perl – array

Write a program that asks a user for five groceries, and then store them (using one of the stack operations. (Either push or unshift.)) in an array called @grocerylist and prints them out in alphabetical order.

print “Input your first grocery item: “;    # user prompt
chomp($item1 = <STDIN>);      # inputs the word and gets rid of the newline
print “Input your second grocery item: “;    # user prompt
chomp($item2 = <STDIN>);      # inputs the word and gets rid of the newline
print “Input your third grocery item: “;    # user prompt
chomp($item3 = <STDIN>);      # inputs the word and gets rid of the newline
print “Input your fourth grocery item: “;    # user prompt
chomp($item4 = <STDIN>);      # inputs the word and gets rid of the newline
print “Input your last grocery item: “;    # user prompt
chomp($item5 = <STDIN>);      # inputs the word and gets rid of the newline

push(@grocerylist, $item1, $item2, $item3, $item4, $item5);  #put the items in the array

@alphagrocery = sort(@grocerylist); #sort the array in alphabetical order
print “What’s on the list in alphabetical order:\n”;
foreach (@alphagrocery)
{
print $_ . “\n”; # print the sorted groceries
}

July 17, 2010 · johoff · No Comments
Tags: , , ,  Â· Posted in: Code

Perl – another while loop

Write a program that prints all the numbers from 1 to 100. Your program should have much fewer than 100 lines of code! – there are a couple of ways to do this.
$count = 1;
while ($count<=100)
{
print “$count “;
$count++;
}

July 16, 2010 · johoff · No Comments
Tags: ,  Â· Posted in: Code

Perl basic WHILE loop

Write a while() loop that counts from 0 to 15 by threes and prints out the number that it is on.

$count = 0;
while ($count<=15)
{
print “$count “;
$count += 3;
}

July 16, 2010 · johoff · No Comments
Tags: ,  Â· Posted in: Code

Perl basic FOR loop

Write a for() loop that counts from 0 to 15 by twos and prints out the number that its on.
for ($count = 0; $count <= 15; $count+=2)
{
print “$count “;
}

July 15, 2010 · johoff · One Comment
Posted in: Code