Wednesday, April 20, 2016

week 1

////////////////////////////////////////////
2
////////////////////////////////////////////
#mathematical calculations


print "Enter the number: ";
$first = <STDIN>;


print "Enter anthor number: ";
$second = <stdin>;


$sum = $first + $second;
$sub = $first - $second;
$mul = $first * $second;
$div = $first / $second;


print "The sum is: ", $sum, "\n";
print "The sub is: ", $sub, "\n";
print "The sub is: ", $mul, "\n";
print "The sub is: ", $div, "\n";

////////////////////////////////////////////
3
////////////////////////////////////////////
#alphabetical order
#A-Z, a-z, for the same word

print "Enter a word: ";
$word1 = <stdin>;


print "Enter another word: ";
$word2 = <stdin>;


if($word1 lt $word2)
{
print " The first word is less than the second";
}
if($word1 gt $word2)
{
print " The first word is greater than the second";
}
if($word1 eq $word2)
{
print " The two word are the same\n";
}

////////////////////////////////////////////
4
////////////////////////////////////////////

#comparing numbers

print "Enter 1st number: ";
$num1 = <stdin>;
chomp($num1);

print "Enter 2nd number: ";
$num2 = <stdin>;
chomp($num2);

print "The value of the 1st number is: ", $num1, "\n";
print "The value of the 1st number is: ", $num2, "\n";


if($num1 < $num2){
print "The first number is less than second\n";
}
if($num1 > $num2){
print "The first number is greater than second\n";
}
if($num1 == $num2){
print "The two numbers are equal\n";
}

////////////////////////////////////////////
5
////////////////////////////////////////////

#bilt-in functions 1

print "Enter your name: ";
$name = <stdin>;
chomp($name);

print $name, "\n";
print uc($name), "\n";
print lc($name), "\n";
print length($name), "\n";



////////////////////////////////////////////
6
////////////////////////////////////////////
#built-in functions 2

#index
$sub = "is";
$sentence = "Learning perl is easy";
$p = index($sentence, $sub);

print "The word ", $sub , " is found at postion ", $p , "\n"; 

#substring
$s = "Green is my favourite color";
$p2 = substr($s,12, 9);
print $p2, "\n";

#replace substring
$s2 = "Green is my favourite color";
substr($s2, 9, 12, "the worst");
print $s2, "\n";

////////////////////////////////////////////
7
////////////////////////////////////////////

#perl list

#simple list 
print(10, " ", 20, " ", 30, "\n");
print "This ", "is", " another ", "list ", "\n";
print 10, " hello ", 14.5, " " , "c", "\n";
#complex list
$n1 = 10;
$n2 = "Hello";
$n3 = 12.3;
$n4 = 't';

print $n1, " " , $n2, " ", $n3, " " , $n4, "\n";

#specific elements from a list
print((10, 22, 34) [2]);
print "\n";

print((5,3,7,2,9) [4,1,3]);


////////////////////////////////////////////
8
////////////////////////////////////////////


#Perl Arrays

@days = (" Mon ", " Tue " , " Wed " , " Thu " , " Fri " , " Sat " , " Sun ");
print @days, "\n";

print @days[2], "\n";

print @days[3,5], "\n";

print scalar(@days), "\n";

print sort(@days), "\n";

@numbers = (7, 1, 4, 9, 5, 2);
print @numbers , "\n";
print sort(@numbers), "\n";



No comments

Post a Comment

follow me

All Rights Reserved by personal © 2015 - 2016
Powered By Blogger, Designed by Mohammed ARG