mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 22:02:24 +03:00
switch case done
This commit is contained in:
175
perl/wiki
175
perl/wiki
@@ -312,9 +312,184 @@
|
|||||||
@group2 : cef5 xcc360g cef8 cef4c
|
@group2 : cef5 xcc360g cef8 cef4c
|
||||||
|
|
||||||
|
|
||||||
|
-> Joining/Splitting the array elements
|
||||||
|
|
||||||
|
* Joining:
|
||||||
|
Syntax:
|
||||||
|
join("v", @array);
|
||||||
|
|
||||||
|
v: String which would be between the elements of array
|
||||||
|
#### join will return a string ####
|
||||||
|
|
||||||
|
* Splitting:
|
||||||
|
Syntax:
|
||||||
|
split("v", $string);
|
||||||
|
|
||||||
|
v: String which act a delimiter to split the give string
|
||||||
|
#### split will return array of strings ####
|
||||||
|
|
||||||
|
For ex:
|
||||||
|
|
||||||
|
@cards = qw(csc01 cef5 xcc369g);
|
||||||
|
|
||||||
|
print "============ Joining array elements to make string ==============";
|
||||||
|
$csvCards = join(",", @cards);
|
||||||
|
print "\$csvCards = $csvCards\n";
|
||||||
|
$dsvCards = join("--", @cards);
|
||||||
|
print "\$dsvCards = $dsvCards\n";
|
||||||
|
|
||||||
|
print "============ Splitting array elements to make array =============";
|
||||||
|
@s2aCards = split("--", $dsvCards);
|
||||||
|
print "\@s2aCards = @s2aCards\n";
|
||||||
|
|
||||||
|
|
||||||
-> my keyword:
|
-> my keyword:
|
||||||
|
|
||||||
my keyword is used in perl to limit the scope of variable
|
my keyword is used in perl to limit the scope of variable
|
||||||
to particular function or any scope where it is defined.
|
to particular function or any scope where it is defined.
|
||||||
|
|
||||||
|
|
||||||
|
-> Sorting and Merging:
|
||||||
|
|
||||||
|
* Sorting:
|
||||||
|
Syntax:
|
||||||
|
sort(@array);
|
||||||
|
|
||||||
|
* Merging:
|
||||||
|
Syntax:
|
||||||
|
(@array1,@array2);
|
||||||
|
|
||||||
|
For ex:
|
||||||
|
@cards = qw(csc01 cef5 xcc360g);
|
||||||
|
|
||||||
|
print "=========== Sorting & Merging ============";
|
||||||
|
@sortedCards = sort(@cards);
|
||||||
|
print "\@sortedCards = @sortedCards\n";
|
||||||
|
@futureCards = qw(cef9 csc03);
|
||||||
|
@presentNfutureCards = (@cards,@futureCards);
|
||||||
|
print "\@presentNfutureCards = @presentNfutureCards\n"
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
========== Sorting & Merging ===========
|
||||||
|
@sortedCards = cef5 csc01 xcc360g
|
||||||
|
@presentnfutureCards = csc01 cef 5 xcc360g cef9 csc03
|
||||||
|
|
||||||
|
-> HashMap:
|
||||||
|
|
||||||
|
Syntax:
|
||||||
|
@hashMap = ('key1', 'value1', 'key2', 'value2', 'key3', 'value3');
|
||||||
|
|
||||||
|
@hashMap = ('key1' => 'value1' , 'key2' => 'value2' , 'key3' => 'value3');
|
||||||
|
|
||||||
|
For ex:
|
||||||
|
|
||||||
|
print "========== HashMap ===========";
|
||||||
|
|
||||||
|
%cardG1CPUarch = ('csc01, 'mips', 'xcc360g', 'ppc', 'cef4c', 'ppc');
|
||||||
|
print "\$cardG1CPUarch{'csc01'} = $cardG1CPUarch{'csc01'}\n";
|
||||||
|
print "\$cardG1CPUarch{'xcc360g'} = $cardG1CPUarch{'xcc360g'}\n";
|
||||||
|
print "\$cardG1CPUarch{'cef4c'} = $cardG1CPUarch{'cef4c'}\n";
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
=========== HashMap ===========
|
||||||
|
$cardG1CPUarch{'csc01'} = mips
|
||||||
|
$cardG1CPUarch{'xcc360g'} = ppc
|
||||||
|
$cardG1CPUarch{'cef4c'} = ppc
|
||||||
|
|
||||||
|
|
||||||
|
print "========== HashMap ===========";
|
||||||
|
|
||||||
|
%cardG1CPUarch = ('csc01' => 'mips', 'xcc360g' => 'ppc', 'cef4c' => 'ppc');
|
||||||
|
print "\$cardG1CPUarch{'csc01'} = $cardG1CPUarch{'csc01'}\n";
|
||||||
|
print "\$cardG1CPUarch{'xcc360g'} = $cardG1CPUarch{'xcc360g'}\n";
|
||||||
|
print "\$cardG1CPUarch{'cef4c'} = $cardG1CPUarch{'cef4c'}\n";
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
=========== HashMap ===========
|
||||||
|
$cardG1CPUarch{'csc01'} = mips
|
||||||
|
$cardG1CPUarch{'xcc360g'} = ppc
|
||||||
|
$cardG1CPUarch{'cef4c'} = ppc
|
||||||
|
|
||||||
|
|
||||||
|
-> Slicing HashMap, Key - Values - Size of hashmap, Adding/Removing from Hashmap
|
||||||
|
|
||||||
|
Ex:
|
||||||
|
%cardG1CPUarch = ('csc01', 'mips', 'xcc360g', 'ppc', 'cef4c', 'ppe');
|
||||||
|
|
||||||
|
print "=============Slicing Hashes================";
|
||||||
|
@cpuSlice1 = @cardG1CPUarch('csc01', 'cef4c');
|
||||||
|
print "\@cpuSlice1 = @cpuSlice1\n";
|
||||||
|
|
||||||
|
print "=============Keys & Values & size of hash=============\n"
|
||||||
|
@g1Cards = keys @cardG1CPUarch;
|
||||||
|
print "\@g1Cards = @g1Cards\n";
|
||||||
|
@g1CPU = values %cardG1CPUarch;
|
||||||
|
print "\@g1CPU = @g1CPU\n"
|
||||||
|
#everything true for arrays can be used for ex:
|
||||||
|
print "\@g1CPU[2] = @g1CPU[2]\n";
|
||||||
|
$g1HashSize = @g1Cards; #donwgrading the array to scalar.
|
||||||
|
print "\$g1HashSize = $g1HashSize\n";
|
||||||
|
|
||||||
|
print "===============Adding or removing elements================\n";
|
||||||
|
$cardG1CPUarch{'PCsim"} = 'i386';
|
||||||
|
@g1Cards = keys %cardG1CPUarch;
|
||||||
|
print "\@g1Cards = @g1Cards\n";
|
||||||
|
@g1CPU = values %cardG1CPUarch;
|
||||||
|
print "\@g1CPU = @g1CPU\n".\;
|
||||||
|
print "removing cef4c from %cardG1CPUarch\n\n";
|
||||||
|
delete $cardG1CPUarch{'cef4c'};
|
||||||
|
@g1Cards = keys %cardG1CPUarch;
|
||||||
|
print "\@g1Cards = @g1Cards\n";
|
||||||
|
@g1CPU = values %cardG1CPUarch;
|
||||||
|
print "\@g1CPU = @g1CPU\n";
|
||||||
|
|
||||||
|
|
||||||
|
print "====================HashMap end========================"
|
||||||
|
|
||||||
|
|
||||||
|
-> Ternary operator in perl
|
||||||
|
|
||||||
|
For Ex:
|
||||||
|
$isSimulator = ($cardG1CPUarch{'PCsim'} == 'i386') ? "Yes" : "No";
|
||||||
|
|
||||||
|
print " \$isSimulator - $isSimulator\n";
|
||||||
|
|
||||||
|
-> Branching in Perl (if-else):
|
||||||
|
|
||||||
|
For ex:
|
||||||
|
|
||||||
|
$a = 20;
|
||||||
|
unless( $a == 30 ){
|
||||||
|
#if condition is false then print the following
|
||||||
|
printf "a has a value which is not 20\n"
|
||||||
|
}
|
||||||
|
elseif( $a == 30 ){
|
||||||
|
#if condition is true then print the following
|
||||||
|
printf "a has a value which is 30\n";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
#if none of the above condition is met
|
||||||
|
printf "a has a value which is $a\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
-> switch in Perl (switch case):
|
||||||
|
#switch with fall thru.
|
||||||
|
#need Switch to be installed on system using cpan
|
||||||
|
|
||||||
|
For ex:
|
||||||
|
use Switch;
|
||||||
|
@array = (10, 20, 30);
|
||||||
|
%hash = ('key1' => 10, 'key2' => 20);
|
||||||
|
|
||||||
|
switch($a){
|
||||||
|
case 10 { print "number 100\n"; next; }
|
||||||
|
case "a" { print "string a" }
|
||||||
|
case (\@array) { print "number in list" }
|
||||||
|
case (\%hash) { print "entry in hash" }
|
||||||
|
else { print "previous case not true"}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user