Sei sulla pagina 1di 13

TATHAGATA DE 17MVD0090 CAT–1

SOLUTION

1. Develop a Perl script that sorts lines of the given file in reverse order, so the last line becomes
first and vice-versa. The script must have two command line arguments, the first is an input file
name and the second is an output file name. For input file check to have existing and readable
file, otherwise print error and exit program. For output file check to have non-existing file,
otherwise print error and exit.

CODE:

1 $in=@ARGV[0];
2 $out=@ARGV[1];
3 open IN,"<$in" or die "Error Opening Input file: $!";
4 while (<IN>)
5 {
6 push (@content, $_);
7 }
8 close (IN);
9
10 print "Original Lines:\n";
11 print "@content\n";
12 $j=0;
13 for ($i=$#content; $i>=0; $i--)
14 {
15
16 $new_content[$j]=$content[$i];
17 $j++;
18 }
19 print "\n";
20
21 print "Reversed Lines:\n";
22 open OUT, ">$out" or die "Error opening the file. $!";
23 print OUT @new_content;
24 print @new_content;
25 close (OUT);

OUTPUT:

1
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

2. Write a Perl script which will read a mathematical sentence from the user, evaluate the result, and print
the result to the screen. For example, if the user types 2 + 4 = the program will output 6. All formulas will
be given in the format Operand_1 Operator Operand_2 = The characters in the formulas may or may not
be separated by whitespace. The operators that must be supported are: '+', '-', '/', '*', '%'. The values of
the operands may be negative (in which case the minus sign will be immediately in front of the operand,
with no spaces). Any whole numbers should be supported as an operand.

CODE:

1 ($in)=@ARGV;
2 $in=~/(.)(\d*)([-+\/*%])(.)(\d*)/;
3 $a=$1;
4 $b=$2;
5 $c=$3;
6 $d=$4;
7 $e=$5;
8 $x=$a.$b;
9 $y=$d.$e;
10
11 if ($c eq "+")
12 {
13 $result=$x+$y;
14 }
15 if ($c eq "-")
16 {
17 $result=$x-$y;
18 }
19 if ($c eq "/")
20 {
21 $result=$x/$y;
22 }
23 if ($c eq "*")
24 {
25 $result=$x*$y;
26 }
27 if ($c eq "%")
28 {
29 $result=$x%$y;
30 }
31 print "Result: $result\n";

OUTPUT:

2
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

3. Assume you need to generate sufficient data to test your design. Write a data generator in
Perl. It should take the description of the desired data format as its input and generate a set of
random data conforming to that format.

CODE:
1 print "How many i/p does your Design have: ";
2 chomp ($ip_no=<STDIN>);
3 print "How many set of data you want to genarate: ";
4 chomp ($set_no=<STDIN>);
5
6 for ($i=1;$i<=$ip_no;$i++)
7 {
8 print "\nPlease enter the no. $i input's name: ";
9 chomp ($ip_name[$i]=<STDIN>);
10 print "\nPlease enter the no. $i input's size(In Bits): ";
11 chomp ($ip_size[$i]=<STDIN>);
12 }
13
14 print "\nRandom value are:\n";
15 for ($j=1;$j<=$#ip_size;$j++)
16 {
17 $temp = ( 2 ** $ip_size[$j] );
18 for ($k=0;$k<=$set_no;$k++)
19 {
20 $random_number = rand($temp);
21 printf "@ip_name[$j]\[@ip_size[$j]\]: %b\n", $random_number;
22 }
23 }

OUTPUT:

3
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

4. Write a Perl program so that when it is executed, it should get the information like names and marks of "N" number
of students from the user through the standard input. Value of N (Number of students) should be passed through
command line argument. Make an associative array that has a list of all the names of the students and their marks.
Also write a subroutine so that it calculates the average marks of all the entries in the associative array.

CODE:

1 ($in)=@ARGV[0];
2 for ($i=0;$i<$in;$i=$i+1)
3 {
4 $no=$i+1;
5 print "Please enter No.$no student's name: ";
6 chomp($name=<STDIN>);
7 print "Please enter $name 's Mark: ";
8 chomp($mark=<STDIN>);
9 $students{$name}=$mark;
10 }
11
12 @names=keys%students;
13 @marks=values%students;
14
15 print "\n\nNAMES\t\tMARKS\n---------------------\n";
16
17 for ($j=0;$j<$in;$j=$j+1)
18 {
19 print "@names[$j]\t\t@marks[$j]\n";
20 }
21 print "\n";
22 avg_marks();
23 print "\n";
24
25 sub avg_marks
26 {
27 $sum=0;
28 for ($i=0;$i<$in;$i=$i+1)
29 {
30 $value = @marks[$i];
31 $sum=$sum+$value;
32 }
33 $avg=$sum/$in;
34 printf "Avg marks of all students is: %.2f\n",$avg;
35 }

OUTPUT:

4
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

5. Create regular expressions that:


a) Match any floating-point number.
b) Swap the two words of the following string "45 67".

CODE:

1 #a) Ans.
2 print "Qus. a)\n";
3 $float_no="5.9745";
4 if ($float_no=~/\d*\.\d*/)
5 {
6 print "$float_no is a floating point number\n";
7 }
8 else
9 {
10 print "$float_no is not a floating point number\n";
11 }
12
13 #b) Ans.
14 print "\nQus. b)\n";
15 $string="45 67";
16 print "Old String: $string\n";
17 @array=split(' ',$string);
18 $temp=$array[0];
19 $array[0]=$array[1];
20 $array[1]=$temp;
21 $new_string=join(' ',@array);
22 print "Reversed String: $new_string\n\n";

OUTPUT:

5
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

6. Does the Tcl scripts given below has any syntax errors? If your answer is yes, then write down
the correct code with the corresponding output. If your answer is no, then write down the output
of the below scripts?

a)
set a 4 , set b 5; puts $a $b
if{$b>$a}{puts OK}
b)
set x [list a {b c} e d]lreplace $x 1 2 B C
lrange $x 1 3
lindex #x 2
lincrement $x 10

CODE:

1 #a) This qus has some errors. And the correct code is,
2 set a 4
3 set b 5
4 puts $a
5 puts $b
6 if {$b>$a} {
7 puts "OK"
8 }
9
#Output:
4
5
OK

10 #b) This qus has some errors. And the correct code is,
11 list a {b c} e d
12
13 lreplace $x 1 2 B C
14
15 lrange $x 1 3
16
17 lindex $x 2
18
19 #This will not execute because x hold some char value and that can't
20 be incramented by 10 i.e. any number.
21 incr x 10
22
#Output:
a {b c} e d
a B C d
{b c} e d
e

6
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

7. Develop a Perl script that deletes all empty lines of the given file. Lines are considered empty if one can’t
see anything, but it may contain spaces. The script must have two command line arguments, the first is an
input file name and the second is an output file name. For input file check to have existing and readable
file, otherwise print error and exit program. For output file check to have non-existing file, otherwise print
error and exit.

CODE:

1 ($in, $out) = @ARGV;


2
3 open IN,"<$in" or die "Error opening file $!";
4
5 while (<IN>)
6 {
7 push(@content,$_);
8 }
9 close (IN);
10
11 $size=@content;
12 $j=0;
13 $k=0;
14 for($i=0;$i<=$size;$i++)
15 {
16 $temp=@content[$i];
17 if($temp=~/^\s+$/)
18 {
19 $j++;
20 }
21 else
22 {
23 @new[$k]=@content[$i];
24 $k++;
25 }
26 }
27
28 open OUT,">$out" or die "Error creating file $!";
29 foreach $text(@new)
30 {
31 print OUT "$text";
32 }
33 close (OUT);

OUTPUT:

7
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

8. Develop two Perl functions. The first function must take a list of elements as an argument and check if there are any
duplicate elements in the list. And the second must delete duplicate elements from the list.

CODE:

1 print "How many elements you want to add: ";


2 chomp ($no=(<STDIN>));
3
4 for ($i=0;$i<$no;$i++)
5 {
6 print "Enter an element: ";
7 chomp ($text=(<STDIN>));
8 @content[$i]=$text;
9 }
10 @data=list(@content);
11 @new_data=del(@content);
12
13 print "List without any dulplicate items: @new_data\n";
14
15 sub list()
16 {
17 @data=@_;
18 $size=@data;
19 $i=0;
20 for($j=0;$j<$size;$j++)
21 {
22 for($k=$j+1;$k<$size;$k++)
23 {
24 if(@data[$j]==@data[$k])
25 {
26 @duplicate[$i]= @data[$j];
27 $i++;
28 }
29 }
30 }
31 print "Dupliacte items are: @duplicate\n";
32 }
33
34 sub del ()
35 {
36 @data=@_;
37 $size=@data;
38 for($j=0;$j<$size;$j++)
39 {
40 for($k=$j+1;$k<$size;$k++)
41 {
42 if(@data[$j]==@data[$k])
43 {
44 delete (@data[$j]);
45 }
46 }
47 }
48 return @data;
49
50 }

OUTPUT:

8
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

9. Write a Perl script that prints the count of all the cells used in the given module of a netlist in
a report file. The script must get two arguments. First is the name of the netlist and the second
is the name of module for which to determine the cell counts. Script must generate a log file
named "netlist_report.log"

CODE:

1 ($module, $netlist_name) = @ARGV;


2
3 open IN, "<$netlist_name" or die "Error opening the file $!";
4 while (<IN>)
5 {
6 push (@text,$_);
7 }
8 close (IN);
9
10 $lines=0;
11 $size=@text;
12 #print "$size\n";
13 for($i=0;$i<=$size;$i++)
14 {
15 if($text[$i]=~/^module\s*$module\s*\(/)
16 {
17 $end=0;
18 $cell_count=0;
19 for($j=$i+1;$j<=$size;$j++)
20 {
21
22 if($text[$j]=~/^\s*(\w+)\s\w+\s\(\s*\./)
23 {
24 $cell_count++;
25 }
26
27 if($text[$j]=~/^endmodule/)
28 {
29 last;
30 }
31
32 }
33 }
34 }
35 print "Total Cell count in the Module \"$module\": $cell_count\n";
36
37 open OUT, ">d:/netlist_report.log" or die "Error creating file $!";
38 print OUT "Total Cell count in the Module \"$module\": $cell_count";
39 close (OUT);

OUTPUT:

9
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

10. Write a Perl programme that creates the skeleton of a test suite such that the directory
structure should be as in fig. 1. Also assume you need to copy two different #.v files residing in
the current working directory to test each test directory. Prompt the user to enter the number of
test directories to be created through the standard input. Pass the names of the #.v files that need
to be copied to each test directory as the command line argument.

CODE:

@v_files=@ARGV;

use Cwd;
$cur_dir = getcwd;

$temp="/";
$in_file_1=$cur_dir.$temp.$v_files[0];
$in_file_2=$cur_dir.$temp.$v_files[1];

open IN,"<$in_file_1" or die "Error opening the file. $!";


while (<IN>)
{
push (@data_1,$_);
}
close (IN);

open IN1,"<$in_file_2" or die "Error opening the file. $!";


while (<IN1>)
{
push (@data_2,$_);
}
close (IN1);

$temp2="/Testcases/";
$dir=$cur_dir.$temp2;
mkdir $dir;
chdir($dir) or die "Can't chdir to c:\\mnt : $!";

print "How many test directories you want to create: ";


chomp ($dir_no=<STDIN>);
for ($i=1;$i<=$dir_no;$i++)
{
$temp1="Test";
$dir_name=$temp1.$i;
mkdir $dir_name;
$path=$dir.$dir_name;
$name1="/$v_files[0]";
$name2="/$v_files[1]";
$path1=$path.$name1;
$path2=$path.$name2;

10
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

open OUT1,">$path1" or die "Error creating the file. $!";


print OUT1 @data_1;
close (OUT1);
open OUT2,">$path2" or die "Error creating the file. $!";
print OUT2 @data_2;
close (OUT2);
}
print "Check $dir\n";

OUTPUT:

11
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

11. Write a Perl script to create an array that contains the names of 10 students of a class. Write
a Perl script to create an array that contains the names of 10 students of a class. Print the array.
Add that names at the end and beginning of array. Print the array. Ask user to input a number.
Print the name that has that number as index. Remove last and starting element of array. Print
the array in alphabetical and in reverse order. Print the array in alphabetical and in reverse
order.

CODE:

1 @students=qw(A B C D E F G H I J);
2
3 for($i=0;$i<10;$i++)
4 {
5 push(@students,@students[$i]);
6 }
7 print "Original Array: @students\n";
8
9 unshift(@students,@students);
10
11 print "New Array: @students\n";
12 print "Enter a number(0-10): ";
13 chomp ($no=(<STDIN>));
14 if($no<10 && $no>=0)
15 {
16 print "Value at $no index no: @students[$no]\n";
17 }
18 else
19 {
20 print "Wrong ip\n";
21 }
22 pop(@students);
23 print "Removed last element: @students\n";
24 shift (@students);
25 print "Removed starting element: @students\n";
26 @sorted=sort(@students);
27 print "Sorted list: @sorted\n";
28 @rev=reverse(@students);
29 print "Reversed list: @rev\n";

OUTPUT:

12
TATHAGATA DE 17MVD0090 CAT–1
SOLUTION

12. Describe the operation and the result in TCL.


set numbers {1 2 3 4 5 6 7 8 9}
set val [expr [join $numbers *]]
set numbers [lreplace $numbers 3 3]
set p [split /usr/loca/bin /]
set path [join {usr local bin} /n]

Answer:

set numbers {1 2 3 4 5 6 7 8 9}
This will store the values 1 to 9 in a list named ‘numbers’.
#Output:
1 2 3 4 5 6 7 8 9

set val [expr [join $numbers *]]


This will glue together all the items of the list ‘numbers’
with ‘*’ i.e. multiplication. And due to ‘expr’ we eill get the
multiplied result.
#Output:
362880

set numbers [lreplace $numbers 3 3]


This will replace the 3rd element of the list ‘numbers’ i.e. 4
by ‘null value’.
#Output:
1 2 3 5 6 7 8 9

set p [split /usr/loca/bin /]


This will split the string from every ‘/’.
#Output:
{} usr loca bin

set path [join {usr local bin} /n]


This will glue the words with ‘/n’ into a string.
#Output:
usr/nlocal/nbin

13

Potrebbero piacerti anche