$_="chim chimery chim chimery chim chim chery";Carefully circle the portion of the string matched by the following expressions (in other words, indicate the result of printing
$&
) :
/chim.*chim/;
chim chimery chim chimery chim chim chery
/chim.*?chim/;
chim chimery chim chimery chim chim chery
$_
and that $_
has been chomped, write the regular expression to correctly validate
an account holder.
In my opinion, the the solution is to use Perl.We wish to catch this as a possible typo by reusing a previously matched pattern in the regex. Note that any valid word boundary is allowed before the first
the
and after the second
the
, but we expect only one or more blank spaces in between them.
Assuming the current line being processed is stored in $_
, write
the regular expression that will return true when double words are
encountered.
$_
):
s/([A-Z][a-z]+) ([A-Z][a-z]+)/$2 $1/;
$_ eq "Betty Boop"
before the above substitution is
applied. What is stored in $_
afterwards?
$_ eq "Leonardo DaVinci"
before the above substitution is
applied. What is stored in $_
afterwards?
$&
, the pattern after the integer
should be enclosed in a lookahead anchor.
For example, given "-20 F\n"
, we wish to match "-20"
. Given
"-20\n"
, we would match nothing. Given "-20 Fh\n"
, we would
also match nothing.
Assuming the data is stored
in $_
and is unchomped, write the regular expression to do
this.
<first-name> <last-name> <team> <carries> <yards><newline>For example,
Jerome Bettis PIT 323 1460Write a Perl script that first opens this file for reading, and a file called avgs.dat for writing (any existing data in the file should be overwritten). If either operation fails, the script should exit with an appropriate error message. Each line read from backs.dat should be separated so that all five fields are stored in separate variables. For each input line, a line of the following form should be output to avgs.dat:
<first-name> <last-name> <avg-yds-per-carry><newline>Note that
<avg-yds-per-carry>
is computed as $yards/$carries
.
Continuing our above example, we would output
Jerome Bettis 4.52012383900929After backs.dat has been processed in its entirety, both files should be closed. If a close fails, an error message should be produced and the script should exit.
@numbers = (3, 2.1, 5); plus_plus(@numbers); # @numbers is now (4, 3.1, 6)You may assume that the list passed in consists of only numbers. The routine does not have to work for lists of literals.
@numbers = (-3, -4.5, 6, 2.2); @abs_vals = absolute(@numbers); # @abs_vals is (3, 4.5, 6, 2.2) @abs_vals = absolute(4, -5.6, -11); # @abs_vals is (4, 5.6, 11)
Recall that the absolute value of a positive number x is simply x
and the absolute value of a negative number x is -x. Do not make
use of the Perl built-in function abs
. You will need to create a
private array to store the list of absolute values. The list passed
in should NOT be modified (you may assume it contains only numbers).
For each key in the hash referenced by the first argument, the
corresponding value should be compared with the scalar search value.
If the value equals (eq
) the scalar search value passed in to
the function, it should be replaced with the replacement scalar value,
that is, the value corresponding to the current key being processed
should be overwritten with the replacement value. Every such value
matched should be replaced. None of the keys should be modified. The
function should return nothing. For example,
# the curly brace defines a reference to an anonymous hash $hash_ref = { "Van Halen" => "David Lee Roth", "Twisted Sister" => "Dee Schnider", "U2" => "Bono", "the News" => "Huey Lewis" }; search_and_replace($hash_ref, "David Lee Roth", "Sammy Hagar"); # this subroutine call replaces every occurrence of the value # "David Lee Roth" with "Sammy Hagar" in the hash referenced by $hash_ref
%lead_singers = ( "Van Halen" => "David Lee Roth", "Twisted Sister" => "Dee Schnider", "U2" => "Bono", "the News" => "Huey Lewis" );
Given the subroutine that you defined above, call
search_and_replace so that the value "Bono"
is replaced with
"the Edge"
in the hash %lead_singers
.
use strict; use CGI qw(:standard); # emit initial HTML tags print header(), start_html("Language Preference"); # print a <H1> header in HTML print h1("Language Preference"); if(param()) { # process form } else { # generate form } # emit closing HTML tags print end_html();
Fill in the blanks in the above code to write a CGI script that both generates and processes a simple form.
hr()
) and start the form. An HTML paragraph p()
should be printed. Within this paragraph should be the string
"Enter name: "
and a textfield()
. The name associated with the
textfield()
should be "user_name"
. A
scrolling_list()
should be emitted within another HTML
paragraph. The scrolling_list()
should be associated with the
name "language"
and acceptable values returned by the widget
should be the strings "C"
, "C++"
, "Java"
,
"Lisp"
, and "Perl"
. All other parameters to
scrolling_list()
may be omitted and default values used. Print
one more HTML paragraph. Within it include an option to
submit()
(associated with the name "engage"
) and an
option to reset()
(associated with the name "clear"
).
Finally, end the form and print another horizontal rule.
"user_name"
and
"language"
entered by the user should be queried using the
appropriate CGI.pm
function and stored in $name
and
$language
respectively. Print a horizontal rule, and within
an HTML paragraph include a string which indicates that
"The favorite language of $name is $language."To finish, print another horizontal rule.