Wednesday, 27 November 2019

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


Read more »

Labels:

Sunday, 24 November 2019

Perl - without reverse function reverse a string

Perl Reverse:
perl reverse function reverses the string which will be very useful for creating or while writing the large code. it saves the time from writing new custom functions for reverse.


Without Perl Reverse Function we can write our custom functions in Perl as below

Read more »

Labels: ,

Thursday, 21 November 2019

what is perl chomp-chomp command-chomp Example-chomp function-chomp string?

Perl Chomp:

Chomp removes value of $/. normally $/ variable will have \n(newline) as its default variable. But we can change that to some other values for major level of stuffs, if we want.

Unix/Linux  Kind of File formats will be separated based on newlines
like,

1st line \n
2nd line  \n

those two lines will be separated based on newline that is input record separator. in perl its defined using $/ variable.

Read more »

Labels: , , , , , , , ,

Monday, 18 November 2019

Perl Unlink - what is?

Perl Unlink:
perl unlink is used to remove the files. based on our requirements we can delete more than single file using unlink function.
by storing array or list elements  as filename we can achieve group of filenames deletion.


Cons:
1. Unlink will not notify which files could not remove. but it can be manageable using perl warning functions.
2. Unlink will not work on many os.
3. Super user permission necessary for Unlinking a directory although it is not recommended for usual coding.

Read more »

Labels: ,

Saturday, 16 November 2019

Anonymous variables in Perl?


A variable which is not having name is called anonymous variables.it returns a reference to a new variable.

Method 1:


$newvariable = [1,2,3];
print $$newvariable[0];

or

print $newvariable->[0];

or

print @$newvariable


Read more »

Labels: , ,

Friday, 15 November 2019

perl array - what is?

Perl Arrays:

Array is a collection of ordered elements, which is assigned by more than one elements like scalars,strings,numbers,floating points etc.

Creating Arrays:

Method 1:


@array=(8,1,3,-6,2);

print @array;

Read more »

Labels: ,

Wednesday, 13 November 2019

perl sort keys - hash alphabetically


%hashmap=('awk','perl','code','things','sed','need');

Method 1:[alphabetically]


foreach $keys (sort keys %hashmap) {

    print "$food is $hashmap{$keys}.\n";

}


Method 2:[Sort Based on Value]
foreach $keys (sort { $hashmap{$a} cmp $hashmap{$b} }

                keys %hashmap)

{

    print "$keys is $hashmap{$keys}.\n";

}

Method 3:[sort Based on value Length]
@length = sort { length($hashmap{$a}) <=> length($hashmap{$b}) } keys %hashmap;

foreach $keys (@length) {

    print "$keys is $hashmap{$keys}.\n";

}

kaavannan perl blogspot

Labels: , ,

Tuesday, 12 November 2019

perl sort keys - hash numerically?


%hashnum=(21,2,44,11,55,5);


Method 1:[ascending order]

foreach $key (sort { $a <=> $b} keys %hashnum) {

    print $hashnum{$key} . "\n";

}

Method 2:[Descending order]

foreach $key (sort { $b <=> $a} keys %hashnum) {

    print $hashnum{$key} . "\n";

}
kaavannan perl blogspot

Labels: , ,

Sunday, 10 November 2019

Perl Array - Common Functionalities and Usage?

Perl Arrays Functionalities:

We Can perform many functionalities using arrays among them below are some of the most used functionalities in perl

Method 1: [find only Duplicate Elements]

@a=(6,6,4,5,7,9,9);
for (@a) {
    push @dup, $_  if ($h{$_}++ == 1)
}
print @dup;


Method 2:[find largest number from an array] 

@sorted = sort { $a <=> $b } @a;
print @sorted[-1];


Method 3:[push and pop functionalities]

push adds the element at  end of  array

push(@a,"one");
push(@a,"two");

pop removes  last element of  array
pop @a

Method 4:[shift and unshift]

shift removes  first element of  array

shift @a;

unshift adds  element  front of array

unshift(@a,'one');

Method 5:[Finding length of an array elements]

1.
@a=(6,6,4,5,7,9,9);
print $#a+1;

2.
print scalar @a;

3.
$b=@a;
print $b;

Method 6:[Emptying array elements]

$#a=-1;

Method 7:[reversing array elements]

@a=(6,6,4,5,7,9,9);
print reverse @a;


Labels: ,

Friday, 8 November 2019

perl validate get ip address


Method 1:

$ip='25.24.23.21';

print "Valid IP\n" if 4 == grep { /^[^0]+$/ and  $_ <= 255  and  /^\d+$/  } split /\./, $ip;


Method 2:

$ip='25.24.23.21';
$i=0;
for(split /\./, $ip) {
if (/^\d+$/&&/^[^0]+$/&& $_<256)
{
   $i++;
}
}

print "Valid IP" if $i == 3;


Method 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;


Method 4:

$ip='25.24.23.21';

$ip=~/^(([01]?\d\d?|2[0-4]\d|25[0-5])[.]?){4}$/ ? print "Valid IP" : print "Not Valid IP";


Method 5:

$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";



Wednesday, 6 November 2019

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=(1,3,5,6,2);
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


 $#array+1;
       or
scalar(@array);
      or
$a=@array;


For Emptying Array
$#array = -1;
     or
@array = ( );

To reverse an array
print reverse @array;


To sort array Elements
ascending order
@a=sort{$a <=> $b }@array;
desending order
@a=sort{$b <=> $a }@array;


To add array elements
$array[0]='55';




To Delete array elements
delete $array[1];
 
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'}







To find length of hash

@array=keys %hash;
scalar(@array);
     or
$#array+1;
     or
$a=@array;



For Emptying hash
%hash=( );




To reverse a hash
print reverse %hash;



To sort hash elements

 foreach $k (sort keys %hash)
{
   print "$k => $hash{$k}\n";
}



To add hash Elements
$hash{1}='one';





To Delete hash elements
delete($hash{'1'});


Labels:

Tuesday, 5 November 2019

perl - 5 ways cpan module installation


Method 1:(Manual Installation From cpan.org)

use wget  or manual search  from  metacpan.org
and get the package download path and use with wget or download manually.

Extract it as described below,

tar -xzvf XML-Simple-2.25.tar.gz
cd XML-Simple-2.25

perl Makefile.PL->make->make test ->make install


Read more »

Labels: , ,

Monday, 4 November 2019

perl read file - 15 ways

Method 1:


open(FH,'<file.txt') or die "Cant open file $!";

print FH;

close(FH);

Read more »

Labels:

Sunday, 3 November 2019

perl empty the file content

Method 1:

open $filename, '>', 'file.txt';


Method 2:

truncate 'file.txt', 0;


Method 3:

`perl -pi -e 's/.*//s' file.txt`;


Method 4:

system(": > file.txt");


Method 5:

system(" > file.txt");


Method 6:

readpipe("cat /dev/null > file.txt");


Method 7:

readpipe("cp /dev/null file.txt");


Saturday, 2 November 2019

perl insert new line at beginning of existing data file

Method 1:

1.perl -pi -e 'print "perl one liner is added" if $. == 1'  file.txt

Method 2:

open my $file1,  '<',  $file1.txt;
open my $file2, '>', "$file2.txt";

print $file2 "perl one liner is added";

while( <$file1> ) {
    print $file2 $_;
}
close $file2;
close $file1;

Method 3:

echo "perl one liner is added" | perl -0 -i -pe 'BEGIN {$input = <STDIN>}; print $input' file.txt

Method 4:

open(M,"<","file1.txt");
@m = <M>;
close(M);
open(M,">","file2.txt");
unshift (@m,"Perl Coder is always magic\n");
print M @m;
close(M);


kaavannan perl blogspot

Labels: , , , ,

Friday, 1 November 2019

perl - find missing numbers in array

Method 1:

@a = (1,3,4,6); @c = map $a[$_-1]+1..$a[$_]-1, 1..@a-1; print join(" ", @c);




Read more »

Labels: