Name:
Full E-mail Address:
UNIX System Used:
1. Extract elements 2 to 4 from the array:
@array = (1,2,3,4,5);
and assign them to the scalars $a, $b and $c making use of:
(a) index access to arrays ( for example: $arrray[2] )
(b) sublist access with @array[...], and
(c) The splice function
2. Create a two-dimensional array of 3 x 3 elements, whose entries are the following
strings:
"0:0" "0:1" "0:2"
"1:0" "1:1" "1:2"
"2:0" "2:1" "2:2"
Make use of two nested for loops, in which you increment two indices $i and $j
3. Output the values of the array: @array = (1, 2, 3) as a string in the format
"(1-2-3)". Assemble the string step by step using a for loop, making sure that
no hyphen appears after the last value. Subsequently, solve the problem by means
of a join instruction.
4. Demonstrate the possibility of passing scalars and an array to a function by value.
For this, the array must be the last parameter. Write a Perl function, func that upon
the call
$a = 1;
$b = 2;
@c = (3, 4, 5);
copies the passed parameters into local variables and verbatim outputs:
$a=1 $b=2 @c=(3,4,5)
5. Determine in which context the following constructs call the function testcontext():
@array = testcontext();
$scalar = testcontext();
$scalar = somefunc(testcontext());
@array = (1,2,testcontext());
testcontext();
if(testcontext()) { }
foreach $i (testcontext()) { }
@array = map { textcontext(); }; ("element");
@array = grep { textcontext(); }; ("element");
Verify your suppositions by copying the above function testcontext together with the
constructs into a file, executing the Perl script, and analyzing the output.
6. Write a function called paramhash, which uses a parameter sequence like:
('-name' => 'Williams', '-first_name' => 'Robin', ... ) to initialize a hash
with the key-value pairs 'name' => 'Williams','first_name' => ..., and return
a reference to it so that,
testme(-name => "Williams", -first_name => "Robin", -weight => 195);
#-------------------------------------------------------------------
sub testme()
#-------------------------------------------------------------------
my $p = paramhash(@_); # to be written !!!
print "$p->{first-name} $p->{name} weighs $p->{weight} kg\n"
#-------------------------------------------------------------------
generates the output: Robin Williams weighs 195 Kg
7. Let's assume a hash %hash with the following values:
%hash = ('Key' => 'Value',
'LongKey => 'LongValue',
'K' => 'V');
Output the key-value pairs in two columns formatted in the following way:
K V
Key Value
LongKey Longvalue
In a first loop, determine the length of the longest key in the hash and use
this length to output the hash columnwise in a second loop. The key column
shall be of constant length, with the current key fitted left-aligned.
8. Write a function called get_formatted_date, which returns
the current date and time in the format: "03/01/1999 14:02:01". All
fields are of constant length. Use the localtime function to fetch
the parameters of the current time and format them with sprintf.
To find out more about the parameters of localtime(), simply call
perldoc -f localtime
9. Implement a script pwrite.pl, that outputs the elements
of an array (e.g. ("abc","def", "ghi") line by line with line breaks.
A second script pread.pl should call pwrite.pl, open it for
reading, receive the lines one by one, and output for each line of pwrite.pl
"pwrite.pl said: 'Line contents (of pwrite.pl)'"
Questions about the questions? Robert Katz: katz@ned.highline.edu
Last Update March 31, 2003