Thursday 28 October 2021

Top 500 unique perl programming interview questions and answers to master in perl regular expression programs - part1

 

  1. How can I remove leading and trailing white spaces from a string in Perl?
perl
1$str = " Hello World! "; 2$str =~ s/^\s+|\s+$//g; 3print $str; # Output: "Hello World!"
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: ,

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)

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

Thursday 21 October 2021

write 1000 uniq perl interview questions and answers to master in perl part 2

 251. Q: In Perl, how can you find the position of a substring within a string?

1A: In Perl, you can find the position of a substring within a string by using the `index` function. Here is an example: 2 3```perl 4use strict; 5use warnings; 6 7my $string = 'Hello, World! World is beautiful.'; 8my $substring = 'World'; 9 10my $position = index $string, $substring; 11 12if ($position == -1) { 13 print "The substring was not found in the string.\n"; 14} else { 15 print "The substring was found at position: $position\n"; 16} 17``` 18 19In this example, the script finds the position of the substring `'World'` within the string `'Hello, World! World is beautiful.'` and prints the resulting position.

Read more »

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