Sie sind auf Seite 1von 1

Types of data

Literals/invariants/constants the data which does not change with time. In Perl, literals are
either numbers or strings.

Numbers may be integers, decimals, hexadecimals or octals. No commas can be used in
integers, as we do in general. For example, 3,45,67,321. Decimals can be used with or
without 0 as in 0.6 or .6. Exponents (base 10) can also be speci_ed by appending the letter
.e and the exponent to the real number portion. e.g. 2e3 is equivalent to 2 x 103 = 2000.

Strings are sequences of characters enclosed by either double quotes or single quotes. In
the first program you did, Welcome to Computational Linguistics is a string literal, enclosed
in double quotes.

A variable is a named entity representing a piece of data of which the content can be
modified throughout its lifetime. That is, the content may not be the same throughout.
Unlike other programming languages, Perl gives you the flexibility that at one time
you may store a number and at other times you may store a string in the same variable,
however, it is a good practice to always store data of a particular type at any time in the
same variable to avoid confusion. By default, all variables are global in Perl. That is, once a
variable is used, it can be accessed anytime and anywhere in the program. That is why, it is
better to limit the scope of variables. As pointed in the last class, a variable in Perl begins
with the symbol $ and limit the scope by using my before the variable. For example,
my $total_marks

We do not need to declare a variable in Perl before you use it. However, you should give it a
default value. Assignment of value is done by using the assignment operator =. For
example,
my $total_marks = 380

Other mathematical operators are: addition +, subtraction -, multiplication * and division /.

Task 1

A student named Joseph obtained the following marks in various subjects in his class 10.
Write a program marks_percent.pl, which calculates the total and percentage; and displays
name, class, the marks in individual subjects, total marks and percentage. Assume that the
marks obtained in individual subject is out of 100.
English : 78 Hindi : 81
Mathematics : 90 Science : 72
Social Studies : 74

Task 2
Modify the program given task1 in such a way that it accepts name, class and marks from
the user and displays the name, class, marks in individual subjects, total marks and percent.
(You need to use the function <STDIN> which accepts input from the user. Use it as follows:
my $english = <STDIN> ). Name this program as marks_percent1.pl.

Task3
Given basic salary of a person, the gross salary can be calculated by adding 50% of the
basic as DA, 30% of the basic as HRA and 5% as Travel Allowance. The net salary can be
calculated by deducting 20% as Income Tax, 2% as Professional Tax and 10% for PF
contribution. Write a program which accepts name and the basic salary from the user and
displays the complete salary certificate (name, basic salary, DA, HRA, TA, Gross salary,
Income Tax, Professional Tax, PF contribution and Net Salary).

Das könnte Ihnen auch gefallen