Done till array slicing

This commit is contained in:
2025-02-21 09:54:25 +00:00
parent 81cd00448f
commit 98761e7104
2 changed files with 232 additions and 1 deletions

11
perl/array.pl Normal file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/perl
@disk_cap = (50, 30, 40, 'fifty five',66,80,90);
$size_of_array_disk_cap = @disk_cap;
$max_index=@disk_cap - 1;
@cards = ("csc01", "xcc360g", "xcc80g");
@line_cards = qw(cef5 cef4c xa10g);
print "disk_cap = @disk_cap\n";
print "disk_cap[0] = $disk_cap[0]\n";
print "line_cards[0] = $cards[0] cards[-1] = $cards[-1]\n";
print "size of array \@disk_cap = $size_of_array_disk_cap max_index = $max_index\n";

220
perl/wiki
View File

@@ -98,3 +98,223 @@
exception:
'#!/usr/bin/perl' Shabang is not comment. It just tell
the bash that its a perl script.
-> Whitespace:
Like C, perl script does not care about whitespaces, unless they
are inside the quoted strings, then they would be printed as it is.
-> Single/double quote:
Only double quotes expand variable and special characters such as
newlines \n.
$a = 10;
print "Value of a = $a\n"
print 'Value of a = $a\n'
Output:
Value of a = 10
Value of a = $a\n$
This is behaviour is similar to shell scripts.
-> Multi line strings
$a = 10;
$multiline_str = <<"END_OF_LINE_DELIMITER";
This is the syntax for multi line string and will continue until
it encounters a END_OF_LINE_DELIMITER in the first line.
Also see the effect of single and double quote a=$a
END_OF_LINE_DELIMITER
print "$multiline_str\n";
Output:
This is the syntax of multi line string and will continute until
it encounters a END_OF_LIN_DELIMITER in the first line.
Also see the effect of single and double quote a=10
In case of : $multiline_str = <<END_OF_LINE_DELIMETER;
Output would differ value of a will not expand. So a=$a will come at the end.
• Escape Char : Perl uses the backslash (\) as escape character .
Ex: print “\$multiline_str\n";
Output : $multiline_str
• Identifiers : Perl does not allow punctuation characters such as @, $, and %
within identifiers. Perl is a case sensitive programming language. So $a & $A
are different.
-> Variable(Scalar):
Mainly perl have three type of variable:
* Scalar (Denoted by $ in the starting)
* Array
* Hashmap
If strict is being used : This is mandatory to declare a variable before we use it if
we use use strict statement in our script.
Syntax:
use strict
• Scalar : defined by $ at the start.
Ex:
#!/usr/bin/perl
$threads= 2; # An integer assignment : a numerical scalar
$compilation_dir = “/home/techie/mirror/$threads"; # A string scalar double quoted
$compilation_dir2 = /home/techie/mirror/$threads; # A string scalar in single quote
print “threads = $threads\n";
print “compilation_dir = $compilation_dir\n";
print “compilation_dir2 = $compilation_dir2\n";
Output :
threads = 2
compilation_dir = /home/techie/mirror/2
compilation_dir2 = /home/techie/mirror/$threads
###Scaler Operations####
#!/usr/bin/perl
$threads= 2 + 4; # adds two numbers
$image_name = “csc01_“ . “mips.img” ; # concatenate 2 strings
$final_with_prefix= $threads . “_” . $image_name ; # concatenate number
with string
print “threads = $threads\n";
print “image_name = $image_name\n";
print “final_with_prefix = $final_with_prefix \n";
Output :
threads = 6
image_name = csc01_mips.img
final_with_prefix = 6_csc01_mips.img
-> Multiline String and special literals:
Special literals cant be expanded.
$string = 'This is a
multiline string with special literals . __PACKAGE__ . “_” . __FILE__ . “_” .
__LINE__ . “__LINE__”;
print "$string\n";
Output : This is a
multiline string with special literals tUtils_train.pl_55__LINE__
-> Variable(array):
Arrays, size of array & max index : Defined by @ at start.
#!/usr/bin/perl
@disk_cap = (50, 30, 40, fifty five,66,80,90);
$size_of_array_disk_cap = @disk_cap
$max_index=@disk_cap - 1
@cards = (”csc01", ”xcc360g", ”xcc80g");
@line_cards = qw(cef5 cef4c xa10g)
print “disk_cap = @disk_cap\n";
print “disk_cap[0] = $disk_cap[0]\n";
print “line_cards[0] = $cards[0] cards[-1] = $cards[-1]\n";
print “size of array \@disk_cap = $size_of_array_disk_cap max_index =
$max_index\n";
Output :
disk_cap = 50 30 40 fifty five 66 80 90
disk_cap[0] = 50
line_cards[0] = cef5 cards[-1] = xcc80g
size of array @disk_cap = 7 max_index = 6
-> Sequential Number arrays:
@var_20=(10..20); print "@var_20\n";
10 11 12 13 14 15 16 17 18 19 20
Similarly, in shell we use:
seq 1 5
Output:
1 2 3 4 5
-> Operation on arrays:
* push:
Syntax:
push(@array, value)
Example:
@cards = (”csc01", ”xcc360g", ”xcc80g");
print "2. \@cards = @cards\n";
#add one element at the end of the array
push(@cards, "csc02");
print "2. \@cards = @cards\n";
Output:
2. @cards = csc01 xcc360g xcc80g
2. @cards = csc01 xcc360g xcc80g csc02
* unshift:
Syntax:
unshift(@cards, "xcc80g")
Example:
@cards = qw(csc01 cef5 xcc360g);
print "2. \@cards = @cards\n"
print " push csc02\n"
#add one element at the start of the array
unshift(@cards, "xcc80g");
print "3. \@cards = @cards\n";
* pop:
Syntax:
pop(@array);
Example:
@cards = qw(csc01 cef5 xcc360g);
print " pop\n";
# remove one element from the last of the array
pop(@cards);
print "4. \@cards = @cards\n"
* shift:
Syntax:
shift(@array)
Example:
@cards = qw(csc01 cef5 xcc360g);
print " shift\n";
# remove one element from the beginning of the array
shift(@cards);
print "5. \@cards = @cards\n";
-> Slicing Array elements:
Syntax:
@array[2,4];
or @array[1..4];
Example:
@cards = qw(csc01 cef5 xcc360g cef8 cef4c);
print "==========Slicing Array elements============"
@group1 = @cards[2,4];
print "\@group1 : @group1\n";
@group2 = @cards[1..4];
print "\@group2 : @groups\n";
Output:
==============Slicing Array elements================
@group1 : xcc360g cef4c
@group2 : cef5 xcc360g cef8 cef4c
-> my keyword:
my keyword is used in perl to limit the scope of variable
to particular function or any scope where it is defined.