# Perl Hashes
#create first hash
%countries =
(
Cyprus => "Cypriot",
France => "French",
England => "English",
Spain => "Spanish",
Germany => "German",
China => "Chinese"
);
#create second hash
%numbers =
(
num1 => 10, num2 => 8, num3 => 5, num4 => 9, num5 =>1
);
#print hash element
for(keys %countries)
{
print($countries{$_});
print("\n");
}
print "\n\n";
for(keys %numbers)
{
print($numbers{$_});
print("\n");
}
print "\n\n";
#return a hash value
print($countries{Spain});
print "\n\n";
print($countries{China});
print "\n\n";
print($numbers{num4});
print "\n\n";
print($numbers{num1});
print "\n\n";
#print sorted items
for(sort keys %numbers)
{
print($numbers{$_});
print("\n");
}
print "\n\n";
#add a new element
$countries{Italy} = "Italian";
for(sort keys %countries)
{
print($countries{$_});
print("\n");
}
print "\n\n";
$numbers{num6} = 76;
for(sort keys %numbers)
{
print($numbers{$_});
print("\n");
}
////////////////////////////////////////////
2
////////////////////////////////////////////
#File Processing
#open file
open(FILE1, "File1.txt") or die("Cannot open file1");
#copy the contents of the file
@input1 = <FILE1>;
#close file
close(FILE1)or die("Cannot open File1");
#print contents of file
print @input1, "\n";
////////////////////////////////////////////
3
////////////////////////////////////////////
#File processing 2
#open file1
open(FILE1, "File1.txt") or die("Cannot open file1");
#copy countents of File1
@input = <FILE1>;
#close file1
close(FILE1)or die("Cannot open File1");
#close file2
open(FILE2, ">", "File2.txt") or die ("Cannot creat File2");
#copy contents in file2
for(@input)
{
print(FILE2 $_);
}
#close file2
close(FILE2) or die ("Cannot open File2");
////////////////////////////////////////////
4
////////////////////////////////////////////
#File processing 3
#open File
open(FILE1, "File1.txt") or die("Cannot open file1");
#copy contents of file1
@in = <FILE1>;
#close file1
close(FILE1)or die("Cannot open File1");
#creat File 3
open(FILE3, ">", "File3.txt") or die ("Cannot creat File3");
#copy reverse countents in file3
foreach(reverse(@in))
{
$out = reverse($_);
print(FILE3 $out, "\n");
}
#close file3
close(FILE3) or die ("Cannot open File3");
////////////////////////////////////////////
5
////////////////////////////////////////////
#File processing 4
#open File
open(FILE1, "File1.txt") or die("Cannot open file1");
#copy contents of file1
@in = <FILE1>;
#close file1
close(FILE1)or die("Cannot open File1");
#creat File 4
open(FILE4, ">", "File4.txt") or die ("Cannot creat File3");
#copy reverse countents in file4
foreach(@in)
{
$out = $_ * 2 + 5 / 6 * 27 ;
print(FILE4 $out, "\n");
}
#close file4
close(FILE4) or die ("Cannot open File4");
No comments
Post a Comment