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 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 3 August 2021

Top 5000 python programs examples with solutions - Part 6

Program 61:

Write a program to add two list of strings using list comprehension?

lst1=['hello','welcome','india']
lst2=['everyone','all']
lst3=[a+b for a in lst1 for b in lst2]
print(lst3)

Read more »

Labels:

Sunday 8 March 2020

Perl - Backtick Tutorial

backticks are used to execute shell commands and capture the output. The general syntax for using backticks in Perl is:

$output = `command`;


Here, the command is enclosed in backticks, and the output is captured into the variable $output.

For example, if you want to capture the output of the ls command, you can use:

$files = `ls`;

The variable $files will now contain the output of the ls command.

You can also use variables in the command that is enclosed in backticks:

$dir = "/path/to/directory";

$files = `ls $dir`;

Here, the value of the $dir variable is used in the ls command.

Backticks can also be used in combination with other Perl functions to process the output. For example, you can split the output into an array:

$files = `ls`;

@file_list = split(/\n/, $files);

Here, the split function is used to split the output of the ls command into an array, with each element containing a file name.

Backticks can also be used in conditional statements. For example, you can check if a file exists using the test command:

$file = "myfile.txt";

if (`test -e $file`) {

    print "$file exists\n";

} else {

    print "$file does not exist\n";

}

Here, the test command is enclosed in backticks and used in the conditional statement to check if the file exists.

It's important to note that using backticks to execute shell commands can be a security risk if the input is not properly sanitized. Make sure to validate and sanitize any user input before using it in a shell command.

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

Saturday 14 January 2023

Perl Interview Questions and Answers - March 2023 Updated

John","Smith", 9999 9999, "42,  some street, some area", "bangalore",560038

in above strings "9999 9999" should be single string and "42,  some street, some area" should be single string and 560038 should be single string remaining separate variables write perl program in multiple methods

Sure, here are three different methods to accomplish the task:

Read more »

Labels:

Tuesday 21 April 2020

Top 10 example of using Vim or VI editor in UNIX and Linux

Vim or VI is a popular text editor used in UNIX and Linux systems. It is a powerful tool that allows users to edit and manipulate text files efficiently. In this article, we will discuss 10 examples of using Vim or VI editor in UNIX and Linux.

1. Opening a file

To open a file in Vim or VI editor, we can use the following command:

vim filename.txt

This command will open the file named filename.txt in Vim or VI editor.

Read more »

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