0% found this document useful (0 votes)
200 views24 pages

Scrpting Langaugae

The document contains code snippets in Ruby, TCL, Perl, and Python. It provides 20 examples of small scripts or programs that demonstrate various programming concepts like string manipulation, user input, conditional statements, loops, functions, lists, files etc. Each code example is accompanied by sample input/output to show the expected behavior.

Uploaded by

Nithesh
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)
200 views24 pages

Scrpting Langaugae

The document contains code snippets in Ruby, TCL, Perl, and Python. It provides 20 examples of small scripts or programs that demonstrate various programming concepts like string manipulation, user input, conditional statements, loops, functions, lists, files etc. Each code example is accompanied by sample input/output to show the expected behavior.

Uploaded by

Nithesh
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

1.

Write a Ruby script to create a new string which is n copies of a given


string where n
is a nonnegative integer

str = "a"
puts str*1
puts str*2
puts str*3
puts str*4
puts str*5

Output:

a
aa
aaa
aaaa
aaaaa
2. Write a Ruby script which accept the radius of a circle from the user
and compute
the parameter and area

perimeter = 0.0;
area = 0.0;
puts "Enter the radius:"
r = gets.to_f
perimeter = 2*(22/7)*r
area = (22/7)*r*r;
puts "perimeter: #{perimeter}"
puts "Area: #{area}"

Output:

Enter the radius:


3
perimeter: 18.0
Area: 27.0
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

print "Enter the first name:"


fname = gets.chomp
print "Enter the last name:"
lname = gets.chomp
RevName = "#{lname} #{fname}"
puts "Your name in reverse Order is: #{RevName}"

Output:

Enter the first name:Twinkle


Enter the last name:Sharma
Your name in reverse Order is: Sharma Twinkle
4.Write a Ruby script to accept a filename from the user print the
extension of that

print "Enter the FileName: "


filename = gets.chomp
extension = File.extname(filename)
puts "The extension of the file is: #{extension}"

Output:

Enter the FileName: hacker.rb


The extension of the file is: .rb
5.Write a Ruby script to find the greatest of three numbers

print "Enter the first number:"


num1 = gets.chomp.to_i
print "Enter the second number:"
num2 = gets.chomp.to_i
print "Enter the third number:"
num3 = gets.chomp.to_i

if num1 > num2 && num1 > num3


puts "The greatest num is #{num1}"
elsif num2 > num1 && num2 > num3
puts "The greatest num is #{num2}"
else
puts "The greatest num is #{num3}"
end

Output:

Enter the first number:10


Enter the second number:20
Enter the third number:39
The greatest num is 30
6. Write a Ruby script to print odd numbers from 10 to 1

num = 10
while num >= 1
if num%2 == 1
puts num
end
num -= 1
end

Output :

9
7
5
3
1
7. Write a Ruby script to check two integers and return true if one of
them is
20 otherwise return their sum

print "Enter the first Integer: "


num1 = gets.chomp.to_i
print "Enter the second Integer: "
num2 = gets.chomp.to_i

if num1 == 20 || num2 == 20
puts true
else
puts num1 + num2
end

Output :

Enter the first Integer: 10


Enter the second Integer: 10
20
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

print "Enter the first temperature: "


temp1 = gets.chomp.to_f
print "Enter the second temperature: "
temp2 = gets.chomp.to_f
if (temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0)
puts true
else
puts false
end

Output:

Enter the first temperature: -1


Enter the second temperature: 101
true
9. Write a Ruby script to print the elements of a given array

arr = [1, 2, 3, 4, 5]
arr.each do |ele|
puts ele
end

Output:

1
2
3
4
5
10. Write a Ruby program to retrieve the total marks where subject name
and
marks of a student stored in a hash

marks = {
"Maths" => 80,
"Science" => 90,
"English" => 75
}
total_marks = 0
marks.each do |subject, mark|
total_marks += mark
end
puts "Total Marks: #{total_marks}"

Output:
Total Marks: 245
11. Write a TCL script to find the factorial of a number

proc Factorial {x} {


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

Output:
3628800
12. Write a TCL script that multiplies the numbers from 1 to 10

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


13. Write a TCL script for Sorting a list using a comparison function

proc compare {a b} {
if {$a < $b} {
return -1
} elseif {$a > $b} {
return 1
} else {
return 0 }}

set mylist {4 2 7 1 9 3}
set sorted_list [lsort -command compare $mylist]
puts "Sorted List: $sorted_list”

Output:

Sorted List: 1 2 3 4 7 9
14. Write a TCL script to
(i)create a list (ii) append elements to the list (iii)Traverse the list
(iv)Concatenate the list

set mylist {apple banana cherry} # Create a list

lappend mylist durian elderberry # Append elements to the list

# Traverse the list


foreach element $mylist {
puts $element }

# Concatenate the list


set concatenated_list [concat $mylist {fig grape}]

# Print the concatenated list


puts "Concatenated List: $concatenated_list

Output:

Concatenated List: apple banana cherry durian elderberry fig grape


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

set file1 [lindex $argv 0]


set file2 [lindex $argv 1]

set mod_time_file1 [file mtime $file1]


set mod_time_file2 [file mtime $file2]

if {$mod_time_file1 > $mod_time_file2} {


puts "$file1 was modified more recently than $file2."
} elseif {$mod_time_file1 < $mod_time_file2} {
puts "$file2 was modified more recently than $file1."
} else {
puts "Both files were modified at the same time."
}

Run:

$ tclsh compare-modified-times.tcl file1.txt file2.txt

Output:

file2.txt was modified more recently than file1.txt.


16. Write a TCL script to Copy a file and translate to native format.

#!/usr/bin/tclsh

# Define the source and destination files


set source_file [lindex $argv 0]
set dest_file [lindex $argv 1]

# Set the file type based on the platform


if {$::tcl_platform(platform) eq "windows"} {
set file_type "-type binary"
} else {
set file_type "-type text"
}

# Copy the file and translate to native format


file copy -force $source_file $dest_file
file translate $file_type $dest_file

run:

tclsh copy-translate.tcl source.txt destination.txt

Output:

The content in the source file will copy to the destination file
17.a) Write a Perl script to find the largest number among three numbers.

#!/usr/bin/perl
print "enter a value";
$a=<stdin>;
print "enter b value";
$b=<stdin>;
print "enter c value";
$c=<stdin>;
if($a > $b)
{
if($a> $c)
{
print " $a is largest number\n";
}
else
{
print " $c is largest number\n";
}
}
elsif($b >$c)
{
print " $b is largest number\n";
}
else
{
print " $c is largest number\n";
}

Output:

enter a value10
enter b value20
enter c value30
30
is largest number

b) Write a Perl script to print the multiplication tables from 1-10 using
subroutines.

print "multiplication tables from 1-10 using subroutines\n";


&table(1);
&table(2);
&table(3);
&table(4);
&table(5);
&table(6);
&table(7);
&table(8);
&table(9);
&table(10);

sub table{
my $i = 1;
my $loop;

foreach $loop(@_){
for($i;$i<=10;$i++){
my $ans = $i*$loop;
print"$loop*$i=$ans \n";
}
print"\n";
}
}

Output:

multiplication tables from 1-10 using subroutines


1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30

4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60

7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70

8*1=8
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
8*10=80

9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90

10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
18. Write a Perl program to implement the following list of manipulating
functions
a) Shift

#!/usr/bin/perl

# Initializing the array


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

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

b) Unshift

#!/usr/bin/perl

# Initializing the array


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

# Print the Initial 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++
c) Push

#!/usr/bin/perl

# Initializing the array


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

# Print the Initial 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
19. a) Write a Perl script to substitute a word, with another word in a
string.

#!/usr/bin/perl -w

# String in which text


# is to be replaced
$string = "GeeksforGeeks";

# Use of s operator to replace


# text with pattern
$string =~ s/for/to/;

# Printing the updated string


print "$string\n";

Output:

GeekstoGeeks

b) Write a Perl script to validate IP address and email address.

#!/usr/bin/perl
# Define the IP address and email address to validate
my $ip = "192.168.1";
my $email = "j.com";
# Validate the IP address using a regular expression
if ($ip =~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/) {
print "$ip is a valid IP address.\n";
} else {
print "$ip is not a valid IP address.\n";
}
# Validate the email address using a regular expression
if ($email =~ /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) {
print "$email is a valid email address.\n";
} else {
print "$email is not a valid email address.\n";
}

Output:

192.168.1 is not a valid IP address.


j.com is not a valid email address.
20. Write a Perl script to print the file in reverse order using command
line arguments

#!/usr/bin/perl
use strict;
use warnings;

my $filename = shift @ARGV; # Get filename from command line arguments


open(my $fh, '<', $filename) or die "Can't open file '$filename': $!";

# Read the file into an array, one line per element


my @lines = <$fh>;
close $fh;

# Print the lines in reverse order


print reverse @lines;

run:

perl reverse_file.pl filename.txt

filename.txt contains

1
2
3
4
5

then output will be:

5
4
3
2
1

You might also like