perl qw - what is?
Perl Qw:
perl qw is quoted words, it is useful for declaring arrays, instead of writing quotes for each strings, we can use qw to minimize it.
Qw Delimiter:
1.space(Recommended and default)
2.comma
3.comment
Labels: perl qw
[There's More Than One Way To Do It]
Always A Blog To Get Confident in Coding
Labels: perl qw
Labels: kaavannan perl blogspot, Reversing a string in perl without using reverse function
Labels: chomp in perl, chomp in python, chomp perl example, chomp unix, chop and chomp in perl, chop function in perl, kaavannan perl blogspot, perl chomp returns 1, stdin in perl
Labels: unlink, unlink files
$newvariable = [1,2,3];
print $$newvariable[0]; or print $newvariable->[0]; or print @$newvariable
Labels: anonymous array, anonymous hash, anonymous subroutine
@array=(8,1,3,-6,2); print @array;
Labels: array create, perl array
foreach $keys (sort keys %hashmap) { print "$food is $hashmap{$keys}.\n"; }
foreach $keys (sort { $hashmap{$a} cmp $hashmap{$b} } keys %hashmap) { print "$keys is $hashmap{$keys}.\n"; }
@length = sort { length($hashmap{$a}) <=> length($hashmap{$b}) } keys %hashmap; foreach $keys (@length) { print "$keys is $hashmap{$keys}.\n"; }
Labels: kaavannan perl blogspot, sort hash keys, sort keys alphabetically
foreach $key (sort { $a <=> $b} keys %hashnum) { print $hashnum{$key} . "\n"; }
foreach $key (sort { $b <=> $a} keys %hashnum) { print $hashnum{$key} . "\n"; }
Labels: kaavannan perl blogspot, sort hash keys numerically, sort keys numerically
Labels: array create, perl array
$ip='25.24.23.21'; print "Valid IP\n" if 4 == grep { /^[^0]+$/ and $_ <= 255 and /^\d+$/ } split /\./, $ip;
$ip='25.24.23.21'; $i=0; for(split /\./, $ip) { if (/^\d+$/&&/^[^0]+$/&& $_<256) { $i++; } } print "Valid IP" if $i == 3;
$ip='25.24.23.21'; $a=~/^(([0-9]|1*[0-9]{2,2}|2[0-4][0-9]|25[0-5])(\.|$)){4,4}/ ? print "Valid IP" : print "Not Valid IP"; print "Valid IP" if $i == 3;
$ip='25.24.23.21'; $ip=~/^(([01]?\d\d?|2[0-4]\d|25[0-5])[.]?){4}$/ ? print "Valid IP" : print "Not Valid IP";
$ip='25.24.23.21'; $ip=~/^((0|1[0-9]{0,2}|2[0-9]?|2[0-4][0-9]|25[0-5]|[3-9][0-9]?)\.){3}(0|1[0-9]{0,2}|2[0-9]?|2[0-4][0-9]|25[0-5]|[3-9][0-9]?)$/ ? print "Valid IP" : print "Not Valid IP";
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
tar -xzvf XML-Simple-2.25.tar.gz
cd XML-Simple-2.25
perl Makefile.PL->make->make test ->make install
Labels: cpan installation, kaavannan perl blogspot, perl module installation
open(FH,'<file.txt') or die "Cant open file $!";
print FH;
close(FH);
Labels: perl read file - 15 ways
Labels: kaavannan perl blogspot, perl add new line to string, perl append new line to file, perl insert line in file after match, perl script to insert line into file