my $a = 10;
print "value of a is $a\n";
my $result = "this is \"number\"";
print "$result, hello\n";
print "\$result, $a\n";
Customized Perl Code Highlighting
my $a = 10;
print "value of a is $a\n";
my $result = "this is \"number\"";
print "$result, hello\n";
print "\$result, $a\n";
Output of the above code:
value of a is 10 this is "number", hello $result, 10
2. Arrays (@)
Definition: Arrays are ordered lists of scalars.
Each element in an array can be accessed using its index, starting from 0.
In Perl, arrays are prefixed with an at symbol (@).
Usage: Arrays are useful for storing a list of items, like a collection of names, numbers, or other scalar values.
Example:
Customized Perl Code Highlighting
my @colors = ("red", "blue", "green"); # Array of strings
my @numbers = (1, 2, 3, 4, 5); # Array of numbers
Customized Perl Code Highlighting
my @colors = ("red", "blue", "green"); # Array of strings
my @numbers = (1, 2, 3, 4, 5); # Array of numbers
Accessing Array Elements:
To access individual elements -> $array[ ]
Customized Perl Code Highlighting
my @colors = ("red", "blue", "green"); # Array of strings
print $colors[0];
Customized Perl Code Highlighting
my @colors = ("red", "blue", "green"); # Array of strings
print $colors[0];
Output of the above code:
red
Example:
Customized Perl Code Highlighting
my @array = ("a","b","c","1","2","hello","hello world");
print "@array\n"; #output: a b c 1 2 hello hello world
print @array,"\n"; #output: abc12hellohello world
print "$array[5]\n"; #output: hello
print "@array[2,5]\n"; #output: c hello
print "@array[3..5]\n"; #output: 1 2 hello
#Negative index is also possible,starts from -1
print "$array[-6]\n"; #output: b
print "@array[-2,-5]\n"; #output: hello c
print "@array[-5..-2]\n"; #output: c 1 2 hello
Customized Perl Code Highlighting
my @array = ("a","b","c","1","2","hello","hello world");
print "@array\n"; #output: a b c 1 2 hello hello world
print @array,"\n"; #output: abc12hellohello world
print "$array[5]\n"; #output: hello
print "@array[2,5]\n"; #output: c hello
print "@array[3..5]\n"; #output: 1 2 hello
#Negative index is also possible,starts from -1
print "$array[-6]\n"; #output: b
print "@array[-2,-5]\n"; #output: hello c
print "@array[-5..-2]\n"; #output: c 1 2 hello
We can replace the space character between the output elements of an array with another character by using '$" .
Customized Perl Code Highlighting
my @array = ("a","b","c","d","e");
$" = "xx";
print "@array\n";
Customized Perl Code Highlighting
my @array = ("a","b","c","d","e");
$" = "xx";
print "@array\n";
Output of the above code:
axxbxxcxxdxxe
Size of an array (or) No. of elements in an array:
Storing an array into a scalar variable will give us the size of an array.
Customized Perl Code Highlighting
my @abc = ("a".."z"); # range operator(..)
my $size = @abc;
print "@abc\n";
print "$size\n";
Customized Perl Code Highlighting
my @abc = ("a".."z"); # range operator(..)
my $size = @abc;
print "@abc\n";
print "$size\n";
Output of the above code:
a b c d e f g h i j k l m n o p q r s t u v w x y z 26 ----> size of array @abc
using 'scalar' function to know size of array
Customized Perl Code Highlighting
my @abc = ("a".."z");
print scalar@abc,"\n"; #output: 26
Customized Perl Code Highlighting
my @abc = ("a".."z");
print scalar@abc,"\n"; #output: 26
Last index of an array:
$#array_name gives the index of the last element in the array.
Since Perl arrays are zero-indexed, the last index will be the total number of elements minus one.
Customized Perl Code Highlighting
my @numbers = (10, 20, 30, 40, 50);
my $last_index = $#numbers;
print "Last index: $last_index\n";
# or simply, print "$#numbers\n";
Customized Perl Code Highlighting
my @numbers = (10, 20, 30, 40, 50);
my $last_index = $#numbers;
print "Last index: $last_index\n";
# or simply, print "$#numbers\n";
Output of the above code:
Last index: 4
If the array is empty, $#array_name will return -1, indicating that there are no elements.
Customized Perl Code Highlighting
my @empty_array = ();
print "Last index: ", $#empty_array, "\n"; # Output: Last index: -1
Customized Perl Code Highlighting
my @empty_array = ();
print "Last index: ", $#empty_array, "\n"; # Output: Last index: -1
You can use $#array_name to access the last element of the array:
Customized Perl Code Highlighting
my @numbers = (10, 20, 30, 40, 50);
print "Last element: $numbers[$#numbers]\n"; #Output: Last element: 50
Customized Perl Code Highlighting
my @numbers = (10, 20, 30, 40, 50);
print "Last element: $numbers[$#numbers]\n"; #Output: Last element: 50
Inserting new elements into an array
You can assign a value directly to a specific index in the array.
If the index is beyond the current size of the array, Perl will automatically extend the array and fill the intervening spaces with undef.
Customized Perl Code Highlighting
my @fruits = ("apple", "banana");
$fruits[2] = "cherry";
print "@fruits\n"; # Output: apple banana cherry
$fruits[5] = "date";
print "@fruits\n"; # Output: apple banana cherry undef undef date
Customized Perl Code Highlighting
my @fruits = ("apple", "banana");
$fruits[2] = "cherry";
print "@fruits\n"; # Output: apple banana cherry
$fruits[5] = "date";
print "@fruits\n"; # Output: apple banana cherry undef undef date
There are other functions to insert new elements into an array like 'push', 'unshift', 'splice' which will be discussed later.
Merging of arrays
You can combine two arrays using the array concatenation operator (push or direct assignment).
Using direct assignment:
Customized Perl Code Highlighting
my @array1 = (1, 2);
my @array2 = (3, 4);
my @merged = (@array1, @array2);
print "@merged\n"; # Output: 1 2 3 4
Customized Perl Code Highlighting
my @array1 = (1, 2);
my @array2 = (3, 4);
my @merged = (@array1, @array2);
print "@merged\n"; # Output: 1 2 3 4
using 'push' :
Customized Perl Code Highlighting
my @array1 = (1, 2);
my @array2 = (3, 4);
push @array1, @array2; # Add all elements of @array2 to @array1
print "@array1\n"; # Output: 1 2 3 4
Customized Perl Code Highlighting
my @array1 = (1, 2);
my @array2 = (3, 4);
push @array1, @array2; # Add all elements of @array2 to @array1
print "@array1\n"; # Output: 1 2 3 4
You can access a value by specifying its key inside curly braces { } and prefixing the hash name with $.
Example:
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
print "$data{'AND'}\n"; #NOTE: Key name should be enclosed in single quotes only or no quotes at all
print "$data{'NOR'}\n";
print "$data{'NAND'}\n";
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
print "$data{'AND'}\n"; #NOTE: Key name should be enclosed in single quotes only or no quotes at all
print "$data{'NOR'}\n";
print "$data{'NAND'}\n";
Output of the above code:
Customized Perl Code Highlighting
45
30
hello world
Customized Perl Code Highlighting
45
30
hello world
We can access multiple multiple elements by storing them into an array
Example:
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = @data{'AND','NOR'};
print "@array\n"; #Output: 45 30
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = @data{'AND','NOR'};
print "@array\n"; #Output: 45 30
Adding or Modifying Elements
You can add a new key-value pair or modify an existing one by assigning a value to a key.
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
$data{'XOR'} = 100; #Add new key-value pair
$data{'NOR'} = 26; #Update value for the key 'NOR'
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
$data{'XOR'} = 100; #Add new key-value pair
$data{'NOR'} = 26; #Update value for the key 'NOR'
Deleting elements
Use the delete function to remove a key-value pair from a hash.
Customized Perl Code Highlighting
delete $data{'NAND'}
Customized Perl Code Highlighting
delete $data{'NAND'}
Check existance of a key
Use the exixst function to check wether a key exists or not in a hash
Customized Perl Code Highlighting
print exists $data{'NAND'};
Customized Perl Code Highlighting
print exists $data{'NAND'};
Printing keys only
Use the 'keys %hash' function and store them into an array, then print the array
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = keys %data;
print "@array\n"; #Output: NOR AND NAND (unordered)
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = keys %data;
print "@array\n"; #Output: NOR AND NAND (unordered)
Printing values only
Using the 'values %hash' function.
Customized Perl Code Highlighting
my @array = values %data;
print "@array\n"; #Output: 30 45 hello world (unordered)
Customized Perl Code Highlighting
my @array = values %data;
print "@array\n"; #Output: 30 45 hello world (unordered)
Size of a hash
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = keys %data; #or values %data
print "the size of hash is",scalar @array,"\n";
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array = keys %data; #or values %data
print "the size of hash is",scalar @array,"\n";
Output of the above code:
the size of hash is 3
Miscellaneous
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
print %data,"\n"; #Output: NOR30AND45NANDhello world
print "%data\n"; #Output: %data
print "@data\n"; #prints nothing
print "$data\n"; #prints nothing
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
print %data,"\n"; #Output: NOR30AND45NANDhello world
print "%data\n"; #Output: %data
print "@data\n"; #prints nothing
print "$data\n"; #prints nothing
Storing hash into array
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array_data = %data;
print "%data\n";
print "@array_data\n";
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
my @array_data = %data;
print "%data\n";
print "@array_data\n";
Output of the above code:
%data NOR 30 AND 45 NAND hello world
Printing a hash
Using each function and while loop
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
while (my ($key, $value) = each %data) {
print "$key => $value\n";
}
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
while (my ($key, $value) = each %data) {
print "$key => $value\n";
}
Output of the above code:
AND => 45 NOR => 30 NAND => hello world
Another way, Using keys and values
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
foreach my $key (keys %data) {
print "$key => $data{$key}\n";
}
Customized Perl Code Highlighting
my %data = (AND => 45, NOR => 30, NAND => "hello world");
foreach my $key (keys %data) {
print "$key => $data{$key}\n";
}