[USflag] The American Programmer [USflag]
Home Programming Books for Computer Professionals Privacy Terms
           Home   > Programming   > Perl    > Practice Problems with Perl
           Home   > Manuals   > Perl Manuals   > Practice Problems with Perl

Practice Problems with Perl

Practice problem A1. How big?

Practice with looping.

Start with a salary of two dollars.

do a for loop that loops 12 times.

inside the loop,

double salary.

display the doubled salary

watch what happens to salary

 

 

Practice problem A2. How small?

Looping.

Start with a salary of two cents.

do a for loop that loops 10 times.

inside the loop,

double salary.

display the doubled salary

check if salary is equal to zero

if so, get out of the loop

after the loop display the loop counter

so you'll see how many times it looped.

 

Practice problem A3. Donít repeat.

Looping.

Create a loop that lets you type in a series of numbers.

You may not type in the same one twice in a row.

if you do, display message 'sorry, repeated a number'.

 

 

 

Practice problem A4. Lottery numbers.

Practice with arrays, subroutines, sorting an array and loops.

Display 6 random lottery numbers. The numbers are from 1 - 40.

Display them in sorted order. No duplicates allowed.

If you would like help with the logic, read the following, otherwise create your own.

suggested logic: (with some actual perl)

set initial values

set the variable $have_6_unique to a "no"

set the variable $i to a 0; # zero out index

set the variable $rand_is_new to a "no";

$x = srand();#this starts the random function with a seed

repeat this until you have 6 unique numbers

call subroutine "get_one_unique"

end of the repeat

 

sort the array @number

call subroutine "showem"

exit;

subroutine "get_one_unique"

{

call subroutine "getrand"

set $rand_is_new variable to "no"

call subroutine "is_rand_new"

if $rand_is_new is equal to "yes"

{

add 1 to $i

if $rand is less than 10, stick a "0" on the front of $rand

#in other words, if $rand is 9, make it 09

$number[$i] = $rand # this is the perl to set an element

# of the array to the new number

} # end of the if

if $i is equal to 6 set the variable $have_6_unique to a "yes"

} # end of subroutine get one unique

 

subroutine "is rand new"

{

set the variable $rand_is_new to a "yes"

#here do a for loop

for ($i1 = 1; $i1 < 7; $i1++)

{

if ($number[$i1] == $rand) {$rand_is_new = "no";}

} # end for

 

} # end subroutine is rand new

 

 

subroutine "showem"

{

here you display the array @number

} # end subroutine showem

 

 

subroutine "getrand"

{

$rand = int(rand(39)) + 1;

} # end subroutine getrand

Practice problem A5. Percent calculator.

Illustrates math, terminal dialogue,

subroutines, Case structure.

The general formula is

X is y% of Z, for example,

10 is 5% of 200

The idea is that you know only two values. The program calculates the missing one.

This is the logic of the program, but in English, not Perl.

Your job is to write it into Perl.

display "X IS Y% OF Z"

display "ENTER TWO OF THE VALUES FOR X, Y AND Z"

display "ENTER ? FOR THE UNKNOWN VALUE"

display "ENTER VALUE FOR X, OR ?"

read the value of X

display "ENTER VALUE FOR Y, OR ?"

read the value of Y

display "ENTER VALUE FOR Z, OR ?"

read the value of Z

case structure

WHEN X is equal to "?" execute subroutine FIND_X

WHEN Y is equal to "?" execute subroutine FIND_Y

WHEN Z is equal to "?" execute subroutine FIND_Z

OTHERWISE display "INPUT ERROR, REDO"

end of the case structure

EXIT

subroutine FIND_X:

X = (Z * Y) / 100

display X" IS "Y"% OF "Z

end of subroutine

subroutine FIND_Y:

Y = (X / Z) * 100

display X" IS "Y"% OF "Z

end of subroutine

subroutine FIND_Z:

Z = (X * 100) / Y

display X" IS "Y"% OF "Z

end of subroutine

 

Practice problem A6. Read a file..

There is a file named readme.txt

it contains these records:

ED HOHOKUS

MO KOKOMO

LU KALAMAZOO

SU OAHU

AL NUTLEY

DI WEEHAWKEN

JO QUASAPAUG

Open the file.

With one Perl instruction read the file into an array named @record

Close the file.

with one print instruction, display the whole array.

 

Practice problem A7. Read a file.and display elements.

Same as the previous, but with a foreach loop

In the loop display each element of the array,

along with the words 'Element number' and the index.

See Example 55,

It will produce results like this:

Element number 0 ED HOHOKUS

Element number 1 MO KOKOMO

etc.

 

 

Practice problem A8. Read a file and break record up into variables.

Building on the previous problem.

there is a file named readme.txt

it contains:

ED HOHOKUS

MO KOKOMO

LU KALAMAZOO

SU OAHU

AL NUTLEY

DI WEEHAWKEN

JO QUASAPAUG

Open the file.

With one Perl instruction read the file into an array named @record

Close the file.

in a foreach loop

using the substr function,

break each element of @record into $key and $data

$key is the first 2 characters

$data is the rest

display $key and $data

 

 

Practice problem A9. Read a file and break record up into variables, set a hash.

Build on the previous.

Inside the loop. Just after you display $key and $data.

Set a hash using $key as the key and $data as the value.

Like this: $hash{$key} = $data;

Put this instruction outside of the loop,

after the end of the loop, to see if you loaded the hash ok.

print "Here is city for mr Ed: $hash{'ED'}\n";

 

 

 

Practice problem A10. The phone directory.

Building on the previous.

Add this after the line: print "Here is city for mr Ed: $hash{'ED'}\n";

Start another loop. (Dialogue loop. see example 17)

This loop repeatedly asks for a name from the keyboard.

(end the loop when just ENTER is pressed)

if the name is in the hash, display it, (exists, example 60)

otherwise display "NOT FOUND"

If you keep getting NOT FOUND:

Consider two things.

Did you type in the name in upper case?

Please type it in using upper case.

Did you chomp the name to get rid of the new line (\n)? (example 16)

Gotta do both things.

 

Practice problem A11

Same as the previous, but change the name typed in

to upper case. This allows you to type it in using lower case?

 

Suggested Solutions for Additional Problems

Solution Practice problem A1.

$salary = "2";

for ($count = 0; $count < 12 ; $count++)

{

$salary = $salary * $salary;

print "salary is $salary \n";

}

print "$count \n";

print "end \n";

Solution Practice problem A2.

$salary = ".02";

for ($count = 0; $count < 10 ; $count++)

{

$salary = $salary * $salary;

print "salary is $salary \n";

if ($salary eq "0") {last;}

}

print "$count \n";

print "end \n";

Solution Practice problem A3.

#never the same number twice in a row

$previous_number = "";

$number = "";

print "please type in a number (not 2 in a row) (ENTER to stop)";

while ($reply = <STDIN>)

{

if ($reply eq "\n") {last};

print "your line was $reply\n";

if ($reply eq $previous_number)

{print "sorry, repeated a number \n";}

else

{$previous_number = $reply;}

print "please type in a number (ENTER to stop)";

}

print "end \n";

Solution Practice problem A4. Lottery numbers.

# lottery number program. creates and displays

# 6 random lottery numbers from 1 - 40

$have_6_unique = "no";

$i = 0; # zero out index

$rand_is_new = "no";

#@number = ();

$x = srand();

until ($have_6_unique eq "yes")

{

&get_one_unique;

#print "unique 6 sw $have_6_unique \n";

#$x = <>; # to pause, to debug

}

&sortem;

&showem;

exit;

 

sub get_one_unique

{

&getrand;

$rand_is_new = "no";

&is_rand_new;

if ($rand_is_new eq "yes")

{

$i++;

#print "value of i is $i \n";

if ($rand < 10) {$rand = "0" . $rand;}

$number[$i] = $rand;

 

if ($i == 6) {$have_6_unique = "yes";}

}

} # end sub get_one_unique

 

 

sub is_rand_new

{

$rand_is_new = "yes";

for ($i1 = 1; $i1 < 7; $i1++)

{

if ($number[$i1] == $rand) {$rand_is_new = "no";}

} # end for

} # end sub is_rand_new

 

sub sortem

{

@number = sort(@number)

}

 

sub showem

{

print "@number \n";

}

print "\n";

 

sub getrand

{

$rand = int(rand(39)) + 1;

#print "got rand $rand \n";

}

Solution Practice problem A5. Percent calculator.

print "X IS Y% OF Z";

print "ENTER TWO OF THE VALUES FOR X, Y AND Z\n";" ;

print "ENTER ? FOR THE UNKNOWN VALUE\n";" ;

print "ENTER VALUE FOR X, OR ? ";

$x = <STDIN>;

print "ENTER VALUE FOR Y, OR ? ";

$y = <STDIN>;

print "ENTER VALUE FOR Z, OR ? ";

$z = <STDIN>;

if ($x eq "?") {&find_x;}

elsif ($y eq "?") {&find_y;}

elsif ($z eq "?") {&find_z;}

else {print "input error, redo\n";";}

exit;

sub FIND_X:

{

$x = ($z * $y) / 100

print "$x IS $y% OF $z \n";

}

sub FIND_Y:

{

Y = (X / Z) * 100

print "$x IS $y% OF $z \n";

}

sub FIND_Z:

{

Z = (X * 100) / Y

print "$x IS $y% OF $z \n";

}

Solution Practice problem A6. Read a file..

open(INF,"readme.txt") or die("Couldn't open data file for reading: $! \n");

@record = <INF>;# reading whole file at once

close(INF);

print "@record \n";

Solution Practice problem A7. Read a file.and display elements.

open(INF,"readme.txt") or die("Couldn't open data file for reading: $! \n");

@record = <INF>;# reading whole file at once

close(INF);

$how_many_in_array = @record; #will contain a 3

for ($i = 0; $i < $how_many_in_array; $i++)

{

print "Element number $i $record[$i]\n";

}

Solution Practice problem A8.

open(INF,"readme.txt") or die("Couldn't open data file for reading: $! \n");

@record = <INF>;# reading whole file at once

close(INF);

$how_many_in_array = @record; #will contain a 3

for ($i = 0; $i < $how_many_in_array; $i++)

{

print "Element number $i $record[$i]\n";

$key = substr($record[$i],0,2);

$data = substr($record[$i],2);

print "key $key \n";

print "data $data \n";

}

Solution Practice problem A9.

open(INF,"readme.txt") or die("Couldn't open data file for reading: $! \n");

@record = <INF>;# reading whole file at once

close(INF);

$how_many_in_array = @record; #will contain a 3

for ($i = 0; $i < $how_many_in_array; $i++)

{

print "Element number $i $record[$i]\n";

$key = substr($record[$i],0,2);

$data = substr($record[$i],2);

print "key $key \n";

print "data $data \n";

$hash{$key} = $data;

}

print "Here is city for mr Ed: $hash{'ED'}\n";

Solution Practice problem A10

open(INF,"readme.txt") or die("Couldn't open data file for reading: $! \n");

@record = <INF>;# reading whole file at once

close(INF);

$how_many_in_array = @record; #will contain a 3

for ($i = 0; $i < $how_many_in_array; $i++)

{

print "Element number $i $record[$i]\n";

$key = substr($record[$i],0,2);

$data = substr($record[$i],2);

print "key $key \n";

print "data $data \n";

$hash{$key} = $data;

}

print "Here is city for mr Ed: $hash{'ED'}\n";

print "please type in a name (ENTER to stop)";

while ($who = <STDIN>)

{

if ($who eq "\n") {last};

chomp $who;

if (exists($hash{$who})) {print "$hash{$who} \n";}

else {print "NOT FOUND \n";}

print "please type in a name (ENTER to stop)";

}# end while

 

Solution Practice problem A11

Add this line after chomp $who;

$who = uc($who);

[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |