perl - array and hash common differences
S.No
|
ARRAYS
|
HASHES
|
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
|
It is ordered data collection
Data Can be accessed based on index value
Stack operations performed using push,pop,shift,unshift
Duplicate Elements will be exist in Array
For Large data, Arrays will not be in speed as getting values from
hash keys.
Arrays can be created using () brackets @array=
For More methods refer this link.
for accessing array index
$array[1];
Array can be accessed in loops as below
1.For(@array)
{
}
2.foreach(@array1,@array2)
{
Print $_;
}
3.print grep{},@array;
4. print map{},@array;
Array slices can be used as below
@array[2];
To find length of an array
For Emptying Array
To reverse an array
To add array elements
$array[0]='55';
To Delete array elements
|
It is unordered Data Collection
Data Can be accessed based on Key value
Stack operations cannot be performed
Keys will not have Duplicate elements but Values would have Duplicate
elements.
Based on hash Keys Data retrival would be fast
Hash is also possible to create using () bracket %hash=(1,3,5,6,2);
For More methods refer this link. https://kaavannan-perl.blogspot.com/2019/10/perl-hashes-what-is.html
For accessing hash index use the key name
$hash{1};
Hash can be accessed based on keys and values
While(($k,$vals) = each %h)
{
Print “$k,$vals\n”;
}
Foreach $k (keys %h)
{
Print “$k => $hash{$k}\n”;
}
Foreach $values (values %hash)
{
Print “$values\n”;
}
Hash slices can be used as below,
$hash{'1'}
@array=keys %hash;
To reverse a hash
print reverse %hash;
To add hash Elements
$hash{1}='one';
To Delete hash elements
delete($hash{'1'});
|
Labels: array hash difference
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home