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

Friday 25 October 2019

Perl Hashes - What is?

Perl Hash: 

Perl hash is un-ordered collection of elements, hash will not retrieve elements based on its index value instead it retrieves elements based on keys.

Hash will be created using curly {} braces and ( ) parenthesis and hash keys will never have repeated elements because it is unique formatting records.

Based on keys able to query faster results in large data. sort will be used for ordered and un-ordered  results.and we can  also perform numeric sorting on same results.

Read more »

Labels: ,

Thursday 24 October 2019

bless - place and plays in perl

WITHOUT   BLESS                                      WITH BLESS
                                                                       
                                                           package Class;
package Class;                                   sub new  { $classname = @_; 

sub new  { $classname = @_;                 $var = {};
$var= {};                                              bless($var, $classname);
return $var; }                                         return $var; }
sub sample { print("TEST"); }       sub sample{ print("TEST"); }
$var1 = Class->new();                       $var1 = Class->new(); 
$o->sample();                                        $o->sample();    
                                               
    
Output:                                                          Output:
Error                                                                TEST
            



Method 1:

sub new { 
  $classvar = shift; 
  $selfref = {};  
  bless $selfref, $classvar; 
}


Method 2:

sub new
{
$a={};
return bless $a;
}


Method 3:

sub new
{
$a={};
bless($a);
return $a;
}


Method 4:

sub new
{
$classvar = shift;
$a={};
bless($a,$classvar);
return $a;
}

Method 5:

sub new
{
$classname=shift;
$a={};
$a->{NAME}="apple";
bless $a,$classname;
}

Method 6:

sub new
{
$classname=shift;
$a=[];
$a->[0]=100;
bless $a,$classname;
return $a;
}

Method 7:

my $x = bless [], "packagename";
$x->functionname;

Method 8:

sub new {
    classvar = $_[0];
    my $var = $_[1]->{'name'};
 
    my $this = {
        'name' => $var,
    }; 
    bless($this, $classvar);
    return $this;
}

Method 9:

 $var1= "scalar";
$var2 = bless \$s, $classname;
return $var2;


Method 10:

my $obj = bless($ref, "Class");
    sub bless {
      my ($ref, $class) = @_;
      $ref->{classname} = $class;
      return $ref;
    }

Method 11:


sub new { 
  $classname = shift; 
  $self = [];  
  bless $self, $classname; 
}


Method 12:

sub new
{
$a=[];
return bless $a;
}

Wednesday 23 October 2019

place and plays $_ in perl?

Method 1:


while(<>)
{
    print;
}

Method 2:


for ('l','s','h','c','g')
{
   print;
   print "\n";
}

Method 3:

@a=split //,'hello';

Method 4:


$_="hello";
$_=~s/he/she/g;

print $_;

Method 5:


$_="he l lo";
for (split)
{
   print;
   print "\n";
}

Method 6:


$_='perlbot';

print if /p/;

Method 7:

foreach ('l','s','h','c','g')
{
   print;
   print "\n";
}

Method 8:


$_ = "prlbot";

print /perl/ ?'perlbot':'normalbot',"\n";

Method 9:


$_="perl";
$_=~tr/p/P/;

print $_;

Method 10:


@a=('l','s','h','c','g');

print grep $_,@a;

Method 11:


@a=('l','s','h','c','g');

print map $_,@a;

Method 12:


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

while (each %hash) {

 print "$_=$hash{$_}\n";

    }

Method 13:


opendir($_,"/tmp") || die "Can't open dir $!";

@array =readdir($_);

closedir $_;

print @array;

Method 14:

while(<>)
{
    chomp($_);
     print $_;
}

Method 15:


while(<>)
{
    chop($_);
    print $_;
}

Method 16:


while(<>)
{
   chomp($_);
   $output = eval $_;
   print $output;
}

Method 17:


$_="he l lo";

print length($_);

Method 18:


$_="he l k lo";
$v=reverse $_;

print $v;

Method 19:


$_ = 9.0;

print exp $_;

Method 20:


$_ = "10";

print hex $_;

Method 21:


$_ = "Hello Perl Bot";

print index $_, 'Perl';

Method 22:


$_="he l k lo";
$v=lc $_;

print $v;

Method 23:


$_="he l k lo";
$v=uc $_;

print $v;

Method 24:


$_="he l k lo";
$v=ucfirst $_;

print $v;

Method 25:


$_="he l k lo";
$v=lcfirst $_;

print $v;

Method 26:


$_ = "144";

print sqrt $_;

Method 27:


$_ = "Hello Perl Bot Perl";

print rindex $_, 'Perl';

Method 28:


while(($_ = getc) eq 'yes')
{
   print $_;
}

Method 29:


$_ = "Hello Perl Bot Perl";

if (defined $_) {
    print "$_ is defined";
}

Method 30:

$_ = "Hello Perl Bot Perl";

print $_;

Tuesday 22 October 2019

place and plays @_ in perl?

Method 1:

sub Addition{ $a=\@_; ($a,$b,$c)=@$a; $sum=$a+$b+$c; } Addtion(10, 20, 30); print $sum; #60



Read more »

Labels:

Monday 21 October 2019

how many ways to get last word of a string using regular expression in perl?

Method 1:

$tex="nice perl you are";
$tex=~/(\S+)$/;
print "$1\n";

Method 2:

$tex="nice perl you are";
$tex=~/(.*)\s+(\w+)$/g;
print "$2\n";

Method 3:

$tex="nice perl you are";
$tex=~/([A-Za-z]+)$/;
print "$1\n";

Method 4:

$tex="nice perl you are";
$tex=~/(\b[A-Za-z]+\b)$/;
print "$1\n";

Method 5:

$tex="nice perl you are";
$tex=~/([\w|.]+)$/g;
print "$1\n";

Method 6:

$tex="nice perl you are";
$tex=~/([\S|.]+)$/g;
print "$1\n";

Method 7:

$tex="nice perl you are";
$tex=~/([\S|.]+)\Z/g;
print "$1\n";

Method 8:

$tex="nice perl you are";
$tex=~/(.*)\s+(.*)$/g;
print "$2\n";

Method 9:

$tex="nice perl you are";
$tex=~/(\w+)\s+(\w+)$/g;
print "$2\n";

Method 10:

$tex="nice perl you are";
$tex=~/\S+\s+(\S+)$/g;
print "$1\n";

Method 11:

$tex="nice perl you are";
$tex=~/([^\s]*)$/g;
print "$1\n";

Method 12:

$tex="nice perl you are";
$tex=~/([\w\d\S._-]*)$/g;
print "$1\n";

Method 13:

$tex="nice perl you are";
$tex=~/(?:$|(?:\.\s))*(\w+)$/g;
print "$1\n";

Method 14:

$tex="nice perl you are";
$tex=~/(?m)\W*(\w+).*?(\w+)\W*$/;
print "$2\n";

Method 15:

$tex="nice perl you are";
$tex=~/\s*(\w+)[^;]+\s(\w+);?.*/g;
print "$2\n";



Sunday 20 October 2019

how can we empty an elements of an array using perl?

Method 1:

@a=(1,7,9,4);
$#a = -1;
print @a;


Method 2:

@a=(1,7,9,4);
@a = ();
print @a;


Method 3:

@a=(1,7,9,4);
$_=(@a) = ();
print @a;


Method 4:

@a=(1,7,9,4);
$_=\@a;
@$_=();
print @a;


Method 5:

@a=(1,7,9,4);
$_=\@a;
@$_=[];
@$_=();
print @a;


Method 6:

@a=(1,7,9,4);
@a= map /![^.]+/,@a;
print @a;



Method 7:

@a=(1,7,9,4)
@a= grep /![^.]+/,@a;
print @a;


Method 8:

@a=(1,7,9,4)
@a= /![^.]+/ ~~ @a
print @a;


Method 9:

@a=(1,7,9,4);
map $_=~s/.+//, @a;
print @a;


Method 10:

@a=(1,7,9,4);
grep $_=~s/.+//, @a;
print @a;


Method 11:

@a=(1,7,9,4);
map {  $_=~s/.+//g; } @a ;
print @a;


Method 12:

@a=(1,7,9,4);
grep {  $_=~s/.+//g; } @a ;
print @a;


Method 13:

@a=(1,7,9,4);
$_=~s/.+// for @a;
print @a;


Method 14:

@a=(1,7,9,4);
my @b=();
@a=@b;
print @a;


Method 15:

@a=(1,7,9,4);
for(@a)
{pop @a;shift @a;}
print @a;


Wednesday 16 October 2019

what will be the value if we directly print the reference in perl?

Method 1:

$ref=\"DAM-DAMA-DAMAAR";
print $ref;                     #SCALAR(0x111ac40)


Method 2:

$varr=6;
$ref="varr";
print "$ref";                 #varr


Method 3:

@b=(4,5,6);
$ref="b";
print "$ref";                 #b


Method 4:

%I=(c,d);
$ref="I";
print "$ref";            #I


Method 5:

sub FName
{

    print "DAM-DAMA-DAMAAR\n";

}

$ref="FName";
print $ref;                        #FName


Method 6:

$varr=7;
$ref=\$varr;
print $ref;                       #SCALAR(0x14bcc28)


Method 7:

@b=(4,5,6);
$ref=\@b;
print "$ref";             #ARRAY(0x23a1c28)


Method 8:

%f=(b=>c);
$ref=\%f;
print "$ref\n";      #HASH(0x22e3c28)


Method 9:

sub FName
{

    print "DAM-DAMA-DAMAAR\n";

}

$ref=\&FName;

print $ref;                         #CODE(0xa70c28)


Method 10:

sub FName
{

    my $b=shift;
    print $b;                   #GLOB(0x1fc6bf8)

}

open(FIL1,">b.txt");
name(\*FIL1);       


Method 11:

$b="DAM-DAMA-DAMAAR";
$ref=\substr($b,0,1);
print $ref;              #LVALUE(0x112af30)


Method 12:

$ref=[4,5,6];
print "$ref";         #ARRAY(0x17f2f30)


Method 13:

$ref=[qw/4 5 6/];
print "$ref";         #ARRAY(0xd3af30)


Method 14:

$ref=[qq/4 5 6/];
print "$ref";         #ARRAY(0x27a6f30)


Method 15:

$ref={c=>D,f=>U};
print $ref;            #HASH(0x1408f30)


Method 16:

$ref= sub {print "DAM-DAMA-DAMAAR\n"};
print $ref;                      #CODE(0x25b0c40)


Method 17:

$varr=55;
$ref=*varr{SCALAR};
print $ref;               #SCALAR(0x23f6c28)



Method 19:

sub FName
{
    print  "DAM-DAMA-DAMAAR\n";
}

$ref=*FName{CODE};
print $ref;                          #CODE(0x2699c28)


Method 20:

@a[0]=[1,2,9,4,5];
@a[1]=[1,2,9,4,5];
@a[2]=[1,2,9,4,5];
@a[3]=[1,2,9,4,5];


print @a;             
#ARRAY(0x20e0f30)ARRAY(0x20fe640)
ARRAY(0x210b370)ARRAY(0x210b418)


Method 21:

$h{1} = { c => "jhjhjh", m => "jhkjkjh", 
          r => "jgjhgjh", }; 
$h{2} = { a => "nbvbnv", b => "fdfdf",
          d => "nbmnbm", }; 
$h{3} = { k => "jhgjh", g => "jhhbm",
          h => "jhjh", }; 



print  %h;

   #3HASH(0xfdd640)1HASH(0xfbff30)2HASH(0xfc0128)

Tuesday 15 October 2019

How to run Linux Command Using Perl?

Method 1:

@a=print readpipe( "ls -l *" );

print @a;


Method 2:

@a=print system( "ls -l *" ); print @a;


Read more »

Labels:

Thursday 10 October 2019

how many ways to create file using perl?

Method 1:

open(FHE,">file1.txt");


Method 2:

readpipe( "touch kkm.txt" );


Method 3:

system("touch kkm.txt");


Method 4:

open(my $file, '|-', "touch kkm.txt");


Method 5:

 `touch kkp.txt`;


Method 6:

qx/touch kkm.txt/;


Method 7:[always execute exec at eof]

exec("touch kkm.txt");


Method 8:

readpipe( "echo  > file1.txt" );


Method 9:

system("echo  > file2.txt");


Method 10:

`echo  > file3.txt`;


Method 11:

qx/echo  > file4.txt/;


Method 12:

open(my $file, '|-', "echo  > file5.txt");


Method 13:[always execute exec at eof]

exec("echo  > file6.txt");


Method 14:

readpipe( "cat '' > cat1111.txt" );


Method 15:

system("cat '' > cat1112.txt");


Method 16:

`cat '' > cat1113.txt`;


Method 17:

qx/cat '' > cat1114.txt/;


Method 18:

open(my $file, '|-', "cat '' > cat1115.txt");


Method 19:[always execute exec at eof]

exec("cat '' > cat1116.txt");


Method 20:

readpipe( "print  > cat1111.txt" );


Method 21:

system("print  > cat1112.txt");


Method 22:

`print  > cat1113.txt`;


Method 23:

qx/print > cat1114.txt/;


Method 24:

open(my $file, '|-', "print  > cat1115.txt");


Method 25:[always execute exec at eof]

exec("print  > cat1116.txt");


Method 26:[always execute exec at eof]

exec "vim   >  file.txt";

Kill process by using any one kill command

pkill vim;


Method 27:[always execute exec at eof]

exec "vi  >  file.txt";

Kill process by using any one kill command

pkill vi;


Method 28:[always execute exec at eof]

exec "nano  >  file.txt";

Kill process by using any one kill command

pkill nano;


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

Saturday 5 October 2019

how many ways string concatenation in perl?

Method 1:

$v='a';
print "$v$v$v";


Method 2:

print "a"."string"."c";


Method 3:

$v='a';
print "a".$v."c";


Method 4:

$v='a';
print "$v".$v.$v;


Method 5:

$v='a';
print  join "", "/home/$v.txt";


Method 6:

$v='a';
print  join "", "/home/".a.".txt";


Method 7:

$v='a';
print  join "", "/home/$v.txt";


Method 8:

$v='a';
print  join "", "/home/${v}.txt";


Method 9:

print join('', 'AA',$var1, 'BB', $var2, 'CC');


Method 10:

print sprintf("%sAA%sBB%sCC%s", $var1, $var2);


Method 11:

$s .= $_ for ($v,$v,$v);
print $s;


Method 12:

@vars = ($v,$v,$v);
print join "",@vars;


Method 13:

push @str, $v, $v, $v;
print join '', @str;


Method 14:

@var = ($v,$v,$v);
$s = "@var";
print $s;


Method 15:

undef $";
print "@{[$v,$v,$v]}";


Method 16:

$v='a';
print join(' AA ',sprintf("%s%s", $v, $v),sprintf("%s%s", $v, $v),sprintf("%s%s", $v, $v));




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Friday 4 October 2019

how many types available for getting string variable length in perl?

Method 1:

my $c = map $_, $h =~ /(.)/g;


Method 2:

my $c = split '', $h;


Method 3:

my $count = () = "hello" =~ /./g;


Method 4:

print length $h;


Method 5:

$cnt = "hello" =~ tr/[a-z][A-Z][0-9]//;


Method 6:

$h='hello';
my $v=$h=~ s/[a-z]//g;


Method 7:

print  @_ = push(@_, map $_, (split '', 'hellgo'));


Method 8:

print  @_ = push(@_, grep $_, (split '', 'hellgo'));


Method 9:

print push(@_, grep $_, "hello" =~ /./g);


Method 10:

print push(@_, map $_, "hello" =~ /./g);


Method 11:

print push(@_, grep $_, reverse "hello" =~ /./g);


Method 12:

print push(@_, map $_, reverse "hello" =~ /./g);


Method 13:

print push(@_, map $_, (split '', 'hellgo'));


Method 14:


my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 15:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 16:

my $c = map $_, (reverse "hello" =~ /./g);
print $c;


Method 17:

my $c = grep/./, (reverse "hello" =~ /./g);
print $c;


Method 18:

my $c = map $_, (reverse split '', 'hello');
print $c;


Method 19:

my $c = grep/./, (reverse split '', 'hello');
print $c;


Method 20:

print scalar map $_, 'hello' =~ /(.)/g;


Method 21:

print scalar grep/./,'helloh' =~ /(.)/g;


Method 22:

print scalar  map $_, (reverse "hello" =~ /./g);


Method 23:

print scalar  map $_, (reverse split '', 'hello');


Method 24:

print scalar  map $_, (split '', 'hello');


Method 25:

print $r = @_= map $_, (split '', 'hello');


Method 26:

print $r = @_= grep $_, (split '', 'hello');


Method 27:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 28:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 29:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 30:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 31:

print $r = @_= map $_, (split '', 'hello');


Method 32:

print $r = @_= grep $_, (split '', 'hello');


Method 33:

print $r = @_= grep $_, (reverse "hello" =~ /./g);


Method 34:

print $r = @_= grep/./,'helloh' =~ /(.)/g


Method 35:

print $r = @_= map  $_, (reverse "hello" =~ /./g);


Method 36:

print $r = @_= map/./,'helloh' =~ /(.)/g


Method 37:

print scalar  map $_, (reverse split '', 'hello');


Method 38:

print length reverse  grep/./,'helloh' =~ /(.)/g;


Method 39:

print length reverse  map $_,'helloh' =~ /(.)/g;


Method 40:

print grep/./, (reverse "hello" =~ tr/[a-z][A-Z][0-9]//);


Method 41:

print map $_, (reverse "hello" =~ tr/[a-z][A-Z][0-9]// );




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Labels: , , , , ,

Thursday 3 October 2019

How to check perticular value is available in array using perl?


Method1:

if( grep( /^$value$/, @a ) ) {print "found\n"}


Method2:

for(@a) {if ($_ eq $value) {print "found\n"}}

Method3:

my %hash = map {$_ => 1} @a;
print "found\n" if(exists $hash{$value});

Method4:

print "found\n" if($value ~~ @a);


Method 5:

             foreach(split(/ /,join(" ",@array)))
             {
                  print "found"  if $_ eq 6;
             }


Method 6:[Take the Copy of Original Array]

$len=scalar(@a);

for($i=0;$i<=$len;$i++)
{
          for(shift @array){ print "found\n" if($_ == 11)}    
}


Method 7:[Take the Copy of Original Array]

$len=scalar(@a);
for($i=0;$i<=$len;$i++)
{
          for(pop @array){ print "found\n" if($_ == 11)}    
}


Method 8:[Take the Copy of Original Array]

$len=scalar(@array);
for($i=0;$i<=$len;$i++)
{
 print "found\n" if(splice(@array ,$i,1) eq 3);   
}

Method 9:

use List::MoreUtils (uniq);
@a = (1,2,3,9);
$value = 9;
print "found\n" if any {$_ eq $value} @a;



For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

Wednesday 2 October 2019

print final element of an array reference using perl?


.Method 1:

                       @array=(1,33,11,4,5);
                       $r=\@array;
                        print  "@$arr_ref [-1]";

Method 2:

                       @array=(1,33,11,4,5);
                       $r=\@array;
                        print  $arr_ref->[-1];

Method 3:

                       @array=(1,33,11,4,5);
                       $r=\@array;
                        print @$r[$t-1]=$t=scalar(@$r);

Method  4:

                       @array=(1,33,11,4,5);
                       $r=\@array;
                        print splice(@$r,scalar(@$r-1),1);


 Method  5:

                        @array=(1,33,11,4,5);
                        $r=\@array;
                        print @$r[($#=@$r-1)];

 Method  6:

                        @array=(1,33,11,4,5);
                        $r=\@array;
                        print @$r[scalar(@$r-1)];

Method 7:
                       @array=(1,33,11,4,5);
                        $r=\@array;
                        print splice(@$r,-1,scalar(@$r-1));




For More Methods Reach Me:@ letscrackperlinterviewblogspot@gmail.com  

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 exact hash key name by giving indexed value in perl?


Method 1:
             
                     %final9=(9,5,7,1,4,6);
                     @d=sort keys %final9;
                      print "\n$d[2]";


Method 2:
       
                     %final3=(9,5,7,1,4,6);
                      print ((sort keys %final3)[0]);


Method 3:

                      %final5=(1,5,9,1,3,6);
                      print ((%final5)[0]);

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