SL Lab Manual
SL Lab Manual
INDEX
Exp. Name of the Experiment Page
No No
1 Write a Ruby script to create a new string which is n copies of a given 8
string where n is a non-negative integer
2 Write a Ruby script which accept the radius of a circle from the user and 9
compute the parameter and area.
3 Write a Ruby script which accept the user's first and last name and print 10
them in reverse order with a space between them.
4 Write a Ruby script to accept a filename from the user print the extension 11
of that.
5 Write a Ruby script to find the greatest of three numbers 12
6 Write a Ruby script to print odd numbers from 10 to 1 13
7 Write a Ruby scirpt to check two integers and return true if one of them is 14
20 otherwise return their sum
8 Write a Ruby script to check two temperatures and return true if one is less 15
than 0 and the other is greater than 100.
9 Write a Ruby script to print the elements of a given array 16
10 Write a Ruby program to retrieve the total marks where subject name and 18
marks of a student stored in a hash
11 Write a TCL script to find the factorial of a number 19
12 Write a TCL script that multiplies the numbers from 1 to 10 20
13 Write a TCL script for Sorting a list using a comparison function 21
14 Write a TCL script to (i)create a list (ii )append elements to the list 22
(iii)Traverse the list (iv)Concatenate the list
15 Write a TCL script to comparing the file modified times. 24
16 Write a TCL script to Copy a file and translate to native format. 25
17 a) Write a Perl script to find the largest number among three numbers. 26
b) Write a Perl script to print the multiplication tables from 1-10 using
subroutines.
18 Write a Perl script to print the multiplication tables from 1-10 using 28
subroutines.
a) Shift b) Unshift c) Push
19 a) Write a Perl script to substitute a word, with another word in a 30
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 33
arguments.
CONTENT BEYOND SYLLABUS
Exp. Name of the Experiment Page
No No
1 Write a Ruby Script to demonstrate Menu Driven Application 34
2 Implement Ruby TK application to demonstrate Event Handling 36
3 Write a PERL script to demonstrate Command Line Arguments 38
4 Write a PERL script to demonstrate to Packages, Modules and Classes 39
INTRODUCTION
Ruby is based on many other languages like Perl, Lisp, Smalltalk, Eiffel and Ada. It is an
interpreted scripting language which means most of its implementations execute instructions
directly and freely, without previously compiling a program into machine-language
instructions. Ruby programmers also have access to the powerful RubyGems (RubyGems
provides a standard format for Ruby programs and libraries).
Beginning with Ruby programming:
1. Finding a Compiler:
Before starting programming in Ruby, a compiler is needed to compile and run our programs.
There are many online compilers that can be used to start Ruby without installing a compiler:
https://www.jdoodle.com/execute-ruby-online
https://repl.it/
There are many compilers available freely for compilation of Ruby programs.
2. Programming in Ruby:
To program in Ruby is easy to learn because of its similar syntax to already widely used
languages.
Writing program in Ruby:
Programs can be written in Ruby in any of the widely used text editors like Notepad++, gedit
etc. After writing the programs save the file with the extension .rb
Advantages of Ruby:
The code written in Ruby is small, elegant and powerful as it has fewer number of
lines of code.
Ruby allows simple and fast creation of Web application which results in less hard
work.
As Ruby is free of charge that is Ruby is free to copy, use, modify, it allow
programmers to make necessary changes as and when required.
Ruby is a dynamic programming language due to which there is no tough rules on how
to built in features and it is very close to spoken languages.
Disadvantages of Ruby:
Ruby is fairly new and has its own unique coding language which makes it difficult for
the programmers to code in it right away but after some practice its easy to use. Many
programmers prefer to stick to what they already know and can develop.
The code written in Ruby is harder to debug, since most of the time it generates at
runtime, so it becomes difficult to read while debugging.
Ruby does not have a plenty of informational resources as compared to other
programming languages.
Ruby is an interpreted scripting language, the scripting languages are usually slower
than compiled languages therefore, Ruby is slower than many other languages.
Applications:
Ruby is used to create web applications of different sorts. It is one of the hot
technology at present to create web applications.
Ruby offers a great feature called Ruby on Rails (RoR). It is a web framework that is
used by programmers to speed up the development process and save time.
About PERL Language
Perl is a general purpose, high level interpreted and dynamic programming language. Perl
supports both the procedural and Object-Oriented programming. Perl is a lot similar to C
syntactically and is easy for the users who have knowledge of C, C++. Since Perl is a lot similar
to other widely used languages syntactically, it is easier to code and learn in Perl. Programs can
be written in Perl in any of the widely used text editors like Notepad++, gedit, Sbublime etc.
Tcl is shortened form of Tool Command Language. John Ousterhout of the University of
California, Berkeley, designed it. It is a combination of a scripting language and its own
interpreter that gets embedded to the application, we develop with it.
Tcl was developed initially for Unix. It was then ported to Windows, DOS, OS/2, and Mac
OSX. Tcl is much similar to other unix shell languages like Bourne Shell (Sh), the C Shell
(csh), the Korn Shell (sh), and Perl.
It aims at providing ability for programs to interact with other programs and also for acting as
an embeddable interpreter. Even though, the original aim was to enable programs to interact,
you can find full-fledged applications written in Tcl/Tk.
Features of Tcl
Applications
Tcl is a general-purpose language and you can find Tcl everywhere. It includes,
Algorithm
1. Read one string
2. Using sort function we can sort the array element in descending order
Code:
puts "enter string"
s1=gets.chomp
puts "enter any no"
no=gets.chomp.to_
i if no > 0
puts s1 * no
els
e
puts "enter +ve no "
end
Output
:
2. Write a Ruby script which accept the radius of a circle from the user and compute the
parameter and area.
Code:
puts "enter
radius"
r=gets.chomp.to_f
pi=3.14
area=pi * r * r
peremeter= 2 * pi * r
Output puts "area=#{area} and peremeter=#{peremeter}"
:
SCRIPTING LANGUAGES LAB 2023-24
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.
Code:
puts "first name"
fname=gets.chomp
puts "enter last
name"
lname=gets.chomp
rfn=fname.reverse
rln=lname.reverse
puts "#{fname.reverse} #{rln}"
Output:
Page 10
SCRIPTING LANGUAGES LAB 2023-24
4. Write a Ruby script to accept a filename from the user print the extension of that.
Code:
puts "enter
filename" file =
gets.chomp
Page 11
SCRIPTING LANGUAGES LAB 2023-24
Page 12
SCRIPTING LANGUAGES LAB 2023-24
Output:
Page 13
SCRIPTING LANGUAGES LAB 2023-24
7. Write a Ruby scirpt to check two integers and return true if one of them is 20 otherwise
return their sum
Code:
def check(a,b)
if(a==20 || b==20)
return true
els
e
return a+b
end
end
puts "enter no 1"
x=gets.chomp.to_
i puts "enter no2"
y=gets.chomp.to_
i res=check(x,y)
puts res
Output
:
Page 14
SCRIPTING LANGUAGES LAB 2023-24
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.
Code:
def check(p,q)
if((p<0 && q>100) or (p>100 &&
q<0)) return true
else
return false
end
end
puts "enter no1"
a=gets.chomp.to_
i puts "enter no2"
b=gets.chomp.to_
i puts check(a,b)
Output
:
Page 15
SCRIPTING LANGUAGES LAB 2023-24
Page 16
SCRIPTING LANGUAGES LAB 2023-24
Output:
Page 17
SCRIPTING LANGUAGES LAB 2023-24
10. Write a Ruby program to retrieve the total marks where subject name and marks of a
student stored in a hash.
Code:
marks =
Hash.new
marks['c'] = 30
marks['java'] = 30
marks['sl'] = 30
marks['ml'] = 0
tmarks = 0
marks.each do |key,value|
tmarks +=value
end
puts "Total Marks: "+tmarks.to_s
Ouput
:
Page 18
SCRIPTING LANGUAGES LAB 2023-24
#set x 5;
while {$i <= $x} {
set product [expr $product *
$i]; incr i;
}
puts "factorial of $x=$product";
Output:
Page 19
SCRIPTING LANGUAGES LAB 2023-24
proc run_table { } {
puts -nonewline "Enter a number: "
flush stdout
gets stdin x
times_table
$x
}
run_table
#end of program
Output
:
Page 20
SCRIPTING LANGUAGES LAB 2023-24
13. Write a TCL script for Sorting a list using a comparison function
Code:
set lst1 {a1 b2 c1
A1}; puts $lst1;
#lsort {a1 b2 c1 A1};
set lst2 [lsort -ascii
$lst1]; puts $lst2;
set lst3 {5 54 3 22 66 77 31};
set slst3 [lsort -integer
$lst3]; puts $slst3;
set rslst3 [lsort -integer -decreasing
$lst3]; puts $rslst3;
set lst4 {5.3 54.5 3.6 2.2 6.6 7.7 3.1};
set slst4 [lsort -real
$lst4]; puts $slst4;
set ralst4 [lsort -real -decreasing
$lst4 ]; puts $ralst4;
Output:
Page 21
SCRIPTING LANGUAGES LAB 2023-24
Code
:
#createing list
set lst1 {1 2
3} puts $lst1
set lst2 {4 5
6} #concat
list
set lst3 [concat $lst1
$lst2] puts $lst3;
#append list
lappend lst3 7 8
puts $lst3;
#travse list
foreach val $lst3
{ puts $val;
}
Page 22
SCRIPTING LANGUAGES LAB 2023-24
Output:
Page 23
SCRIPTING LANGUAGES LAB 2023-24
Page 24
SCRIPTING LANGUAGES LAB 2023-24
16. Write a TCL script to Copy a file and translate to native format.
Code:
puts "enter file name to
copy"; gets stdin fn
puts $fn
set fp [open $fn r]
set data [read $fp]
#puts $file_data
#close $fp
Page 25
SCRIPTING LANGUAGES LAB 2023-24
17. a). Write a Perl script to find the largest number among three
numbers. Code:
#!/usr/bin/perl
#use strict;
use
warnings;
print "enter no1\n";
$p=<STDIN>;
print "enter no2\n";
$q=<STDIN>;
print "enter no3\n";
$r=<STDIN>;
Page 26
SCRIPTING LANGUAGES LAB 2023-24
b). Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Code
:
sub multable
{
$n=@_[0];
print("table no: ".$n."\
n"); my $i=1;
my $re=0;
while($i<=10
)
{
$re=$i*$n;
print "$i*$n=$re\n";
$i++;
}
print "-----------\n";
}
$i=1
; while($i<=10)
{
multable($i);
$i++;
}
Output
:
Page 27
SCRIPTING LANGUAGES LAB 2023-24
Page 28
SCRIPTING LANGUAGES LAB 2023-24
18. Write a Perl script to print the multiplication tables from 1-10 using subroutines.
a) Shift b) Unshift c) Push
Code:
#implement shift,unshift,push,pop
methods #implement shift
@arr1=(1,2,3,4);
$ele=shift @arr1;
# shift delete the first element in the array and returns the element
print $ele;
#implement
unshift
@arr1=(1,2,3,4);
#unshift add the element in the array at the
begiening print "before unshift :@arr1";
unshift (@arr1,9);
print "after unshift : @arr1";
#implement poo
@arr1=(1,2,3,4)
;
$ele=pop @arr1;
# pop delete the last element in the array and returns the element
print $ele."\n";
#implement push
@arr1=(1,2,3,4)
;
#push add the element in the array at the last
pos.. print "before push :@arr1";
push (@arr1,9);
print "after push : @arr1";
Page 29
SCRIPTING LANGUAGES LAB 2023-24
Output:
Page
210
SCRIPTING LANGUAGES LAB 2023-24
19. a).Write a Perl script to substitute a word, with another word in a string.
Code:
use strict;
use warnings;
my $myString = "";
my $myCount = $myString =~
s/$myWord2/$myWord3/; print "$myString \n";
print "$myCount \
n"; exit(0);
Ouput
:
Page 30
SCRIPTING LANGUAGES LAB 2023-24
Code
:
print "enter ip address";
$ip=<STDIN>;
if(($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) && ($1 <= 255 && $2
<= 255 && $3 <= 255 && $4 <= 255 )){
print "ip address is $ip" ;
}
else
{
print "wrong ip address";
}
Output:
Page 31
SCRIPTING LANGUAGES LAB 2023-24
Code:
use strict;
use
warnings;
use 5.010;
use Email::Address;
print "enter email";
my $line =
<STDIN> ;
chomp($line);
my $addresses = Email::Address-
>parse($line); print $addresses,"\n";
if($addresses==1)
{
print "valied email address";
}
else{
print "invalied email address";
}
Output:
Page 32
SCRIPTING LANGUAGES LAB 2023-24
20. Write a Perl script to print the file in reverse order using command line arguments.
Code:
#print "enter filename to
read"; my $file=$ARGV[0];
chomp($file); open(DATA,
$file) or die $!;
@lines=<DATA>;
@rlines=reverse(@lines);
print @rlines;
foreach my $x (@rlines) {
$rline=reverse($x)
; print $rline;
}
close(DATA);
Output:
Page 33
SCRIPTING LANGUAGES LAB 2023-24
Code:
require "tk"
root = TkRoot.new
root.title = "Window"
menu_click = Proc.new
{ Tk.messageBox(
'type' => "ok",
'icon' =>
"info", 'title' =>
"Title",
'message' => "Message"
)
}
file_menu = TkMenu.new(root)
file_menu.add('command',
'label' => "New...",
'command' => menu_click,
'underline' => 0)
file_menu.add('command',
'label' => "Open...",
'command' => menu_click,
'underline' => 0)
file_menu.add('command',
'label' => "Close",
'command' => menu_click,
'underline' => 0)
file_menu.add('separator'
)
file_menu.add('command',
'label' => "Save",
'command' => menu_click,
'underline' => 0)
file_menu.add('command',
'label' => "Save As...",
Page 34
SCRIPTING LANGUAGES LAB 2023-24
Page 35
SCRIPTING LANGUAGES LAB 2023-24
'underline' => 5)
file_menu.add('separator'
)
file_menu.add('command',
'label' => "Exit",
'command' => menu_click,
'underline' => 3)
menu_bar =
TkMenu.new
menu_bar.add('cascade',
'menu' => file_menu,
'label' => "File")
root.menu(menu_bar
) Tk.mainloop
Ouput:
Page 36
SCRIPTING LANGUAGES LAB 2023-24
Code:
require "tk"
f1 = TkFrame.new
{ relief 'sunken'
borderwidth 3
background "red"
padx 15
pady 20
pack('side' => 'left')
}
f2 = TkFrame.new {
relief 'groove'
borderwidth 1
background
"yellow" padx 10
pady 10
pack('side' => 'right')
}
f3 = TkFrame.new {
relief 'groove'
borderwidth 1
background
"blue" padx 30
pady 20
pack('side' => 'top')
}
TkButton.new(f3)
{ text 'Button1'
command {print "push button1!!\
n"} pack('fill' => 'x')
}
TkButton.new(f1)
{ text 'Button1'
command {print "push button1!!\
n"} pack('fill' => 'x')
}
Page 37
SCRIPTING LANGUAGES LAB 2023-24
TkButton.new(f1)
{ text 'Button2'
command {print "push button2!!\
n"} pack('fill' => 'x')
}
TkButton.new(f2)
{ text 'Quit'
command 'exit'
pack('fill' =>
'x')
}
Tk.mainloop
Output:
Page 38
SCRIPTING LANGUAGES LAB 2023-24
}
$n1=$ARGV[0];
$op=$ARGV[1];
$n2=$ARGV[2];
$ans=0;
if ( $op eq "+" ) {
$ans = $n1 + $n2;
}
elsif ( $op eq "-"){
$ans = $n1 - $n2;
}
elsif ( $op eq "/"){
$ans = $n1 / $n2;
}
elsif ( $op eq "*"){
$ans = $n1 * $n2;
}
else
{ print "Error: op must be +, -, *, / only\
n"; exit;
}
print "$ans\n";
Output:
Page 39
SCRIPTING LANGUAGES LAB 2023-24
Code:
MyClass.pm
package
MyClass; use
strict;
use warnings;
sub add {
my $self = shift;
print "class_variable: $a1\
n"; #$a=shift;
#$b=shift;
($a,$b)=@_;
print $self;
print " $a\n";
print "$b";
print $a+$b;
$b;
print "subtract:($a-
$b)="; print $a-$b;
Page
311
SCRIPTING LANGUAGES LAB 2023-24
}
sub mul
{
my $self=shift;
$a=shift;
$b=shift;
$c=$a*$b
; return
$c;
}
1;
Myclass1.pl
#!/usr/bin/
perl use
strict;
use
warnings;
use MyClass;
my $res;
my $foo = MyClass->new();
$foo->add(20,22);
$foo->subtract(55,22);
$res=$foo->mul(8,8);
print "\nresult=$res ";
Ouput:
Page 40
SCRIPTING LANGUAGES LAB 2023-24
1. What Is Tcl?
2. How To Increment Eacl Element In A List?
3. How To Run A Package In Tcl ?
4. How to create arrays in Tcl?
5. How Increment A Character?
6. How Do You Find The Length Of A String Without Using String Length
Command In Tcl?
7. How To Check Whether A String Is Palindrome Or Not Using Tcl Script?
8. What is namespace?
9. Upvar Command?
10. How to create procedures in TCL?
11. What is the purpose of eval command explain?
12. What is the purpose of source command explain?
13. What is the purpose of exec command explain?