0% found this document useful (0 votes)
5 views13 pages

Ruby, TCL, and Perl Scripting Examples

The document contains a series of Ruby, TCL, and Perl scripts that perform various programming tasks such as string manipulation, mathematical calculations, file operations, and data validation. Each script includes a description, sample input, and expected output. The scripts demonstrate fundamental programming concepts and syntax in their respective languages.

Uploaded by

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

Ruby, TCL, and Perl Scripting Examples

The document contains a series of Ruby, TCL, and Perl scripts that perform various programming tasks such as string manipulation, mathematical calculations, file operations, and data validation. Each script includes a description, sample input, and expected output. The scripts demonstrate fundamental programming concepts and syntax in their respective languages.

Uploaded by

kodumurisusena
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Scripting Language Lab programs

1. Ruby script: create a new string which is n copies of a given


string

Program

print "Enter a string: "


str = [Link]
print "Enter n (non-negative integer): "
n = gets.to_i

result = str * n
puts "Output: #{result}"

Sample Input

Hello
3

Output

Output: HelloHelloHello

---

✅ 2. Ruby script: accept radius and compute area & perimeter

Program

print "Enter radius: "


r = gets.to_f

area = 3.14 * r * r
perimeter = 2 * 3.14 * r

puts "Area = #{area}"


puts "Perimeter = #{perimeter}"

Sample Input

5
Output

Area = 78.5
Perimeter = 31.400000000000002

---

✅ 3. Ruby script: accept first & last name and print in reverse
order with space

Program

print "Enter first name: "


f = [Link]
print "Enter last name: "
l = [Link]

puts "Reverse order: #{l} #{f}"

Sample Input

John
Doe

Output

Reverse order: Doe John

---

✅ 4. Ruby script: accept a filename and print its extension

Program

print "Enter filename: "


file = [Link]

ext = [Link](file)
puts "Extension: #{ext}"
Sample Input

[Link]

Output

Extension: .txt

---

✅ 5. Ruby script: find greatest of three numbers

Program

print "Enter three numbers: "


a = gets.to_i
b = gets.to_i
c = gets.to_i

greatest = [a, b, c].max


puts "Greatest = #{greatest}"

Sample Input

10
25
18

Output

Greatest = 25

---

✅ 6. Ruby script: print odd numbers from 10 to 1

Program

[Link](1) do |i|
puts i if [Link]?
end
Output

9
7
5
3
1

---

✅ 7. Ruby script: check two integers → return true if one is 20 OR


sum is 20

Program

print "Enter number 1: "


a = gets.to_i
print "Enter number 2: "
b = gets.to_i

if a == 20 || b == 20 || (a + b) == 20
puts true
else
puts false
end

Sample Input

10
10

Output

true

---

✅ 8. Ruby script: check two temperatures → true if one < 0 AND


other > 100
Program

print "Enter temperature 1: "


t1 = gets.to_i
print "Enter temperature 2: "
t2 = gets.to_i

if (t1 < 0 && t2 > 100) || (t2 < 0 && t1 > 100)
puts true
else
puts false
end

Sample Input

-5
120

Output

true

---

✅ 9. Ruby script: print elements of a given array

Program

arr = [10, 20, 30, 40, 50]

puts "Array elements:"


[Link] do |x|
puts x
end

Output

Array elements:
10
20
30
40
50

---

✅ 10. Ruby program: retrieve total marks from a hash (subject →


marks)

Program

marks = {
"Math" => 85,
"Science" => 90,
"English" => 78
}

total = [Link]
puts "Total Marks = #{total}"

Output

Total Marks = 253

11. TCL script to find factorial of a number

Program

puts -nonewline "Enter a number: "


flush stdout
set n [gets stdin]

set fact 1
for {set i 1} {$i <= $n} {incr i} {
set fact [expr $fact * $i]
}

puts "Factorial = $fact"

Sample Input

5
Output

Factorial = 120

---

✅ 12. TCL script that multiplies the numbers from 1 to 10

Program

set product 1
for {set i 1} {$i <= 10} {incr i} {
set product [expr $product * $i]
}
puts "Product = $product"

Output

Product = 3628800

---

✅ 13. TCL script for sorting a list using comparison

Program

set list {5 1 4 2 3}

set sorted [lsort -integer $list]

puts "Sorted List: $sorted"

Output

Sorted List: 1 2 3 4 5

---

✅ 14. TCL script to:

(i) create a list


(ii) append elements
(iii) traverse
(iv) concatenate lists

Program

# (i) Create
set list1 {10 20 30}

# (ii) Append
lappend list1 40 50

# (iii) Traverse
puts "Traversing list:"
foreach x $list1 {
puts $x
}

# (iv) Concatenate
set list2 {60 70}
set result [concat $list1 $list2]

puts "Concatenated List: $result"

Output

Traversing list:
10
20
30
40
50
Concatenated List: 10 20 30 40 50 60 70

---

✅ 15. TCL script comparing file modified time

Program

set file1 "[Link]"


set file2 "[Link]"

set time1 [file mtime $file1]


set time2 [file mtime $file2]

if {$time1 > $time2} {


puts "$file1 is modified later"
} else {
puts "$file2 is modified later"
}

Sample Output

[Link] is modified later

---

✅ 16. TCL script to copy a file & translate to native format

Program

set inFile "[Link]"


set outFile "[Link]"

set fin [open $inFile r]


set fout [open $outFile w]

fconfigure $fout -translation native

puts $fout [read $fin]

close $fin
close $fout

puts "File copied successfully"

Output

File copied successfully

---
⭐ PERL PROGRAMS (17 to 20)

---

✅ 17(a). Perl script to find largest among three numbers

Program

print "Enter three numbers: ";


$a = <STDIN>;
$b = <STDIN>;
$c = <STDIN>;

$max = $a;

$max = $b if ($b > $max);


$max = $c if ($c > $max);

print "Largest = $max\n";

Sample Input

10
25
18

Output

Largest = 25

---

✅ 17(b). Perl script to print multiplication tables (1–10) using


subroutines

Program

sub table {
my $n = shift;
for ($i=1; $i<=10; $i++) {
print "$n x $i = ", $n*$i, "\n";
}
print "\n";
}

for ($n=1; $n<=10; $n++) {


table($n);
}

Output (sample from table 2)

2x1=2
2x2=4
...
2 x 10 = 20

---

✅ 18. Perl script for list manipulation

Operations:
a) Shift
b) Unshift
c) Push

Program

@list = (10, 20, 30);

$a = shift(@list); # removes first


print "After shift: @list\n";

unshift(@list, 5); # adds to beginning


print "After unshift: @list\n";

push(@list, 40); # adds to end


print "After push: @list\n";

Output

After shift: 20 30
After unshift: 5 20 30
After push: 5 20 30 40

---

✅ 19(a). Perl script: substitute a word with another word

Program

$sentence = "I love Ruby programming";


$sentence =~ s/Ruby/Python/;

print "$sentence\n";

Output

I love Python programming

---

✅ 19(b). Perl script: validate IP & email

Program

print "Enter IP: ";


$ip = <STDIN>;
chomp($ip);

if ($ip =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
print "Valid IP\n";
} else {
print "Invalid IP\n";
}

print "Enter Email: ";


$email = <STDIN>;
chomp($email);

if ($email =~ /^[a-zA-Z0-9._]+@[a-zA-Z]+\.[a-zA-Z]+$/) {
print "Valid Email\n";
} else {
print "Invalid Email\n";
}

Output

Valid IP
Valid Email

---

✅ 20. Perl script: print file in reverse order (command line)

Program

$file = $ARGV[0];

open(FH, $file) or die "Cannot open file";

@lines = <FH>;
close FH;

@rev = reverse(@lines);
print @rev;

Run in terminal

perl [Link] [Link]

Output (example)

Line3
Line2
Line1

You might also like