Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Scripting Language

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 55

RISHI MS INSTITUTE OF ENGINEERING &TECHNOLOGY

FOR WOMEN
(Approved by AICTE, New Delhi and Affiliated to JNTUH)
Nizampet Cross Road, JNTUH Kukatpally Hyderabad–500085

LABORATORY MANUAL

SCRIPTING LANGUAGES LAB

B.Tech IIIYEAR II SEM (R22 REGULATIONS)


For
B. Tech
III Year II semester

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING,


INFORMATION TECHNOLOGY
INDEX

S.NO TOPIC PAGE NO

I List of Experiments ii

II V/M /POs/PSOs/PEOs iv

III Syllabus ix

IV Course Objectives & Course Outcomes xi

i
List of Experiments
Exp. No. Experiment Name Page No.

1 Write a Ruby script to create a new string which is n copies of a given string where n
is a non-negative integer

2 Write a Ruby script which accept the radius of a circle from the user and compute the
parameter and area.

3 Write a Ruby script which accept the users first and last name and print them in
reverse order with a space between them

4 Write a Ruby script to accept a filename from the user print the extension of that

5 Write a Ruby script to find the greatest of three numbers

6 Write a Ruby script to print odd numbers from 10 to 1

7 Write a Ruby script to check two integers and return true if one of them is 20
otherwise return their sum

8 Write a Ruby script to check two temperatures and return true if one is less than 0 and
the other is greater than 100

9 Write a Ruby script to print the elements of a given array

10 Write a Ruby program to retrieve the total marks where subject name and marks of a
student stored in a hash

11 Write a TCL script to find the factorial of a number

12 Write a TCL script that multiplies the numbers from 1 to 10

13 Write a TCL script for sorting a list using a comparison function

14 Write a TCL script to (i) create a list (ii) append elements to the list (iii) Traverse the
list (iv) Concatenate the list

15 Write a TCL script to comparing the file modified times.

16 Write a TCL script to Copy a file and translate to native format.

17 a) Write a Perl script to find the largest number among three numbers. b) Write a Perl
script to print the multiplication tables from 1-10 using subroutines.

18 Write a Perl program to implement the following list of manipulating functions

a) Shift b) Unshift c) Push

19 a) Write a Perl script to substitute a word, with another word in a string. b) Write a
Perl script to validate IP address and email address.

ii
20 Write a Perl script to print the file in reverse order using command line arguments

iii
RISHI M.S INSTITUTE OF ENGINEERING & TECHNOLOGY FOR WOMEN
(Affiliated to JNTUH University, Approved by AICTE)
Department of Computer Science And Engineering & Information Technology

Vision of the institution:

To be a center of excellence in producing women engineers and scientists who are professionally
competent social leaders to face multi-disciplinary global environment by imparting quality technical
education, values and ethics through innovation methods of teaching and learning.

Mission of the institution:

 To promote women technocrats capable enough to resolve the problems faced by the society using
the knowledge imparted.
 To prepare self-reliant women engineering for technological growth of the nation and society by
laying strong theoretical foundation accompanied by wide practical training.
 To equip the young women with creative thinking capabilities and empowering them towards
innovation.

iv
RISHI M.S INSTITUTE OF ENGINEERING & TECHNOLOGY FOR WOMEN
(Affiliated to JNTUH University, Approved by AICTE)
Department of Computer Science And Engineering & Information Technology

Department of Information Technology


Vision & Mission

Vision of the department

To empower women by providing cutting-edge technology to female technocrats in the fields of


Information Technology, allowing them to develop into competent engineers and entrepreneurs.

Mission of the department

 Adopting creative techniques to nurture and strengthen the core skill of Computer Science.
 Introduce students to the most recent technological advancements.
 Impart quality education, improve the research, entrepreneurial, and employability skills of women
technocrats.
 Instil professional ethics and a sense of social responsibility in students.
 Strengthen the Industry-Academia interface, which will enable graduates to emerge as
academic leaders or inspiring entrepreneurs

v
RISHI M.S INSTITUTE OF ENGINEERING & TECHNOLOGY FOR WOMEN
(Affiliated to JNTUH University, Approved by AICTE)
Department of Computer Science And Engineering & Information Technology

Program outcomes (POs)


1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem Analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct Investigations of Complex Problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long Learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.

vi
RISHI M.S INSTITUTE OF ENGINEERING & TECHNOLOGY FOR WOMEN
(Affiliated to JNTUH University, Approved by AICTE)
Department of Computer Science And Engineering & Information Technology

Program specific outcomes (PSOs)

PSO 1: Improve the student's ability to decipher the basic principles and methodology of computer
systems. Improve the students' ability to absorb facts and technical ideas in order to build and
develop software.
PSO 2: The capacity to create novel job routes as an entrepreneur using modern computer languages
and evolving technologies like SDLC, Python, Machine Learning, Social Networks, Cyber
Security, Mobile Apps etc.

vii
RISHI M.S INSTITUTE OF ENGINEERING & TECHNOLOGY FOR WOMEN
(Affiliated to JNTUH University, Approved by AICTE)
Department of Computer Science And Engineering & Information Technology

Program educational objectives (PEOs)

PEO-1: Engineering graduates with excellent fundamental and technical skills will have
successful careers in industry, meeting the needs of Indian and worldwide firms.

PEO-2: With determination, development, self-reliance, leadership, morality, and moral principles,
engineering graduates will become successful entrepreneurs who will leverage employability.

PEO-3: To support personal and organisational progress, engineering graduates will pursue higher
education and engage in lifelong learning.

viii
SYLLABUS
SCRIPTING LANGUAGES LAB
B.TECH III Year II Sem

Week -1:
1. Write a Ruby script to create a new string which is n copies of a given string where n is a non-
negative integer
2. Write a Ruby script which accept the radius of a circle from the user and compute the parameter and
area
3. Write a Ruby script which accept the users first and last name and print them in reverse order with a
space between them
Week - 2:
4. Write a Ruby script to accept a filename from the user print the extension of that
5. Write a Ruby script to find the greatest of three numbers
6. Write a Ruby script to print odd numbers from 10 to 1

Week - 3:
7. Write a Ruby script to check two integers and return true if one of them is 20 otherwise return their
sum
8. Write a Ruby script to check two temperatures and return true if one is less than 0 and the other is
greater than 100
9. Write a Ruby script to print the elements of a given array

Week - 4:
10. Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash
11. Write a TCL script to find the factorial of a number
12. Write a TCL script that multiplies the numbers from 1 to 10
Week - 5:
13. Write a TCL script for sorting a list using a comparison function
14. Write a TCL script to (i) create a list (ii) append elements to the list (iii) Traverse the list (iv)
Concatenate the list
15. Write a TCL script to comparing the file modified times.
Week - 6:
16. Write a TCL script to Copy a file and translate to native format.
17. a) Write a Perl script to find the largest number among three numbers. b) Write a Perl script to print the
multiplication tables from 1-10 using subroutines.
18. Write a Perl program to implement the following list of manipulating functions a) Shift b) Unshift c) Push
Week - 7:
19. a) Write a Perl script to substitute a word, with another word in a string. b) Write a Perl script to
validate IP address and email address.
20. Write a Perl script to print the file in reverse order using command line arguments

TEXT BOOKS:
1. The World of Scripting Languages, David Barron,Wiley Publications.

ix
2. Ruby Programming language by David Flanagan and Yukihiro Matsumoto O’Reilly
3. “Programming Ruby” The Pramatic Progammers guide by Dabve Thomas Second edition

REFERENCE BOOKS:

1. Open Source Web Development with LAMP using Linux Apache, MySQL, Perl and PHP, J.Lee and B. Ware
(Addison Wesley) Pearson Education.
2. Perl by Example, E. Quigley, Pearson Education.
3. Programming Perl, Larry Wall, T. Christiansen and J. Orwant, O’Reilly, SPD.
4. Tcl and the Tk Tool kit, Ousterhout, Pearson Education. 5. Perl Power, J. P. Flynt, Cengage Learning.

x
Course Objectives:
 To Understand the concepts of scripting languages for developing web based projects
 To understand the applications the of Ruby, TCL, Perl scripting languages

Course Outcomes:
 Ability to understand the differences between Scripting languages and programming languages
 Gain some fluency programming in Ruby, Perl, TCL

CO - PO MAPPING:

CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

Python CO1 3 1 2 1 2 2
Programming
Lab CO2 1 2 3 2 1 3

CO3 3 2 3 1 3 3

CO - PSO MAPPING:
PSO-1 PSO-2
CO1 1 3
CO2 2 3
CO3 2 3

xi
WEEK-1

EXPERIMENT NO: 1.
Write a Ruby script to create a new string which is n copies of a given string where n is a non-negative
integer

Aim: Write a Ruby script to create a new string which is n copies of a given string where n is a
nonnegative integer

CODE:

def multiple_string(str, n)
return str*n
end
print multiple_string('a', 1),"\n"
print multiple_string('a', 2),"\n"
print multiple_string('a', 3),"\n"
print multiple_string('a', 4),"\n"
print multiple_string('a', 5),"\n"

Output:
a
aa
aaa
aaaa
aaaaa

i
EXPERIMENT NO: 2
Write a Ruby script which accept the radius of a circle from the user and compute the parameter and
area.
Aim: Write a Ruby script which accept the radius of a circle from the user and compute the parameter
and area.

CODE:
radius = 5.0
perimeter = 0.0
area = 0.0
print "Input the radius of the circle: "
radius = gets.to_f
perimeter = 2 * 3.141592653 * radius
area = 3.141592653 * radius * radius
puts "The perimeter is #{perimeter}."
puts "The area is #{area}."

Output:
Input the radius of the circle: The perimeter is 31.41592653.
The area is 78.539816325.

ii
EXPERIMENT NO: 3
Write a Ruby script which accept the user's first and last name and print them in reverse order with a
space between them
Aim: Write a Ruby script which accept the user's first and last name and print them in reverse order
with a space between them

CODE:
puts "Input your first name: "
fname = gets.chomp
puts "Input your last name: "
lname = gets.chomp
puts "Hello #{lname} #{fname}"

Output:
Input your first name:
Input your last name:
Hello Lanoie Gary

iii
WEEK-2

EXPERIMENT NO: 4 Write a Ruby script to accept a filename from the user print the extension of
that
Aim: Write a Ruby script to accept a filename from the user print the extension of that
Code:
file = "/user/system/test.rb"
# file name
fbname = File.basename file
puts "File name: "+fbname
# basename
bname = File.basename file,".rb"
puts "Base name: "+bname
# file extention
ffextn = File.extname file
puts "Extention: "+ffextn
# path name
path_name= File.dirname file
puts "Path name: "+path_name

Output:
File name: test.rb
Base name: test
Extention: .rb
Path name: /user/system

iv
EXPERIMENT NO: 5. Write a Ruby script to find the greatest of three numbers
Aim: Write a Ruby script to find the greatest of three numbers
Code:
x,y,z = 2,5,4
if x >= y and x >= z
puts "x = #{x} is greatest."
elsif y >= z and y >= x
puts "y = #{y} is greatest."
Else
puts "z = #{z} is greatest."
end
Copy

Output:
y = 5 is greatest.

v
EXPERIMENT NO: 6. Write a Ruby script to print odd numbers from 10 to 1.
Aim: Write a Ruby script to print odd numbers from 10 to 1.
Code:
puts "Odd numbers between 9 to 1: "
9.step 1, -2 do |x|
puts "#{x}"
end

Output:
Odd numbers between 9 to 1:
9
7
5
3
1

vi
WEEK-3

EXPERIMENT NO.: 7 Write a Ruby scirpt to check two integers and return true if one of
them is 20 otherwise return their sum.
AIM: Write a Ruby scirpt to check two integers and return true if one of them is 20 otherwise
return their sum.
Ruby Code:
def makes20(x,y)
return x == 20 || y == 20 || x + y == 20
end
print makes20(20, 10),"\n"
print makes20(40, 10),"\n"
print makes20(15, 20)
Copy

Output:
true
false
true

vii
EXPERIMENT NO.: 8 Write a Ruby script to check two temperatures and return true if one is
less than 0 and the other is greater than 100
AIM: Write a Ruby script to check two temperatures and return true if one is less than 0 and the
other is greater than 100
Ruby Code:
def temp(temp1, temp2)
return ( temp1 < 0 && temp2 > 100 ) || ( temp1 > 100 && temp2 < 0 );
end
print temp(110, -1),"\n"
print temp(-1, 110),"\n"
print temp(2, 120)

Output:
true
true
false

viii
EXPERIMENT NO.: 9 Write a Ruby script to print the elements of a given array
AIM: Write a Ruby script to print the elements of a given array
Sample array : ["Ruby", 2.3, Time.now]

Ruby Code:
array1 = ["Ruby", 2.3, Time.now]
for array_element in array1
puts array_element
end
Copy

Output:
Ruby
2.3
2017-12-28 06:01:53 +0000

ix
WEEK-4

EXPERIMENT NO.: 10.


Write a Ruby program to retrieve the total marks where subject name and marks of a student
stored in a hash Sample subject and marks : Literature -74, Science – 89, Math-91
AIM: Write a Ruby program to retrieve the total marks where subject name and marks of a
student stored in a hash Sample subject and marks : Literature -74, Science – 89, Math-91
Ruby Code:
student_marks = Hash.new 0
student_marks['Literature'] = 74
student_marks['Science'] = 89
student_marks['Math'] = 91
total_marks = 0
student_marks.each {|key,value|
total_marks +=value
}
puts "Total Marks: "+total_marks.to_s
Copy

Output: Total Marks: 254

x
EXPERIMENT NO.: 11
Write a TCL script to find the factorial of a number
AIM: Write a TCL script to find the factorial of a number
Code:
set counter 4
set factorial 1
while {$counter > 0} {
set factorial [expr $factorial * $counter]
incr counter -1
}
puts $factorial
Factorial 10
=> 3628800

xi
EXPERIMENT NO.: 12. Write a TCL script that multiplies the numbers from 1 to 10
AIM: Write a TCL script that multiplies the numbers from 1 to 10

III-II B-TECH CSE (AIML)


DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html> xii

<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium xiii

PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
xiv
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
xv
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head> xvi

<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html> xvii

<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM: xvii
i
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
xix

Program</title>
</head>
<body>
<p id="output">0</p>
Code:
set n 10
for {set i 1} {$i <= $n} {incr i}
{ puts $i }

Output
1
2
3
4
5
6
7
8
9
10

xx
WEEK-5

EXPERIMENT NO.: 13.


Write a TCL script for Sorting a list using a comparison function
AIM: Write a TCL script for Sorting a list using a comparison function
PROGRAM:
The syntax for sorting a list is given below –
lsort listname
An example for sorting a list is given below –
set var {orange blue red green}
set var [lsort $var]
puts $var

output
blue green orange red

xxi
EXPERIMENT NO.: 14.
Write a TCL script to
(i)create a list
(ii )append elements to the list
(iii)Traverse the list
(iv)Concatenate the list
AIM: Write a TCL script to
(i)create a list
(ii )append elements to the list
(iii)Traverse the list
(iv)Concatenate the list

III-II B-TECH CSE (AIML)


DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium xxii

PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
xxii
i
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
xxi
v
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head> xxv

<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html> xxv
i

<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM: xxv
ii
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
III-II B-TECH CSE (AIML)
DEVOPS LAB MANUAL
EXPERIMENT NO.: 11. Write
a simple program in
JavaScript and
perform testing using Selenium
xxv
iii
AIM: Write a simple program in
JavaScript and perform testing
using
Selenium
PROGRAM:
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
<button id="incre
PROGRAM:
xxi
x
 Simple JavaScript program that
you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title>Simple JavaScript
Program</title>
</head>
<body>
<p id="output">0</p>
Code:
Creating a List
Some examples are given below –
set colorList1 {red green blue}
set colorList2 [list red green blue]
set colorList3 [split "red_green_blue" ]
puts $colorList1
puts $colorList2
puts $colorList3
result –
red green blue
red green blue
red green blue
Appending Item to a List
Some examples are given below – xxx

set var orange


append var " " "blue"
puts $var
result − orange blue

Traversing lists
For each item {1 2 3 4 5 6 7 8 9}
{ puts $item
}
result –
1
2
3
4
5
6
7
8
9
Concatenate the list
set i [concat {a b c} {1 2 3}]
puts $i
result − a b c 1 2 3
Output
1
2
3
4
5
6
7
8
9
10

xxx
i
EXPERIMENT NO.: 15.
Write a TCL script to comparing the file modified times.
AIM: Write a TCL script to comparing the file modified times.
PROGRAM:
proc newer { fp fp2 } {
if ![file exists $fp] {
puts "file exsist"
} else {
# Assume file1 exists
expr [file mtime $fp] > [file mtime $fp2]
puts "file modification times compared."
}
}
newer file1.txt file2.txt

xxx
ii
WEEK-6
EXPERIMENT NO.: 16.
Write a TCL script to Copy a file and translate to native format.
AIM: Write a TCL script to Copy a file and translate to native format.
PROGRAM:
proc File_Copy {src dest} {
if [file isdirectory $src] {
file mkdir $dest
foreach f [glob -nocomplain [file join $src *]] {
File_Copy $f [file join $dest [file tail $f]]
}
return
}
if [file isdirectory $dest] {
set dest [file join $dest [file tail $src]] }
set in [open $src]
set out [open $dest w]
puts -nonewline $out [read $in]
close $out ; close $in
}

xxx
iii
EXPERIMENT NO.: 17.
a) Write a Perl script to find the largest number among three numbers. Vi great.pl
AIM: Write a Perl script to find the largest number among three numbers. Vi great.pl
PROGRAM:
#!/usr/bin/perl
print "enter a value";
$a=; print "enter b value";
$b=; print "enter c value";
$c=; if($a > $b) //if compares string use gt ,lt,le,ge
{
if($a> $c)
{
print " $a is largest number\n";
}
else
{
print " $c is largest number\n"; }
}
elsif($b >$c)
{
print " $b is largest number";
}
Else
{
print " $c is largest nnumber";
}

xxx
iv
OUT PUT:
Perl great.pl
Enter a value 4
Enter b value 6
Enter c value 5
6 is largest number
b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Program:
for($i=1;$i<=12;$i++)
{
$a[$i]=$i;
}
for($i=1;$i<=12;$i++)
{
for($j=1;$j<=12;$j++)
{
print(($a[$j]*$a[$i])," ");
}
print "\n\n";
}

Output:
1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8
12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54
60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45
54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110
121 132 12 24 36 48 60 72 84 96 108 120 132 144

xxx
v
EXPERIMENT NO.: 18.
Write a Perl program to implement the following list of manipulating functions
a)Shift
b)Unshift
c)Push
Perl provides various inbuilt functions to add and remove the elements in an array.
Functio
N Description
Push Inserts values of the list at the end of an array
Pop Removes the last value of an array shift
Shifts all the values of an array on its left
unshift Adds the list element to the front of an array
push function This function inserts the values given in the list at an end of an array
Multiple values can be inserted separated by comma.
This function increases the size of an array. It returns number of elements in new array.

Syntax: push(Array, list)


Example:
 Perl
#!/usr/bin/perl
# Initializing the array @x = ('Java', 'C', 'C++');
# Print the Initial array print "Original array: @x \n";
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
# Printing the array print "Updated array: @x";

xxx
vi

Output: Original array: Java C C++ Updated array: Java C C++ Python Perl
pop function
This function is used to remove the last element of the array. After executing the pop function, size of
the array is decremented by one element. This function returns undef if list is empty otherwise returns
the last element of the array.
Syntax: pop(Array)
Example:
 Perl
#!/usr/bin/perl
# Initializing the array @x = ('Java', 'C', 'C++');
# Print the Initial array print "Original array: @x \n";
# Prints the value returned by pop print "Value returned by pop: ", pop(@x);
# Prints the array after pop operation print "\nUpdated array: @x";

Output:
Original array: Java C C++
Value returned by pop: C++
Updated array: Java C

xxx
vii
shift function
This function returns the first value in an array, removing it and shifting the elements of the array list to
the left by one. Shift operation removes the value like pop but is taken from the start of the array
instead of the end as in pop. This function returns undef if the array is empty otherwise returns first
element of the array.

Syntax: shift(Array)
Example:
 Perl
#!/usr/bin/perl
# Initializing the array @x = ('Java', 'C', 'C++');
# Print the Initial array print "Original array: @x \n";
# Prints the value returned
# by shift function print "Value returned by shift: ", shift(@x
# Array after shift operation print "\nUpdated array: @x";

Output:
Original array: Java C C++
Value returned by shift :Java
Updated array: C C++

xxx
viii
unshift function
This function places the given list of elements at the beginning of an array. Thereby shifting all the
values in an array by right. Multiple values can be unshift using this operation. This function returns the
number of new elements in an array.

Syntax: unshift(Array, List)


Example:
 Perl
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array print "Original array:
@x \n";
# Prints the number of elements
# returned by unshift print "No of elements returned by unshift: ",
unshift(
@x, 'PHP', 'JSP');
# Array after unshift operation "\n
Updated array: @x";

Output:
Original array: Java C C++
No of elements returned by unshift :5
Updated array: PHP JSP Java C C++

xxx
ix
WEEK-6

EXPERIMENT NO.: 19.


a) Write a Perl script to substitute a word, with another word in a string.
AIM: Write a Perl script to substitute a word, with another word in a string
PROGRAM:
Substitution Operator or ‘s’ operator in Perl is used to substitute a text of the string with some pattern
specified by the user.
Syntax: s/text/pattern
Returns: 0 on failure and number of substitutions on success
Example 1:
#!/usr/bin/perl -w
# String in which text
# is to be replaced
$string = "GeeksforGeeks";
# Use of s operator to replace
# text with pattern
$string =~ s/for/to/;
# Printing the updated string
print "$string\n";

Output:
GeekstoGeeks

xl
b) Write a Perl script to validate IP address and email address.
#!/usr/bin/perl print("Enter the IP Address you would like to validate - ");
my $ip = ;
if($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
{
$ip = $1;
}
chomp($ip); if($ip =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/)
{
print("\nIP address found - $ip\n");
if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
{
print("Each octet of an IP address is ", "within the range - $1.$2.$3.$4\n");
print("\n-> $ip IP address accepted!\n"); }
else
{
print("Octet(s) out of range. ", "Valid number range between 0-255\n"); }
}
Else
{
print("IP Address $ip is not in a valid format\n");
}

xli
EXPERIMENT NO.: 20.
Write a Perl script to print the file in reverse order using command line arguments
AIM: Write a Perl script to print the file in reverse order using command line arguments
PROGRAM:
@lines = <>;
print reverse @lines;
output
1
2
3
4
5

Reverse
5
4
3
2
1

xlii
xliii

You might also like