You are on page 1of 33

SWAMI VIVEKANANDA INSTITUTE OF TECHNOLOGY

Mahbub College Campus, Secunderabad 500 003.

SCRIPTING LANGUAGES
LABORATORY MANUAL

NAME OF THE STUDENT:…………………………………………

ROLL NO :………………………………………………………………

BRANCH:……………………………..SECTION:……………………

YEAR: …………………………SEMESTER:………………………..

FACULTY INCHARGE SIGNATURE


CS623PE: SCRIPTING LANGUAGES LAB (Professional Elective - III)

III Year B.Tech.ECE II-Sem L T P C

0 0 2 1

Prerequisites: Any High-level programming language (C, C++)

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

Course Outcomes:
 AbilitytounderstandthedifferencesbetweenScriptinglanguagesandprogramminglangua
ges
 Able to gain some fluency programming in Ruby, Perl,TCL

List of Experiments
1. Write a Ruby script to create a new string which is n copies of a given string

where n is a non- negativeinteger


2. WriteaRubyscriptwhichaccepttheradiusofacirclefromtheuserandcomputetheparame

ter and area.


3. Write a Ruby script which accept the user's first and last name and print them in

reverse order with a space betweenthem


4. Write a Ruby script to accept a filename from the user print the extension ofthat

5. Write a Ruby script to find the greatest of threenumbers

6. Write a Ruby script to print odd numbers from 10 to1

7. Write a Ruby scirpt to check two integers and return true if one of them is 20

otherwise return theirsum


8. WriteaRubyscripttochecktwotemperaturesandreturntrueifoneislessthan0andtheothe
r is greater than100
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 anumber

12. Write a TCL script that multiplies the numbers from 1 to10

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 thelist


15. Write a TCL script to comparing the file modifiedtimes.

16. Write a TCL script to Copy a file and translate to nativeformat.

17. a) Write a Perl script to find the largest number among threenumbers.

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 manipulatingfunctions

a)Shift
b)Unshift
c)Push
19. a) Write a Perl script to substitute a word, with another word in astring.

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 linearguments
CONTENTS

WEEK List of Experiments 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 WriteaRubyscriptwhichaccepttheradiusofacirclefromtheusera
ndcomputetheparameter and area.

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

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 to1

7 Write a Ruby scirpt to check two integers and return true if


one of them is 20 otherwise return their sum

8 WriteaRubyscripttochecktwotemperaturesandreturntrueifonei
slessthan0andtheother is greater than100

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 to10
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.


20 Write a Perl script to print the file in reverse order using
command line arguments
Scripting Languages 2021-2022 SVIT

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

Source Code

#!/usr/bin/ruby

def multiple_string(str, n)

return str*n

end

print multiple_string('ruby', 1),"\n"

print multiple_string('ruby', 2),"\n"

print multiple_string('ruby', 3),"\n"

print multiple_string('ruby', 4),"\n"

print multiple_string('ruby', 5),"\n"

OUTPUT

ruby
rubyruby
rubyrubyruby
rubyrubyrubyruby
rubyrubyrubyrubyruby

Dept Of Electronic &Communication Engg Page 1


Scripting Languages 2021-2022 SVIT

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

Source Code:

#!/usr/bin/ruby

puts "Please enter the radius of circle:"

circleRadius = gets.to_f

radius = gets.to_f

perimeter = 2 * 3.141592653 * circleRadius

area = 3.141592653 *circleRadius*circleRadius

puts "The perimeter is #{perimeter}."

puts "The area is #{area}."

OUTPUT

Please enter the radius of circle:


6
The perimeter is 37.699111836.
The area is 113.097335508.

Dept Of Electronic &Communication Engg Page 2


Scripting Languages 2021-2022 SVIT

Experiments-3 Write a Ruby script which accept the user's first and last name and print
Date: them in reverse order with a space betweenthem

Source Code

#!/usr/bin/ruby

puts "enter your first name:"

fname=gets.chomp

puts "Enter your last name:"

lname=gets.chomp

puts "#{lname}#{fname}"

OUTPUT

enter your first name:

nikitha

Enter your last name:

reddyvishnu

reddyvishnunikitha

Dept Of Electronic &Communication Engg Page 3


Scripting Languages 2021-2022 SVIT

Experiments-4 Write a Ruby script to accept a filename from the user print the
Date: extension of that.

Source Code

#!/usr/bin/ruby

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

# fileextention

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

Dept Of Electronic &Communication Engg Page 4


Scripting Languages 2021-2022 SVIT

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


Date:

Source Code

#!/usr/bin/ruby

x,y,z=10,9,25

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

(or)

puts "Enter three numbers:"

x = Integer(gets.chomp)

y = Integer(gets.chomp)

z = Integer(gets.chomp)

if (x == y) && (x == z)

puts "All numbers are equal"

elsif (x > y) && (x > z)

puts "#{x} is larger than #{y} and #{z}"

elsif (y > x) && (y> z)

Dept Of Electronic &Communication Engg Page 5


Scripting Languages 2021-2022 SVIT

puts "#{y} is larger than #{x} and #{z}"

elsif (z > x) && (z > y)

puts "#{z} is larger than #{y} and #{x}"

end

OUTPUT

y = 5 is greatest.

Dept Of Electronic &Communication Engg Page 6


Scripting Languages 2021-2022 SVIT

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


Date:

Source Code

#!/usr/bin/ruby

puts "Odd numbers between 10 to 1: "

9.step 1,-2do|x|

puts "#{x}"

end

OUTPUT

Odd numbers between 10 to 1:


9
7
5
3
1

Dept Of Electronic &Communication Engg Page 7


Scripting Languages 2021-2022 SVIT

Experiments-7 Write a Ruby script to check two integers and return true if one of them is
Date: 20 otherwise return their false statement.

Source Code

#!/usr/bin/ruby

defmakes20(x,y)

return x ==20|| y ==20|| x + y ==20

end

print makes20(10,10),"\n"

print makes20(40,10),"\n"

print makes20(15,20)

OUTPUT

true
false
true

Dept Of Electronic &Communication Engg Page 8


Scripting Languages 2021-2022 SVIT

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

Source Code

#!/usr/bin/ruby

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

Dept Of Electronic &Communication Engg Page 9


Scripting Languages 2021-2022 SVIT

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


Date:

Source Code

#!/usr/bin/ruby

array1 = ["Ruby", 2.3, Time.now]

for array_element in array1

puts array_element

end

OUTPUT

Ruby
2.3
2020-03-16 17:16:14 +0000

Dept Of Electronic &Communication Engg Page 10


Scripting Languages 2021-2022 SVIT

Experiments-10 Write a Ruby script to retrieve the total marks where subject name and
Date: marks of a student stored in a hash.

Source Code

#!/usr/bin/ruby

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

OUTPUT

Total Marks: 254

Dept Of Electronic &Communication Engg Page 11


Scripting Languages 2021-2022 SVIT

Experiments-11 Write a TCL script to find the factorial of anumber


Date:

Source Code:
set fact 1
for {set i 0} {$i<= 6} {incri} {
puts "$i! = $fact"
set fact [expr {$fact * ($i + 1)}]
}

OUTPUT:
$tclshmain.tcl
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720

Dept Of Electronic &Communication Engg Page 12


Scripting Languages 2021-2022 SVIT

Experiments-12 Write a TCL script that multiplies the numbers from 1 to10
Date:

Source code:

set i 1
while {$i <= 10} {
puts $i
incr i
}

output:
1*1=1
1*2=2
2*3=6……….

Dept Of Electronic &Communication Engg Page 13


Scripting Languages 2021-2022 SVIT

Experiments-13
Date: Write a TCL script for Sorting a list using a comparison function

Source Code

#!/usr/bin/tclsh

set names { John Mary Lenka Veronika Julia Robert }

set nums{ 1 5 4 3 6 7 9 2 11 0 8 2 3 }

puts [lsort $names]

puts [lsort -ascii $names]

puts [lsort -ascii -decreasing $names]

puts [lsort -integer -increasing $nums]

puts [lsort -integer -decreasing $nums]

puts [lsort -integer -unique $nums]

set concatsort [concat $names [lsort -integer -unique $nums]]

puts $concatsort

OUTPUT

John Julia Lenka Mary Robert Veronika


John Julia Lenka Mary Robert Veronika
Veronika Robert Mary Lenka Julia John
0 1 2 2 3 3 4 5 6 7 8 9 11
11 9 8 7 6 5 4 3 3 2 2 1 0
0 1 2 3 4 5 6 7 8 9 11
John Mary Lenka Veronika Julia Robert 0 1 2 3 4 5 6 7 8 9 11

Dept Of Electronic &Communication Engg Page 14


Scripting Languages 2021-2022 SVIT

Experiments-14 Write a TCL script to (i)create a list (ii )append elements to the list
Date: (iii)Traverse the list (iv)Concatenate thelist

TCL script to

(i)create a list

Source Code

#!/usr/bin/tclsh
set colorList1 {red green blue}
set colorList2 [list red green blue]
set colorList3 [split "red_green_blue" _]
puts $colorList1
puts $colorList2
puts $colorList3

OUTPUT

red green blue


red green blue
red green blue

(ii )append elements to the list

Source Code

#!/usr/bin/tclsh

set var orange

append var " " "blue"

lappend var "red"

lappend var "green"

Dept Of Electronic &Communication Engg Page 15


Scripting Languages 2021-2022 SVIT

puts $var

OUTPUT

orange blue red green


(iii)Traverse the list

Source code

#!/usr/bin/tclsh

set days [list Monday Tuesday Wednesday Thursday \

Friday Saturday Sunday]

set n [llength $days]

set i 0

while {$i< $n} {

puts [lindex $days $i]

incr i

OUTPUT
Traverse of the list
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Dept Of Electronic &Communication Engg Page 16


Scripting Languages 2021-2022 SVIT

(iv)Concatenate thelist

Source Code

#!/usr/bin/tclsh
set animals1 { lion eagle elephant dog cat }
set animals2 { giraffe tiger horse dolphin }
set animals [concat $animals1 $animals2]
puts $animals

puts [lsearch -exact $animals eagle]


puts [lreplace $animals 3 4 buffalo crocodile]

OUTPUT

lion eagle elephant dog cat giraffe tiger horse dolphin


1
lion eagle elephant buffalo crocodile giraffe tiger horse dolphin

Dept Of Electronic &Communication Engg Page 17


Scripting Languages 2021-2022 SVIT

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


Date:

Source Code

proc newer { file1 file2 } {

if ![file exists $file2] {

return 1

} else {

# Assume file1 exists

expr [file mtime $file1] > [file mtime $file2]

OUTPUT

Time files

Dept Of Electronic &Communication Engg Page 18


Scripting Languages 2021-2022 SVIT

Experiments-16 Write a TCL script to Copy a file and translate to native format.
Date:

Source code:

set f [open $filename]


while {[gets $f line] >= 0} {
# work with $line here ...
}
close $f

output:

one to another file

Dept Of Electronic &Communication Engg Page 19


Scripting Languages 2021-2022 SVIT

Experiments-17 a) Write a Perl script to find the largest number among threenumbers.
Date:

a) Write a Perl script to find the largest number among threenumbers.

Source Code

#!/usr/bin/perl
print "enter a value";
$x=<stdin>;
print "enter b value";
$y=<stdin>;
print "enter c value";
$z=<stdin>;
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";
}

OUTPUT

Enter a value 4
Enter b value 6
Enter c value 5
6 is largest number

Dept Of Electronic &Communication Engg Page 20


Scripting Languages 2021-2022 SVIT

Experiments-17
b) Write a Perl script to print the multiplication tables from 1-10 usings
Date:

Source Code

for($i=1;$i<=10;$i++)
{
$a[$i]=$i;

for($i=1;$i<=10;$i++)
{
for($j=1;$j<=10;$j++)
{
print(($a[$j]*$a[$i])," ");
}
print "\n\n";
}

Output:

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

Dept Of Electronic &Communication Engg Page 21


Scripting Languages 2021-2022 SVIT

Experiments-18
Write a Perl program to implement the following list of manipulating
Date:
function a) shift b)Unshift c)Push

a)Shift

Source Code

#!/usr/bin/perl

# Initalizing the array


@x = ('Java', 'C', 'C++');

# Print the Inital 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++

Dept Of Electronic &Communication Engg Page 22


Scripting Languages 2021-2022 SVIT

b)Unshift

Source Code

#!/usr/bin/perl

# Initalizing the array


@x = ('Java', 'C', 'C++');

# Print the Inital 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


print "\nUpdated array: @x";

OUTPUT

Original array: Java C C++


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

Dept Of Electronic &Communication Engg Page 23


Scripting Languages 2021-2022 SVIT

c) Push

Source Code

#!/usr/bin/perl

# Initalizing the array


@x = ('Java', 'C', 'C++');

# Print the Inital array


print "Original array: @x \n";

# Pushing multiple values in the array


push(@x, 'Python', 'Perl');

# Printing the array


print "Updated array: @x";

OUTPUT

Original array: Java C C++


Updated array: Java C C++ Python Perl

Dept Of Electronic &Communication Engg Page 24


Scripting Languages 2021-2022 SVIT

Experiments-19 a) Write a Perl script to substitute a word, with another word in astring.
Date:

Source Code

my $string = q|

Here is some multi-line text.

We could replace all words in this text

beginning with "c" with the word "badger".

Or of course we could do something

completely different.

|;

$string =~ s/\bc\w*\b/badger/ig;

print $string;

my $string = "Tea is good with milk.";

substr($string, 4, 2) = "might be";

print $string;

OUTPUT

Here is some multi-line text.


We badger replace all words in this text
beginning with "badger" with the word "badger".
Or of badger we badger do something
badger different.

Tea might be good with milk.

Dept Of Electronic &Communication Engg Page 25


Scripting Languages 2021-2022 SVIT

Experiments-19 (b) Write a Perl script to validate IP address and email address.
Date:

Source Code

print "Testing IP Addresses in array, \@ip: \n";

my @ip = ("0123.456.789.654",

"a123.456.789.654",

"123.456.789.6543",

"111.222.333.444")

for $ip (@ip) {

if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)

print "yes $ip has a match: $1 $2 $3 $4\n";

else

print "no match\n";

print "\n\t whereas, with anchors in the ip, \n";

Dept Of Electronic &Communication Engg Page 26


Scripting Languages 2021-2022 SVIT

for $ip(@ip) {

if ( $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {

print "Second regex matches $1 $2 $3 $4\n";

else

print "no match in $ip \n";

OUTPUT

Testing IP Addresses in array, @ip:


yes 0123.456.789.654 has a match: 123 456 789 654
yes a123.456.789.654 has a match: 123 456 789 654
yes 123.456.789.6543 has a match: 123 456 789 654
yes 111.222.333.444 has a match: 111 222 333 444

whereas, with anchors in the ip,


no match in 0123.456.789.654
no match in a123.456.789.654
no match in 123.456.789.6543
Second regex matches 111 222 333 444

Dept Of Electronic &Communication Engg Page 27


Scripting Languages 2021-2022 SVIT

Experiments-20 Write a Perl script to print the file in reverse order using command line
Date: arguments

Source Code

# rcat - a program to display the contents of a file in reverse order.

# author - alvin alexander, devdaily.com.

print"Reverse of a file program content.\n";

@lines = <welcome to perl script>;

print reverse @lines;

OUTPUT

Reverse of a file program content.


scriptperltowelcome

Dept Of Electronic &Communication Engg Page 28

You might also like