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

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

Saturday 1 January 2022

Example of sorting an ArrayList in reverse order in Java

Hi, Today program demonstrates how to sort an ArrayList of integers in reverse order in Java using the Collections.sort method with the Collections.reverseOrder() method. It creates a new ArrayList of integers, adds four elements to it, and prints the contents of the ArrayList before and after sorting in reverse order.

Read more »

Labels: , ,

Wednesday 6 November 2019

perl - array and hash common differences

     S.No
                             ARRAYS
                           HASHES

         1.

         2.


         3.

          
          4.

     
          


          5.

          



         6.





           7.


           
         8.


















          

        9.






      10.









      11.




     12.



      13.






       14.






       15.

It is ordered data collection

Data Can be accessed based on index value

Stack operations performed using push,pop,shift,unshift

Duplicate Elements will be exist in Array




For Large data, Arrays will not be in speed as getting values from hash keys.


Arrays can be created using () brackets @array=(1,3,5,6,2);
For More methods refer this link.

for accessing array index
$array[1];


Array can be accessed in loops as below

1.For(@array)
{
}

2.foreach(@array1,@array2)
   {
       Print $_;
   }

3.print grep{},@array;

4. print map{},@array;







Array slices can be used as below
@array[2];





To find length of an array


 $#array+1;
       or
scalar(@array);
      or
$a=@array;


For Emptying Array
$#array = -1;
     or
@array = ( );

To reverse an array
print reverse @array;


To sort array Elements
ascending order
@a=sort{$a <=> $b }@array;
desending order
@a=sort{$b <=> $a }@array;


To add array elements
$array[0]='55';




To Delete array elements
delete $array[1];
 
It is unordered Data Collection

Data Can be accessed based on Key value

Stack operations cannot be performed


Keys will not have Duplicate elements but Values would have Duplicate elements.

Based on hash Keys Data retrival would be fast


Hash is also possible to create using () bracket %hash=(1,3,5,6,2);
For More methods refer this link.
https://kaavannan-perl.blogspot.com/2019/10/perl-hashes-what-is.html


For accessing hash index use the key name
$hash{1};


Hash can be accessed based on keys and values


While(($k,$vals) = each %h)
{
    Print “$k,$vals\n”;
}

Foreach $k (keys %h)
{
Print “$k => $hash{$k}\n”;
}

Foreach $values (values %hash)
{
Print “$values\n”;
}



Hash slices can be used as below,
$hash{'1'}







To find length of hash

@array=keys %hash;
scalar(@array);
     or
$#array+1;
     or
$a=@array;



For Emptying hash
%hash=( );




To reverse a hash
print reverse %hash;



To sort hash elements

 foreach $k (sort keys %hash)
{
   print "$k => $hash{$k}\n";
}



To add hash Elements
$hash{1}='one';





To Delete hash elements
delete($hash{'1'});


Labels:

Monday 21 December 2020

Top 10 example of sort command in UNIX or Linux

Sorting is an essential task when working with data, especially large datasets. In Unix and Linux systems, the 'sort' command is used to sort files or data streams. In this blog article, we will explore ten examples of the 'sort' command that are frequently used in Unix and Linux systems.

Example 1: Sort a File Alphabetically

To sort a file alphabetically, you can use the following command:

sort file.txt

This command will sort the contents of 'file.txt' in alphabetical order and print the result on the console.

Read more »

Labels: , , ,

Sunday 18 April 2021

Top 5000 python programs examples with solutions - Part 1

Program 1:

Print the last character or element of an identifier?

var=list("print the sample sentence")
c=var[-1]
print(c)

Read more »

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

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: