[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Scripting Languages
Experiment 1: Create a new string which is n copies of a given string
Aim: Write a Ruby script to create a new string which is n copies of a given string where n is a
non-negative integer.
Tools Required:
Ruby Interpreter (e.g., ruby 3.x)
Any code editor (Notepad++, VS Code, etc.)
Code:
def repeat_string(str, n)
return str * n
end
puts repeat_string("Hello", 3)
Output:
HelloHelloHello
1
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 2: Compute perimeter and area of a circle
Aim: Write a Ruby script to accept the radius of a circle from the user and compute the
perimeter and area.
Tools Required:
Ruby Interpreter
Text editor or IDE
Code:
puts "Enter radius:"
r = gets.to_f
area = Math::PI * r**2
perimeter = 2 * Math::PI * r
puts "Area = #{[Link](2)}"
puts "Perimeter = #{[Link](2)}"
Output:
Enter radius:
3
Area = 28.27
Perimeter = 18.85
2
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 3: Reverse full name entered by user
Aim: Write a Ruby script to accept the user's first and last name and print them in reverse order
with a space between them.
Tools Required:
Ruby Interpreter
Text editor
Code:
puts "Enter First Name:"
first = [Link]
puts "Enter Last Name:"
last = [Link]
puts "Reverse Order: #{last} #{first}"
Output:
Enter First Name:
Ayesha
Enter Last Name:
Kausar
Reverse Order: Kausar Ayesha
3
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 4: Print file extension from filename
Aim: Write a Ruby script to accept a filename from the user and print its extension.
Tools Required:
Ruby Interpreter
Text editor
Code:
puts "Enter filename:"
file = [Link]
ext = [Link](file)
puts "Extension: #{ext}"
Output:
Enter filename:
[Link]
Extension: .txt
4
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 5: Find the greatest of three numbers
Aim: Write a Ruby script to find the greatest of three numbers entered by the user.
Tools Required:
Ruby Interpreter
Text editor or IDE
Code:
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i
greatest = [a, b, c].max
puts "Greatest number is: #{greatest}"
Output:
Enter three numbers:
12
78
34
Greatest number is: 78
5
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 6: Print odd numbers from 10 to 1
Aim: Write a Ruby script to print all odd numbers from 10 down to 1.
Tools Required:
Ruby Interpreter
Text editor
Code:
[Link](1) do |i|
puts i if [Link]?
end
Output:
9
7
5
3
1
6
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 7: Check if one integer is 20 or return their sum
Aim: Write a Ruby script to check two integers and return true if one of them is 20, otherwise
return their sum.
Tools Required:
Ruby Interpreter
Text editor
Code:
def check_twenty(a, b)
if a == 20 || b == 20
return true
else
return a + b
end
end
puts check_twenty(20, 5)
puts check_twenty(8, 12)
Output:
true
20
7
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 8: Check temperature conditions (one < 0 and other > 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.
Tools Required:
Ruby Interpreter
Text editor
Code:
def extreme_temp?(t1, t2)
(t1 < 0 && t2 > 100) || (t2 < 0 && t1 > 100)
end
puts extreme_temp?(120, -5)
puts extreme_temp?(25, 30)
Output:
true
false
8
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 9: Print elements of an array
Aim: Write a Ruby script to print the elements of a given array.
Tools Required:
Ruby Interpreter
Text editor
Code:
arr = [10, 20, 30, 40, 50]
puts "Array elements:"
[Link] do |item|
puts item
end
Output:
Array elements:
10
20
30
40
50
9
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 10: Retrieve total marks from a hash
Aim: Write a Ruby program to retrieve the total marks where subject name and marks of a
student are stored in a hash.
Tools Required:
Ruby Interpreter
Text editor
Code:
marks = {
"Maths" => 85,
"Science" => 90,
"English" => 88
}
total = [Link]
puts "Total Marks: #{total}"
Output:
Total Marks: 263
10
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 11: Find factorial of a number
Aim: Write a TCL script to find the factorial of a number.
Tools Required:
TCL Interpreter (tclsh)
Text editor
Code:
puts "Enter a number:"
gets stdin num
set fact 1
for {set i 1} {$i <= $num} {incr i} {
set fact [expr $fact * $i]
}
puts "Factorial of $num is $fact"
Output:
Enter a number:
5
Factorial of 5 is 120
11
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 12: Multiply numbers from 1 to 10
Aim: Write a TCL script that multiplies the numbers from 1 to 10.
Tools Required:
TCL Interpreter
Text editor
Code:
set product 1
for {set i 1} {$i <= 10} {incr i} {
set product [expr $product * $i]
}
puts "Product of numbers from 1 to 10 is: $product"
Output:
Product of numbers from 1 to 10 is: 3628800
12
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 13: Sort a list using comparison function
Aim: Write a TCL script to sort a list using a comparison function.
Tools Required:
TCL Interpreter
Text editor
Code:
set numbers {12 5 8 1 14 7}
set sorted [lsort -integer $numbers]
puts "Original List: $numbers"
puts "Sorted List: $sorted"
Output:
Original List: 12 5 8 1 14 7
Sorted List: 1 5 7 8 12 14
13
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 14: Perform list operations: create, append, traverse, concatenate
Aim: Write a TCL script to perform the following list operations: (i) create a list (ii) append
elements to the list (iii) traverse the list (iv) concatenate the list
Tools Required:
TCL Interpreter
Text editor
Code:
set mylist [list "Apple" "Banana" "Cherry"]
lappend mylist "Date" "Elderberry"
puts "List Elements:"
foreach item $mylist {
puts $item
}
set joined [join $mylist ", "]
puts "Concatenated: $joined"
Output:
List Elements:
Apple
Banana
Cherry
Date
Elderberry
Concatenated: Apple, Banana, Cherry, Date, Elderberry
14
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 15: Compare file modified times
Aim: Write a TCL script to compare the last modified times of two files.
Tools Required:
TCL Interpreter
Two sample files (e.g., [Link] and [Link])
Code:
set file1 "[Link]"
set file2 "[Link]"
set mod1 [file mtime $file1]
set mod2 [file mtime $file2]
if {$mod1 > $mod2} {
puts "$file1 is modified more recently than $file2"
} elseif {$mod1 < $mod2} {
puts "$file2 is modified more recently than $file1"
} else {
puts "Both files were modified at the same time"
}
Output:
[Link] is modified more recently than [Link]
15
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 16: Copy a file and translate to native format
Aim: Write a TCL script to copy a file and translate it to the native format line by line.
Tools Required:
TCL Interpreter
Source and destination file names
Code:
set source "[Link]"
set target "[Link]"
set in [open $source r]
set out [open $target w]
while {[gets $in line] >= 0} {
puts $out $line
}
close $in
close $out
puts "File copied successfully."
Output:
File copied successfully.
16
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 17:
a) Find largest among 3 numbers
b) Multiplication tables using subroutines
Aim: a) Write a Perl script to find the largest number among three numbers. b) Write a Perl
script to print multiplication tables from 1 to 10 using subroutines.
Tools Required:
Perl Interpreter
Text editor
Code (a):
print "Enter three numbers:
";
chomp($a = <STDIN>);
chomp($b = <STDIN>);
chomp($c = <STDIN>);
$max = $a;
if ($b > $max) {
$max = $b;
}
if ($c > $max) {
$max = $c;
}
print "The largest number is: $max
";
Output (a):
Enter three numbers:
12
78
34
The largest number is: 78
17
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Code (b):
sub print_table {
my $num = shift;
print "Multiplication Table for $num:
";
for my $i (1..10) {
print "$num x $i = ", $num * $i, "
";
}
print "
";
}
for my $n (1..10) {
print_table($n);
}
Output (b):
Multiplication Table for 1:
1 x 1 = 1
1 x 2 = 2
...
1 x 10 = 10
...
Multiplication Table for 10:
10 x 10 = 100
18
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 18: List manipulation: Shift, Unshift, Push
Aim: Write a Perl program to implement the following list manipulation functions: Shift,
Unshift, and Push.
Tools Required:
Perl Interpreter
Text editor
Code:
@fruits = ("Apple", "Banana", "Cherry");
print "Original List: @fruits
";
push(@fruits, "Date");
print "After push: @fruits
";
unshift(@fruits, "Avocado");
print "After unshift: @fruits
";
$removed = shift(@fruits);
print "Removed using shift: $removed
";
print "Final List: @fruits
";
Output:
Original List: Apple Banana Cherry
After push: Apple Banana Cherry Date
After unshift: Avocado Apple Banana Cherry Date
Removed using shift: Avocado
Final List: Apple Banana Cherry Date
19
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 19:
a) Substitute word in string
b) Validate IP and email
Aim:
a) Write a Perl script to substitute a word with another word in a string.
b) Write a Perl script to validate an IP address and an email address.
Tools Required:
Perl Interpreter
Text Editor
Code (a): Substitute word in string
$text = "Perl is fun and powerful.";
print "Original: $text\n";
$text =~ s/fun/awesome/;
print "Modified: $text\n";
Output:
Original: Perl is fun and powerful.
Modified: Perl is awesome and powerful.
20
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Code (b): Validate Email and IP
# Email
$email = "student@[Link]";
if ($email =~ /^[\w\.-]+@[\w\.-]+\.\w+$/) {
print "Valid Email: $email\n";
} else {
print "Invalid Email: $email\n";
}
# IP
$ip = "[Link]";
if ($ip =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
print "Valid IP: $ip\n";
} else {
print "Invalid IP: $ip\n";
}
Output:
Valid Email: student@[Link]
Valid IP: [Link]
21
[DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING]
Experiment 20: Print file in reverse order using command-line arguments
Aim:
Write a Perl script to print the contents of a file in reverse order using command-line arguments.
Tools Required:
Perl Interpreter
Text Editor
Code:
if (@ARGV != 1) {
die "Usage: perl reverse_file.pl <filename>\n";
}
$filename = $ARGV[0];
open(my $fh, '<', $filename) or die "Cannot open $filename: $!\n";
@lines = <$fh>;
close($fh);
print "File contents in reverse:\n";
print reverse @lines;
Output (for file with 3 lines):
File contents in reverse:
Line 3
Line 2
Line 1
22