0% found this document useful (0 votes)
28 views

SL Lab Manual

Uploaded by

Keshav Bagaade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

SL Lab Manual

Uploaded by

Keshav Bagaade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Program1:

Write a Ruby program to create a new string which is n copies of a given string where n is a non-
negative 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

flowchart
Program 2:
Write a Ruby program 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
Flowchart:

Program-3:
Write a Ruby program 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

Flow chart:

Program 4:
Write a Ruby program 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

Flowchart:

Program 5:
Write a Ruby program 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

Output:
y = 5 is greatest.

Flowchart:

Program 6:
Write a Ruby program 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

Flowchart:
Program 7:
Write a Ruby program to check two integers and return true if one of them is 20 otherwise return
their sum.

Code:
def makes20(x,y)
return x == 20|| y==20 ||x +y
end
print makes20(10, 10),"\n"
print makes20(40, 10),"\n"
print makes20(15, 20)

output:
true
false
true

flowchart:
Program 8:
Write a Ruby program to check two temperatures and return true if one is less than 0 and the other
is greater than 100.

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

flowchart:

Program 9:
Write a Ruby program to print the elements of a given array.
Sample array : ["Ruby", 2.3, Time.now]

Code:

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


for array_element in array1
puts array_element
end

output:
Ruby
2.3
2017-12-28 06:01:53 +0000
Flowchart:
Program 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
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

output:
Total Marks: 254

Flowchart:
Program 11:
write a tcl script to find the factorial of a number

code:
proc Factorial {x} {
set i 1; set product 1
while {$i <= $x} {
set product [expr $product * $i]
incr i
}
return $product
}

Output:
Factorial 10
=> 3628800

Program 12:
write a tcl script that multiplies the numbers from 1 to 10

code:

proc mul {10} {


set i 1; set product 1
while {$i <= 10} {
set product [expr $product * $i]
incr i
}
return $product
}

Output:
3628800

Program 13:
write a tcl script for sorting a list using a comparison function
Code:
#!/usr/bin/tclsh

set var {orange blue red green}


set var [lsort $var]
puts $var

output:
blue green orange red

Program 14:
write a tcl script to i) create a list ii)append elements to the list iii)traverse the list iv) concatenate
the list
i)create the list:

syntax:
set listName { item1 item2 item3 .. itemn }
# or
set listName [list item1 item2 item3]
# or
set listName [split "items separated by a character" split_character]

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:

syntax:
append listName split_character value
# or
lappend listName value
code:
#!/usr/bin/tclsh

set var orange


append var " " "blue"
lappend var "red"
lappend var "green"
puts $var

Output:
orange blue red green

iii) traverse the list

Code:

#!/usr/bin/tclsh

foreach item {1 2 3 4 5 6 7 8 9} {

puts $item
}

Output:
$ ./traverse1.tcl
1
2
3
4
5
6
7
8
9
(or)
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
}

We traverse the list using a while loop. When working with a while loop, we also need a counter and
the number of items in the list.
set days [list Monday Tuesday Wednesday Thursday \
Friday Saturday Sunday]
The list command is used to create a list of days.
set n [llength $days]
The length of the list is determined with the llength command.
set i 0
This is a counter.
while {$i < $n} {

puts [lindex $days $i]


incr i
}
The while loop executes the commands in the body until the counter is equal to the number of
elements in the list.
puts [lindex $days $i]
The lindex returns a value from the list pointed to by the counter.
incr i
The counter is increased.
$ ./traverse2.tcl
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

iv) concatenate list


syntax:
concat value1 value2 …

code:
% concat {a b c} {1 2 3}

Output:
abc123

Program 15:
write a tcl script to comparing the file modified times

Syntax:
Get the modification time:
set timestamp [file mtime $filename]

# Set the modification time to ‘now’:


file mtime $filename [clock seconds]
Code:

Program 16:
write a tcl script to copy a file and translate to native format

Syntax:
file copy ?-force? file1 file2

code:
Program 17:
a) write a perl script to find largest of three numbers

Code:
Vi great.pl

#!/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";
}

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

Code:
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
• 22 33 44 55 66 77 88 99 110 121 132
• 24 36 48 60 72 84 96 108 120 132 144

Program 18:
write a perl program to implement the following list of manipulating function
a) shift b) unshift c) push

code:
• shift
If you imagine the array starting on the left hand side, the shift function will move the whole array
one unit to the left. The first element will "fall off" the array and become the function's return value.
(If the array was empty, shift will return undef.)
my @names = ('Foo', 'Bar', 'Moo');
my $first = shift @names;
print "$first\n"; # Foo
print "@names\n"; # Bar Moo

• un shift
This is the opposite operation of shift. unshift will take one or more values (or even 0 if that's what
you like) and place it at the beginning of the array, moving all the other elements to the right.
You can pass it a single scalar value, which will become the first element of the array. Or, as in the
second example, you can pass a second array and then the elements of this second array
(@others in our case) will be copied to the beginning of the main array (@names in our case) moving
the other elements to higher indexes.

my @names = ('Foo', 'Bar');


unshift @names, 'Moo';
print "@names\n"; # Moo Foo Bar
my @others = ('Darth', 'Vader');
unshift @names, @others;
print "@names\n"; # Darth Vader Moo Foo Bar

• push:
The push function can add one or more values to the end of an array. (Well, it can also add 0 values,
but that's not very useful, is it?)
my @names = ('Foo', 'Bar');
push @names, 'Moo';print "@names\n"; # Foo Bar Moo
my @others = ('Darth', 'Vader');
push @names, @others;
print "@names\n"; # Foo Bar Moo Darth Vader
In this example we originally had an array with two elements. Then we pushed a single scalar value
to the end and our array got extended to a 3-element array.
In the second call to push, we pushed the content of the @others array to the end of
the @names array, extending it to a 5-element array.

Program 19:
A) write a perl script to substitute a word with another word in a string

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

$string =~ s/tea/coffee/ig;

print $string;

output:
coffee is good with milk

Here we've used the s/FIND/REPLACE/ syntax to replace all occurrences of "tea" with "coffee".

We've used two flags here. i specifies that the match should be case-insensitive. g says that we want
to replace all occurrences of the specified string or regular expression, not just the first one. In this
case g is superfluous since there is only one "tea" in the string.

b) write a perl script to validate ip address and email address

Code:
use strict;
use warnings;
use 5.010;
use Email::Valid;

foreach my $email ('[email protected]', ' [email protected] ', 'foo at bar.com') {


my $address = Email::Valid->address($email);
say ($address ? "yes '$address'" : "no '$email'");
}

Out put:
yes '[email protected]'
yes '[email protected]'
no 'foo at bar.com'
It properly verifies if an e-mail is valid, it even removes unnecessary white-spaces from both ends of
the e-mail address, but it cannot really verify if the given e-mail address is really the address of
someone, and if that someone is the same person who typed it in, in a registration form. These can
be verified only by actually sending an e-mail to that address with a code and asking the user there
to verify that indeed s/he wanted to subscribe, or do whatever action triggered the email validation.

Ip address
Code:
use strict;
use warnings;
use Data::Validate::IP;

my $ip_with_port="216.108.225.236:60099";
my $ip=(split /:/,$ip_with_port)[0];

my $validator=Data::Validate::IP->new;

if($validator->is_ipv4($ip))
{
print "Yep, $ip is a valid IPv4 address.\n";
}
else
{
print "Nope, $ip is not a valid IPv4 address.\n";
}

Output:
Yep, 216.108.225.236 is a valid IPv4 address.
(or)
while (my $ip = <DATA>) {
chomp $ip;
# older version
# if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)

# see below for explanation


if ($ip =~ /\b(\d{1,3}(?:\.\d{1,3}){3}:\d{1,5})\b/)
{
print "$ip - matches\n";
} else {
print "$ip - does not match\n";
}
}

__DATA__
216.108.225.236:60099
4.2.2.1:1
216.108.225.236:0
1216.1108.1225.1236:1234
216.108.225.236x:123
9216.108.225.236:8472
10.10.10.10

Output:
216.108.225.236:60099 - matches
4.2.2.1:1 - matches
216.108.225.236:0 - matches
1216.1108.1225.1236:1234 - does not match
216.108.225.236x:123 - does not match
9216.108.225.236:8472 - does not match
10.10.10.10 - does not match

Explanation:
/\b # word boundary
( # start memory capture group 1
\d{1,3} # one to three digits, first octat
(:? # start non memory capture group, notice ?:
\.\d{1,3} # a literal dot followed by an ip octet
) # end non memory capture group
{3} # three times of dots and ip octets
: # match a colon
\d{1,5} # port number, one to five digits
) # end of memory capture group 1
\b # word boundary

Program 20:
write a perl script to print the file in reverse order using command line arguments
Code:
Here's the source code for my Perl "reverse file contents" program:
#!/usr/bin/perl

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


# author - alvin alexander, devdaily.com.

@lines = <>;
print reverse @lines;
To run this Perl script from the Unix/Linux command line, first save it in a file named rcat, and then
make it executable, like this:
chmod +x rcat
After that, assuming you have a file named numbers that has contents like this:
1
2
3
4
5
if you run this command:
rcat numbers

output:
5
4
3
2
1

You might also like