Sei sulla pagina 1di 4

# 5.

## Create regular expressions that:

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

CODE:

#a) Ans.
print "Qus. a)\n";
\$float_no="5.9745";
if (\$float_no=~/\d*\.\d*/)
{
print "\$float_no is a floating point number\n";
}
else
{
print "\$float_no is not a floating point number\n";
}

#b) Ans.
print "\nQus. b)\n";
\$string="45 67";
print "Old String: \$string\n";
@array=split(' ',\$string);
\$temp=\$array[0];
\$array[0]=\$array[1];
\$array[1]=\$temp;
\$new_string=join(' ',@array);
print "Reversed String: \$new_string\n\n";

OUTPUT:
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:

#a) This qus has some errors. And the correct code is,
set a 4
set b 5
puts \$a
puts \$b
if {\$b>\$a} {
puts "OK"
}

#Output:
4
5
OK

#b) This qus has some errors. And the correct code is,
list a {b c} e d

lreplace \$x 1 2 B C

lrange \$x 1 3

lindex \$x 2

#This will not execute because x hold some char value and that can't
be incramented by 10 i.e. any number.
incr x 10

#Output:
a {b c} e d
a B C d
{b c} e d
e
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:

## open IN,"<\$in" or die "Error opening file \$!";

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

\$size=@content;
\$j=0;
\$k=0;
for(\$i=0;\$i<=\$size;\$i++)
{
\$temp=@content[\$i];
if(\$temp=~/^\s+\$/)
{
\$j++;
}
else
{
@new[\$k]=@content[\$i];
\$k++;
}
}

## open OUT,">\$out" or die "Error creating file \$!";

foreach \$text(@new)
{
print OUT "\$text";
}
close (OUT);

OUTPUT: