Sunday 1 January 2023

Without changing the original,how do we perform a replacement on a string using perl regex?



Method1:
 
(my $newstring = $oldstring) =~ s/foo/bar/g;

Method2:
 
my $newstring = $oldstring =~ s/foo/bar/gr;   

Method3:
 
if it is an arrays:
my @a = ('appl', 'appl bal', 'fruit?');
my @b = map { s/appl/apple/; $_ } map { $_ } @a;
 

Labels: , , , , , ,

Saturday 12 November 2022

perl rest client crud tutorial example using REST::Client


Hi,Today  Perl script that demonstrates how to interact with a REST API using the REST::Client module. The script shows examples of performing HTTP requests such as GET, POST, and DELETE to interact with different endpoints of a RESTful API. The script starts by importing necessary modules, and then sets up basic configurations for authentication and API endpoint URLs. The code defines three subroutines, each performing a specific type of API request: GetList(), CreatePost(), and DeleteList(). These subroutines use the HTTP methods of the REST::Client module to call the respective API endpoints, and they return the API response. Finally, the script calls these subroutines and prints the API responses on the console.

Read more »

Labels: , , ,

Friday 24 January 2020

What is the opendir() function in Perl?


opendir  function is used for reading the content of a particular folder.

Method 1:


opendir(DIRHANDLE,"/some director/path") or die "Can't open directory";

Method 2:


opendir DIRHANDLE, '/some diretory/path' or die "Can't open directory";

Read more »

Labels: , , , , , , , ,

Wednesday 1 January 2020

What is a continue function in Perl?


Control statement makes iteration to beginning of loops for next iteration.

Method 1:

$t = 0;

while($t < 3) {
   print "Value of var t = $t\n";
} continue {
   $t = $t + 1;
}


Read more »

Labels: , , ,

Sunday 1 December 2019

perl piped open - reading data from a system process?

Perl Piped Open:

open function is very useful for reading or writing the data files on the system and apart from this file processing we can use pipe symbol for getting data from a system process or sending the data to a system process can be achieved.   lets try this now,


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: , , , , , , , ,

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: , ,

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: , ,

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: , , , ,

Thursday 31 October 2019

Perl In Line Hack - Perform Variable Assigning and Deletion in one Line

Method 1:


($_ = 'hellio') =~ s/i//;

print $_;


Method 2:


s/i// for (my $_ = 'hellio');

print $_;


Method 3:


substr(($_ = 'helloi'), index ($_, "i"), length($_),"")

print $_;


Method 4:


map $_ =~ s/i//,$_='hellio';

print $_;


Method 5:


grep $_ =~ s/i//,$_='hellio';

print $_;


Method 6:


print grep $_=~/[^i]+/,split('',$_='hellio');



Method 7:


map $_=~tr/i//d,$_="hellio";

print $_;


Method 8:


$_=~ s/i// if $_='hellio';

print $_;


Method 9:


$_ =~ tr/i//d if $_ = 'hellio';

print $_;


Method 10:


print grep $_=~/[^i]+/, @_, split '', 'hellio';


Method 11:


print grep $_=~/[^i]+/, "hellio"=~ /./g;


Method 12:


($_='hellio')=~tr/i//d;

print $_;


Method 13:


$_="hellio",$_=~tr/i//d;

print $_;


Method 14:


print @_,grep $_=~/[^i]+/, (split '', 'hellio');


Method 15:


grep $_=~tr/i//d,$_="hellio";

print $_;


kaavannan perl blogspot

Labels: , , , ,

Monday 28 October 2019

perl strip spaces from both side of string?

Method1:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^\s+|\s+$//g;


print $unmai;


Method2:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^\s+(.*)\s+$/$1/g;

print $unmai;


Method3:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)([\w|.|\s]+)(\s+)$/$2/eg;

print $unmai;



Method4:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)([A-Za-z\s]+)(\s+)$/$2/eg;

print $unmai;


Method5:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)(\b[A-Za-z\s]+\b)(\s+)$/$2/eg;

print $unmai;


Method6:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)([\s|.|\S]+)(\s+)$/$2/eg;

print $unmai;


Method 7:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)(.*)(\s+)$/$2/eg;

print $unmai;


Method 8:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)([^.]*)(\s+)$/$2/eg;


print $unmai



Method 9:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^(\s+)([\w\d\S.\s_-]*)(\s+)$/$2/eg;

print $unmai



Method 10:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~s/^\s+|((?:$|(?:\.\s))*)(\s+)$//g;


print $unmai;



Method 11:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~/(?m)\W*(.*?)\W*$/g;

print $1;


Method 12:

$unmai=" jaathi enbathu oru tholil thavira athu oru mayirum illai yaar vandumaanalum entha tholilum seyyalam ";

$unmai=~/\s*((\w+)[^;]+\s(\w+);?).*/g;

print $1;



kaavannan perl blogspot

Labels: , , ,

Tuesday 8 October 2019

how many methods for reference creation in perl?

Method 1:

$ref=\"HelloHi";
print $$ref;                     #HelloHi


Method 2:

$var=5;
$ref="var";
print "$$ref";                 #5


Method 3:

@a=(1,2,3);
$ref="a";
print "@$ref";                 #1 2 3


Method 4:

%h=(a,b);
$ref="h";
print "$$ref{a}";            #b


Method 5:

sub name
{
    print "Hi Lets Crack\n";
}

$ref="name";
&{$ref};                        #Hi Lets Crack


Method 6:

$var=5;
$ref=\$var;
print $$ref;                 #5


Method 7:

@a=(1,2,3);
$ref=\@a;
print "@$ref";              #1 2 3


Method 8:

%h=(a=>b);
$ref=\%h;
print "$$ref{a}\n";      #b


Method 9:

sub name
{
    print "Hi Lets Crack\n";
}
$ref=\&name;
&$ref;                           #Hi Lets Crack


Method 10:

sub name
{
    my $a=shift;
    print $a "Hi Lets Crack\n";
}
open(FILE,">a.txt");
name(\*FILE);     


Method 11:

@a=(1,2,3);
@b=(4,5,6);
@array= name(\@a,\@b);
print @array;       


Method 12:

$a="hellohi";
$ref=\substr($a,0,1);
print $$ref;              #h


Method 13:

$ref=[1,2,3];
print "@$ref";         #1 2 3


Method 14:

$ref=[qw/1 2 3/];
print "@$ref";         #1 2 3


Method 15:

$ref=[qq/1 2 3/];
print "@$ref";         #1 2 3


Method 16:

$ref={a=>A,
b=>B};
print $$ref{a};         #A


Method 17:

$ref= sub {print "Hello\n"};
&$ref;                      #Hello


Method 18:

$var=5;
$ref=*var{SCALAR};
print $$ref;                #5


Method 19:

$var = 1;
${*var{SCALAR}} = 5;
print $var;                  #5


Method 20:

sub name
{
    print  "Hi Lets Crack\n";
}
$ref=*name{CODE};
&$ref;                       #Hi Lets Crack



kaavannan perl blogspot

Labels: , , , , , ,

Sunday 6 October 2019

how many ways string can be identified by perl?

Method 1:


print "hello hi";

Method 2:


print 'hello hi';

Method 3:


print join " ",qw(hello hi);

Method 4:


print q(hello hi);

Method 5:

print qq(hello hi);


kaavannan perl blogspot

Labels: , , , , , ,

Tuesday 1 October 2019

hash-inversion-by-reversing-the-values-to-keys-and-keys-to-values-in-perl


Method 1:

                             %hash=(9,5,2,1,4,6);
                             @final5{values %hash} = keys %hash;
                              print @final5;


Method 2:

                              $z=0;
                              %hash=(9,5,2,1,7,6);
                              foreach((sort values %hash)) {
                              $final5{ $_ } = (sort keys %hash)[$z];
                              $z++;
                              }


Method 3:

                                %hash=(9,5,2,1,4,6);

                                @first1=values %hash;
                                @second2=keys %hash;
                                foreach my $a1 (@first1)
                                {
                                        my $a2= shift @second2;
                                       $final3{ $a1 } = $a2;
                                 }


Method 4:

                               %hash=(9,5,2,1,4,6);
                               @first=values %hash;
                               @second=keys %hash;
                               for($j = 0; $j < @first; $j++)
                               {
                                         $final{$first[$j]} = $second[$j];
                               }


Method 5:

                                %hash=(9,5,6,1);
                               @first_1=values %hash;
                               @second_1=keys %hash;
                               while( @first_1 || @second_1)
                              {
                                      my $a1= shift @first_1 || "Perl_Methods_Master";
                                      my $a2= shift @second_1 || "Perl_Methods_Master";
                                      $final{$a1} = $a2;
                               }



Method 6:

                                %hash=(9,5,2,1,4,6);
                               @first1=values %hash;
                               @second2=keys %hash;
                               while( @first1 && @second2)
                              {
                                      my $b1= shift @first1;
                                      my $b2= shift @second2;
                                      $final{$b1} = $b2;
                               }



Method 7:

                               %hash=(9,5,2,1,4,6);
                               @first2=values %hash;
                                @second1=keys %hash;
                               while (@first2 || @second1)
                               {
                                           $final{shift @first2} = shift @second1;
                                }


Method  8:

                               %hash=(9,5,2,1,4,6);
                               @first1=values %hash;
                               @second1=keys %hash;
                               $final{shift @first1}=shift(@second1) while @second1;



Method 9:

                             %final1=(9,5,2,1,4,6);
                             @first2=values %final1;
                             @second2=keys %final1;
                              $i=0;
                             %final2=map {$first[$i++],$_; } @second2;



Method 10:

                             %final5=(9,5,2,1,4,6);
                              while (($k, $v) = each %final5)
                             {
                                        $final2{$v}=$k;
                              }



Method 11: 
                   
                              %final5=(9,5,2,1,4,6);
                              %final5 = reverse %final5;

                      .

Labels: ,

how to get verified the hash and its elements availability in perl?

Method 1:

                          %final=(9,5,8,1,4,7);
                          print "not empty string" if exists($final{"keys"});

Method 2:

                          %final=(9,5,8,1,4,7);
                          if(delete $final{9})
                         {
                              print "empty deleted";
                          }
                          else
                         {
                              print "not exits";
                          }


Method  3:
                     
                          %final=(9,5,8,1,4,7);
                          if(%final)
                         {
                              print "string";
                          }
                          else
                         {
                              print "empty";
                          }


Method  4:

                           %final=(9,5,8,1,4,7);
                           if(!%final)
                         {
                              print "empty";
                          }
                          else
                         {
                              print "available";
                          }

kaavannan perl blogspot

Labels: ,

Monday 30 September 2019

How to assign more than one arrays to crate a complete hash in perl?


Method 1[Faster]:


                     %final=();
                    @first=(7,8,3,4);
                     @second=('a','s','d','f');
                     my %final;
                    @final{@first} = @second;
                     print %final;
Read more »

Labels: , , , , , ,

Wednesday 11 September 2019

How to print the elements of single hash in Perl without using sort builtin function in many methods?


Method 1:
                           %final=(9,2,1,4,7,6);
                           print "@{[%final]}";

Method  2:
                          %final=(9,2,1,4,7,6);
                          print %final,"\n";

Method  3:
                           %final=(9,2,1,4,7,6);
                           print "$_ $final{$_}\n" for (keys %final);

Method  4:
                           %final=(9,2,1,4,7,6);
                           while (my ($k,$v)=each %final){print "$k $v\n"}

Method  5:
                           %final=(9,2,1,4,7,6);
                           print "$_ => $final{$_}\n" for (sort keys %final);

Method  6:
                     
                          %final=(9,2,1,4,7,6);
                          @d=%final;
                           print @d;

Method  7:
                          %final=(9,2,1,4,7,6);
                          print map { "$_ $final{$_}\n" } keys %final;

Method  8:
                           %final=(9,2,1,4,7,6);
                           print map {$_ . " "} %final, "\n";

Method  9:

                           %final=(9,2,1,4,7,6);
                           foreach(keys %final) { print "$_   $final{$_}\n"; }

Method  10:
                           %final=(9,2,1,4,7,6);
                           map {print "$_  $final{$_}\n"; } keys %final;

Method 11:
                           %final=(9,2,1,4,7,6);
                           print "$_ $final{$_}\n" for keys %final;

kaavannan perl blogspot

Labels: , , , , ,

Tuesday 10 September 2019

How Many Possible ways to remove Duplicates in an array using perl?


Method 1:

sub filter {
    my %hash;
    grep !$hash{$_}++, @_;
}

my @array = qw(e f g h i);
my @final= filter(@array);


Method 2:

use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);


Method 3:

my %hash = ();
foreach my $item (@array)
{
    $hash{$item}++;
}
my @final = keys %hash;


Method 4:

my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;


Method 5:

%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;


Method 6:

my @array;
my @finals;

foreach my $var ( @array )
{
    if (! grep(/$var/, @finals ))
    {
          push( @finals, $var );
     }
}


Method 7:

my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;


Method 8:

my @final = keys {map {$_ => 1} @array};


Method 9:

@array = qw(e f g h i);
%hash1   = map { $_ => 1 } @array;
@final = keys %hash1;


Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
        $hash{$v}=1;
}
@final=keys%hash;


kaavannan perl blogspot

Labels: , ,