1)
#! /usr/bin/perl
@num = (3..5);
@alp = (a..e);
@mix = (3 .."s");
print "num == @num\n";
print "alp == @alp\n";
print "mix 3tos == @mix\n";
print " \@num[0] == @num[0]==== $num[0]=-==\n";
===== OUT PUT =======
num == 3 4 5
alp == a b c d e
mix 3tos ==
@num[0] == 3==== 3===
===== XXX =======
2)Simple add program using subroutine
use strict;
my @a = qw(1 2 3 4 5 6 7 8 9 );
my $c = myadd(@a);
print "sum = $c\n";
sub myadd {
my $value=0;
foreach my $num (@_) {
$value += $num;
}
return($value);
}
3)
use strict;
my $a = 2;
my $b = 3;
my $c = myadd($a,$b);
print "$a + $b = $c\n";
sub myadd {
my ($num1, $num2) = @_;
my $num3 = $num1 + $num2;
return($num3);
}
4. simple hash using key words keys & values
#! /usr/bin/perl
use strict;
my %hash = ("a" => "1",
"b" => "2",
"c" => "3",
);
my @keys = sort keys %hash;
my @values = sort values %hash;
print " all keys of hash == @keys\n";
print " all values of hash == @values\n";
print "File name is == $0\n";
===== out put ======
all keys of hash == a b c
all values of hash == 1 2 3
File name is == all_keys_values_function.pl
===== XXX =====
5) using $# syntax
#! /usr/bin/perl
use strict;
my @ary = qw(hi shaik shabbir);
my $scalar = scalar(@ary);
print "\$scalar == $scalar\n";
my $last_ary_value = $#ary;
print "\$last_ary_value == $last_ary_value\n";
$#ary++;
@ary[$#ary] = "suresh";
print "\$#ary == $#ary\n";
print "@ary == @ary\n";
print "\n";
===== out put ======
$scalar == 3
$last_ary_value == 2
$#ary == 3
hi shaik shabbir suresh == hi shaik shabbir suresh
===== XXX =====
6) concatenation operator "."
#! /usr/bin/perl
use strict;
my $name1 = 5; #"Shaik";
my $name2 = 4; #"Shabbir" ;
my $full_name = $name1.$name2;
print "full name == $full_name\n";
===== out put ======
full name == 54
===== XXX =====
7) keword each in Hashes
#! usr/bin/perl
use strict;
my %hash = ("a" => "1",
"b" => "2",
"c" => "3",
);
my $key;
my $value;
for (my $i = 0; $i <= 10; $i++) {
($key,$value) = each %hash;
print "key == $key and value == $value\n";
}
===== out put ======
key == c and value == 3
key == a and value == 1
key == b and value == 2
key == and value ==
key == c and value == 3
key == a and value == 1
key == b and value == 2
key == and value ==
key == c and value == 3
key == a and value == 1
key == b and value == 2
===== XXX =====
8) finding even and odd numbers
#! /usr/bin/perl
use strict;
my @number = (2 .. 100);
my ($even,$odd) = (0,0);
my (@even_num,@odd_num);
foreach my $num(@number){
my $check = $num % 2;
if(!$check){
$even_num[$even] = $num;
$even++;
}
else{
$odd_num[$odd] = $num;
$odd++;
}
}
print "Even numbers are == @even_num\n";
print "Odd Numbers are == @odd_num\n\n";
print "Total Even numbers are == $even\n";
print "Total Odd Numbers are == $odd\n";
9) shift and unshift syntax
#! /usr/bin/perl -w
use strict;
my @ary = "1";
print "0 == $ary[0]\n";
unshift(@ary,"2");
print "unshift 0 = $ary[0] 1 == $ary[1]\n";
my $pop = shift(@ary);
print "shift == @ary\n";
10) Comparing Arrays and Hashes
use strict;
my @my_array = ("e0",
"e1",
"e2");
my %my_hash = ("key0" => "value0",
"key1" => "value1",
"key2" => "value2",);
print "ARRAY: $my_array[0]\n"; #=== > array print syntax
print "HASH: $my_hash{\"key0\"}\n"; #=== > Hash print syntax
foreach my $element (@my_array) {
print "$element\n";
}
print "\n\n";
foreach my $xyz (keys %my_hash) {
print "$xyz: $my_hash{$xyz}\n";
}
11) Hash Dumper syntax && Hash print using hash reference
#! /usr/bin/perl
use strict;
use Data::Dumper;
my %hash = ("a" => "1",
"a_hash" => {"a1" => "a1_value",
"a2" => "a2_value",
},
"b" => "2",
"c_hash" => {"c1" => "c1_value",
"c2" => "c2_value",
},
);
my %hash_1 = ("a_hash1" => "1",
"a_hash1" => {"a1_hash1" => "a1_value_hash1",
"a2_hash1" => "a2_value_hash1",
},
"b_hash1" => "2",
"c_hash1" => {"c1_hash1" => "c1_value_hash1",
"c2_hash1" => "c2_value_hash1",
},
);
my %hash_2 = ("a_hash2" => "1",
"a_hash2" => {"a1_hash2" => "a1_value_hash2",
"a2_hash2" => "a2_value_hash2",
},
"b_hash2" => "2",
"c_hash2" => {"c1_hash2" => "c1_value_hash2",
"c2_hash2" => "c2_value_hash2",
},
);
my @hash_ref = (\%hash,\%hash_1,\%hash_2);
print Dumper(@hash_ref);
print "=====Printing using Hash Reference======\n";
print "$hash_ref[0]->{a_hash}{a1}\n";
12) Hierarchical Hashes Syntax
#! /usr/bin/perl
use strict;
my %simple_hash = ("a" => "1",
"b" => "2",);
my %hierarchical_hash = ("c" => {
"c1" => "c1_value",
"c2" => "c2_value",
},
"d" => {"d1" => "d1_value",
"d2" => "d2_value",
},
);
my @simple_keys = keys %simple_hash;
my @hirerchy_keys = keys %hierarchical_hash;
print "simple_keys ==> @simple_keys\n";
print "value of simple_keys ==> $simple_hash{$simple_keys[0]}\n\n";
print "hirerchy_keys ==> @hirerchy_keys\n";
print "Value of hirerchy_keys ==> $hierarchical_hash{$hirerchy_keys[0]}{c1}\n";
my $value = $hierarchical_hash{c}->{c1};
print "===Value == $value\n";
======= OUT PUT ===
simple_keys ==> a b
value of simple_keys ==> 1
hirerchy_keys ==> c d
Value of hirerchy_keys ==> c1_value
===Value == c1_value
=== XXX ===
13) Interview question to enter name and get its game (imc.pl)
#! /usr/bin/perl
use strict;
use Data::Dumper;
my @ary_string = ();
my %hash;
my %hash_of_hash;
$ary_string[0] = ("shaik is playing cricket");
$ary_string[1] = ("shabbir is playing footboll");
#print "@ary_string\n";
foreach(@ary_string) {
$_ =~ m/^(.*) is playing (.*)$/i ;
#print "\$1 === $1 \$2 == $2\n";
$hash_of_hash{$1}->{$1} = ($1 => $2);
$hash{$1} = ($1 => $2);
}
print Dumper(%hash);
print "\n\n";
print Dumper(%hash_of_hash);
print "Enter the name and get game\n";
my $name = <stdin>;
chomp($name);
print "from hash == $name is playing $hash{$name}\n\n";
print "from hash_of_hash == $name is playing $hash_of_hash{$name}->{$name}\n\n";
#! /usr/bin/perl
@num = (3..5);
@alp = (a..e);
@mix = (3 .."s");
print "num == @num\n";
print "alp == @alp\n";
print "mix 3tos == @mix\n";
print " \@num[0] == @num[0]==== $num[0]=-==\n";
===== OUT PUT =======
num == 3 4 5
alp == a b c d e
mix 3tos ==
@num[0] == 3==== 3===
===== XXX =======
2)Simple add program using subroutine
use strict;
my @a = qw(1 2 3 4 5 6 7 8 9 );
my $c = myadd(@a);
print "sum = $c\n";
sub myadd {
my $value=0;
foreach my $num (@_) {
$value += $num;
}
return($value);
}
3)
use strict;
my $a = 2;
my $b = 3;
my $c = myadd($a,$b);
print "$a + $b = $c\n";
sub myadd {
my ($num1, $num2) = @_;
my $num3 = $num1 + $num2;
return($num3);
}
4. simple hash using key words keys & values
#! /usr/bin/perl
use strict;
my %hash = ("a" => "1",
"b" => "2",
"c" => "3",
);
my @keys = sort keys %hash;
my @values = sort values %hash;
print " all keys of hash == @keys\n";
print " all values of hash == @values\n";
print "File name is == $0\n";
===== out put ======
all keys of hash == a b c
all values of hash == 1 2 3
File name is == all_keys_values_function.pl
===== XXX =====
5) using $# syntax
#! /usr/bin/perl
use strict;
my @ary = qw(hi shaik shabbir);
my $scalar = scalar(@ary);
print "\$scalar == $scalar\n";
my $last_ary_value = $#ary;
print "\$last_ary_value == $last_ary_value\n";
$#ary++;
@ary[$#ary] = "suresh";
print "\$#ary == $#ary\n";
print "@ary == @ary\n";
print "\n";
===== out put ======
$scalar == 3
$last_ary_value == 2
$#ary == 3
hi shaik shabbir suresh == hi shaik shabbir suresh
===== XXX =====
6) concatenation operator "."
#! /usr/bin/perl
use strict;
my $name1 = 5; #"Shaik";
my $name2 = 4; #"Shabbir" ;
my $full_name = $name1.$name2;
print "full name == $full_name\n";
===== out put ======
full name == 54
===== XXX =====
7) keword each in Hashes
#! usr/bin/perl
use strict;
my %hash = ("a" => "1",
"b" => "2",
"c" => "3",
);
my $key;
my $value;
for (my $i = 0; $i <= 10; $i++) {
($key,$value) = each %hash;
print "key == $key and value == $value\n";
}
===== out put ======
key == c and value == 3
key == a and value == 1
key == b and value == 2
key == and value ==
key == c and value == 3
key == a and value == 1
key == b and value == 2
key == and value ==
key == c and value == 3
key == a and value == 1
key == b and value == 2
===== XXX =====
8) finding even and odd numbers
#! /usr/bin/perl
use strict;
my @number = (2 .. 100);
my ($even,$odd) = (0,0);
my (@even_num,@odd_num);
foreach my $num(@number){
my $check = $num % 2;
if(!$check){
$even_num[$even] = $num;
$even++;
}
else{
$odd_num[$odd] = $num;
$odd++;
}
}
print "Even numbers are == @even_num\n";
print "Odd Numbers are == @odd_num\n\n";
print "Total Even numbers are == $even\n";
print "Total Odd Numbers are == $odd\n";
9) shift and unshift syntax
#! /usr/bin/perl -w
use strict;
my @ary = "1";
print "0 == $ary[0]\n";
unshift(@ary,"2");
print "unshift 0 = $ary[0] 1 == $ary[1]\n";
my $pop = shift(@ary);
print "shift == @ary\n";
10) Comparing Arrays and Hashes
use strict;
my @my_array = ("e0",
"e1",
"e2");
my %my_hash = ("key0" => "value0",
"key1" => "value1",
"key2" => "value2",);
print "ARRAY: $my_array[0]\n"; #=== > array print syntax
print "HASH: $my_hash{\"key0\"}\n"; #=== > Hash print syntax
foreach my $element (@my_array) {
print "$element\n";
}
print "\n\n";
foreach my $xyz (keys %my_hash) {
print "$xyz: $my_hash{$xyz}\n";
}
11) Hash Dumper syntax && Hash print using hash reference
#! /usr/bin/perl
use strict;
use Data::Dumper;
my %hash = ("a" => "1",
"a_hash" => {"a1" => "a1_value",
"a2" => "a2_value",
},
"b" => "2",
"c_hash" => {"c1" => "c1_value",
"c2" => "c2_value",
},
);
my %hash_1 = ("a_hash1" => "1",
"a_hash1" => {"a1_hash1" => "a1_value_hash1",
"a2_hash1" => "a2_value_hash1",
},
"b_hash1" => "2",
"c_hash1" => {"c1_hash1" => "c1_value_hash1",
"c2_hash1" => "c2_value_hash1",
},
);
my %hash_2 = ("a_hash2" => "1",
"a_hash2" => {"a1_hash2" => "a1_value_hash2",
"a2_hash2" => "a2_value_hash2",
},
"b_hash2" => "2",
"c_hash2" => {"c1_hash2" => "c1_value_hash2",
"c2_hash2" => "c2_value_hash2",
},
);
my @hash_ref = (\%hash,\%hash_1,\%hash_2);
print Dumper(@hash_ref);
print "=====Printing using Hash Reference======\n";
print "$hash_ref[0]->{a_hash}{a1}\n";
12) Hierarchical Hashes Syntax
#! /usr/bin/perl
use strict;
my %simple_hash = ("a" => "1",
"b" => "2",);
my %hierarchical_hash = ("c" => {
"c1" => "c1_value",
"c2" => "c2_value",
},
"d" => {"d1" => "d1_value",
"d2" => "d2_value",
},
);
my @simple_keys = keys %simple_hash;
my @hirerchy_keys = keys %hierarchical_hash;
print "simple_keys ==> @simple_keys\n";
print "value of simple_keys ==> $simple_hash{$simple_keys[0]}\n\n";
print "hirerchy_keys ==> @hirerchy_keys\n";
print "Value of hirerchy_keys ==> $hierarchical_hash{$hirerchy_keys[0]}{c1}\n";
my $value = $hierarchical_hash{c}->{c1};
print "===Value == $value\n";
======= OUT PUT ===
simple_keys ==> a b
value of simple_keys ==> 1
hirerchy_keys ==> c d
Value of hirerchy_keys ==> c1_value
===Value == c1_value
=== XXX ===
13) Interview question to enter name and get its game (imc.pl)
#! /usr/bin/perl
use strict;
use Data::Dumper;
my @ary_string = ();
my %hash;
my %hash_of_hash;
$ary_string[0] = ("shaik is playing cricket");
$ary_string[1] = ("shabbir is playing footboll");
#print "@ary_string\n";
foreach(@ary_string) {
$_ =~ m/^(.*) is playing (.*)$/i ;
#print "\$1 === $1 \$2 == $2\n";
$hash_of_hash{$1}->{$1} = ($1 => $2);
$hash{$1} = ($1 => $2);
}
print Dumper(%hash);
print "\n\n";
print Dumper(%hash_of_hash);
print "Enter the name and get game\n";
my $name = <stdin>;
chomp($name);
print "from hash == $name is playing $hash{$name}\n\n";
print "from hash_of_hash == $name is playing $hash_of_hash{$name}->{$name}\n\n";
No comments:
Post a Comment