############################################################################################################
#Author: Leo
#Email: ahyeek@gmail.com
#Desc:
# The author has created some codes to test out during the learning process for the book called
# <Professional Perl Programming>, publisher: Wrox Pree Ltd, year in 2001
# ISBN: 1-861004-49-4.
#
#Summary:
# Examples code for <Professional Perl Programming>.
# Chapter 5: Beyond Scalars - More Data Types
#
############################################################################################################
#!/usr/bin/perl
use warnings;
use strict;
######################################################
# List and Arrays.
######################################################
print "\nList and Arrays.\n";
my @array = (1, 2, 3, 4, 5, 6);
@array[2..4] = @array[0..2];
print "Array value = @array \n";
######################################################
# Replace element number three with three new elements
# to create eight elements list.
######################################################
print "\nReplace element number...\n";
my @arr1 = ('a', 'b', 'c', 'd', 'e');
my $removed = splice @arr1, 2, 1, (1, 2, 3);
print "New array = @arr1\n";
print "Removed element = $removed \n";
my @removed = splice @arr1, 2, 3;
print "After removing 3 elements from no.2 position = @arr1\n";
print "Removed array = @removed \n";
my @my_next_last_3_elements = splice @arr1, -3;
print "Removing last 3 elements = @arr1\n";
print "Last 3 removed array = @my_next_last_3_elements \n";
######################################################
# Counting an array...
######################################################
print "\nCounting an array...\n";
my @countarray = (11, 22, 33, 44, 55, 66, 77, 88);
my $last_element = (11, 22, 33, 44, 55, 66, 77, 88);
print "Caution! Assigned array directly to a scalar is getting the last element: $last_element \n";
my $num_of_element = scalar(@countarray);
print "The number of element in array: $num_of_element \n";
print "Getting index for each elements...\n";
foreach(0..$#countarray)
{
print "Element number $_ contains $countarray[$_]\n";
}
######################################################
# Ways of adding element to an array...
######################################################
print "\nWays of adding element to an array...\n";
my @addarray = ('aa', 'bb', 'cc');
print "Original array = @addarray\n";
$addarray[3] = "dd";
print "After add 1 element to the array = @addarray\n";
$addarray[scalar(@addarray)] = "ee";
print "After add another element to the array = @addarray\n";
push @addarray, "ff";
print "After add another element to the array = @addarray\n";
######################################################
# Truncating an array...
######################################################
print "\nTruncating an array...\n";
print "Array with elements number = " . scalar(@addarray) . "\n";
print "Truncating the array to 1 element...\n";
$#addarray = 0;
print "Array after truncate = @addarray\n";
######################################################
# Removing element from array ...
######################################################
print "\nRemoving element from array...\n";
my @rmarray = ('bb', 'cc', 'dd');
unshift @rmarray, 'aa', '00', '-1-1'; #Add element in front of an array.
print "@rmarray\n";
shift @rmarray;
print "After removing 1st element from array...@rmarray\n";
shift @rmarray;
print "After removing 1st element again from array...@rmarray\n";
pop @rmarray;
print "After removing 1st element again from array from the end...@rmarray\n";
splice(@rmarray, -1);
print "After removing 1st element again from array from the end...@rmarray\n";
######################################################
# Destroying an array
######################################################
undef @rmarray;
print "Destroyed array ==> @rmarray\n";
######################################################
# Sorting & Reversing Lists and Arrays.
######################################################
print "\nSorting & Reversing Lists and Arrays.\n";
my @testarray = ('aa', 'bb', 'cc', 'dd');
@testarray = reverse @testarray;
print "Reversed array ==> @testarray\n";
@testarray = sort @testarray;
print "Sorted array ==> @testarray\n";
######################################################
# Converting Lists into Formatted Strings
######################################################
my @words = ('This', 'is', 'going', 'to', 'join.,,');
my $string = join ' ', @words;
print "Join array ==> $string\n";
$string = join ';', @words;
print "Join in other format ==> $string\n";
$string = join '', @words;
print "Join in other format ==> $string\n";
my @codes = (80, 101, 114, 108);
my $wordfromarr = pack 'C*', @codes;
print "Word from array ==> $wordfromarr\n";
my @sentences = ('Practical', 'extration', 'reporting', 'language');
my $first_letter_here = pack 'a' x @sentences, @sentences;
print "Extract first letter from every words in array --> $first_letter_here" . "\n";
######################################################
# Research on Data type: HASH
######################################################
print "\nResearch on Data type: HASH\n";
my %hash = ("Mouse" => "Jerry", "Cat" => "Tom", "Dog" => "Spike");
print "The mouse is " . $hash{"Mouse"} . "\n";
my @return_hash_value_in_array = @hash{'Mouse', 'Cat', 'Dog'};
print "Return hash value in array ==> @return_hash_value_in_array\n";
my @hash_keys = keys %hash;
print "Obtain keys value from a hash ==> @hash_keys\n";
print "Sorted keys are:";
print join(',', sort keys %hash);
print "Dump content of a hash:\n";
foreach (sort keys %hash){
print "$_ ==> $hash{$_} \n";
}
print "\nModifying hash value...\n";
$hash{'Cat'} = 'Sylvester';
$hash{'Bird'} = 'Tweety'; #If the key not exist in a hash table, new key will be added.
foreach (sort keys %hash){
print "$_ ==> $hash{$_} \n";
}
print "\nChange hash value by array...\n";
@hash{('Mouse', 'Dog')} = ('Tom', 'XiaoHuangGou');
foreach (sort keys %hash){
print "$_ ==> $hash{$_} \n";
}
print "\nRemoving a hash key...\n";
#undef $hash{'Bird'}; #Doing this only will create warning and error.
delete $hash{'Bird'};
foreach (sort keys %hash){
print "$_ ==> $hash{$_} \n";
}
print "\nConverting an Array to Hash key...\n";
my @tohash = ('key0', 'value0', 'key1', 'value1', 'key2', 'value2');
my %myhash = @tohash;
foreach (sort keys %myhash){
print "$_ ==> $myhash{$_} \n";
}
print "\nPerforming Key and Value reversing in myhash...\n";
print "Check ... The value for Key:Key0 in myhash is ==> $myhash{'key0'}\n";
foreach (keys %myhash)
{
$myhash{$myhash{$_}} = $_; #Inverting the key and value by inserting new key.
delete $myhash{$_}; #Remove original key.
}
print "Check ... The value for Key:Value0 in myhash is ==> $myhash{'value0'}\n";
print "\nGetting just the values in myhash...\n";
my @myhashvalues = values %myhash;
foreach(@myhashvalues)
{
print "Value got: $_\n";
}
print "\nGetting just the values in myhash in more proper and sorted way...\n";
foreach(sort values %myhash)
{
print "Value got: $_\n";
}
print "\nIterate through Hash...\n";
my %Vhash = ('K1' => 1, 'K2' => 2, 'K3' => 3, 'K4' => 4, 'K5' => 5);
while( my ($key, $value) = each %Vhash)
{
print "$key => $value \n";
$Vhash{$key}++;
}
######################################################
# Converting HASHES into SCALARS...
######################################################
print "\nConverting HASHES into SCALARS...\n";
my %hashdata = (one => 1, two => 2, three => 3, four => 4, five => 5);
my $elements = scalar(keys %hashdata);
print "Element data: $elements\n";
my $hashref = \%hashdata;
my $data = $hashref->{'one'};
print "Dereference element 'one' => $data\n";
if($data eq 1) { print "Data matched to 1 tested!\n"; }
if($hashref->{'one'} eq 1) { print "Data matched to 1 from direct ref tested!\n"; }
print "\nConvert hash data to array...\n";
my @hasharray = %hashdata;
foreach(@hasharray)
{
print "Array element from hash => $_\n";
}
print "\nConvert sorted hash data to array...\n";
my @sorthasharray = sort %hashdata;
foreach(@sorthasharray)
{
print "Array element from hash => $_\n";
}
######################################################
# Comparing references...
######################################################
print "\nComparing references...\n";
my $text = "This is a value.";
my $ref1 = \$text;
my $ref2 = \$text;
print "\$ref1==\$ref2 result:" . ($ref1==$ref2) . "\n";
$$ref1 = "Changed value.";
print "After \$ref1 change value, \$ref2=>" . $$ref2 . "\n";
my $test1 = "Text 1";
my $test2 = "Text 2";
my $rt1 = \$test1;
my $rt2 = \$test2;
print "\$rt1==\$rt2 result:" . ($rt1==$rt2) . "\n";
$$rt1 = "Change value.";
print "After \$rt1 change value, \$rt2=>" . $$rt2 . "\n";
######################################################
# Complex data strucutures...
######################################################
print "\nComplex data strucutures...\n";
my @multi_array = (
["one", "two", "three"],
["Red", "Yellow", "Blue"],
["Left", "Middle", "Right"]
);
print "Multi Array Element [0][2] ->$multi_array[0][2]\n";
print "Multi Array Element [2][1] ->$multi_array[2][1]\n";
my @second_row = @{$multi_array[1]};
print "Second row array = @second_row\n";
######################################################
# The undefined value...
######################################################
print "\nThe undefined value...\n";
my $a;
print "defined(a)?==>" . defined($a) . "\n";
print "It's defined\n" if defined $a;
$a=100;
print "defined(a)?==>" . defined($a) . "\n";
print "It's defined\n" if defined $a;
######################################################
# Define constant value...
######################################################
print "\nDefine constant value...\n";
use constant PI => 3.1415926;
print "36 degree is " . 36*PI/180 . "rad.\n";
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment