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




Method 1:


$h = 'perl reverse code';

grep $b=$_.$b,$h =~ /(.)/g; 

print $b;

Method 2:


$h = 'perl reverse code';

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

print $b;

Method 3:


$h = 'perl reverse code';

grep $b=$_.$b,split '', $h;

print $b;

Method 4:


grep $b=$_.$b,"perl reverse code" =~ /./g;

print $b;

Method 5:


$h = 'perl reverse code';

for(1..length($h))
{

  print substr($h, -$_, 1)
}

Method 6:


foreach($h =~ /./g)
{

$b=$_.$b

}
print $b;

Method 7:


foreach('perl reverse code' =~ /./g)
{

  print substr($h, -$_, 1)

}

Method 8:


for(split '', $h)
{

  $b=$_.$b;

}

print $b;


Method 9:


$h = 'perl reverse code';

$h=~ s/(.)/$b=$1.$b/eg;

print $b;

Method 10:


$h = 'perl reverse code';

print map substr($h, -$_, 1), 1..length($h);

kaavannan perl blogspot

Labels: ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home