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 30 January 2023

Perl Data Structures - Arrays of Arrays

Creating Arrays of Arrays:

Method 1:

 @array=(["apple","orange"],["Mango","Banana"],["Papaya","Peach"]);

 print $array[0][1];

Read more »

Labels:

Tuesday 10 September 2019

How Many Possible ways to remove Duplicates in an array using perl?


Method 1:

sub filter {
    my %hash;
    grep !$hash{$_}++, @_;
}

my @array = qw(e f g h i);
my @final= filter(@array);


Method 2:

use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);


Method 3:

my %hash = ();
foreach my $item (@array)
{
    $hash{$item}++;
}
my @final = keys %hash;


Method 4:

my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;


Method 5:

%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;


Method 6:

my @array;
my @finals;

foreach my $var ( @array )
{
    if (! grep(/$var/, @finals ))
    {
          push( @finals, $var );
     }
}


Method 7:

my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;


Method 8:

my @final = keys {map {$_ => 1} @array};


Method 9:

@array = qw(e f g h i);
%hash1   = map { $_ => 1 } @array;
@final = keys %hash1;


Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
        $hash{$v}=1;
}
@final=keys%hash;


kaavannan perl blogspot

Labels: , ,

Friday 19 March 2021

perl data structures tutorial - part1

Perl provides a rich set of data structures that can be used to store and manipulate data. In this tutorial, I'll cover the most commonly used data structures in Perl, along with code examples.

Arrays

Arrays are ordered lists of scalar values. To declare an array, you use the @ symbol followed by the array name. Here's an example:

@my_array = (1, 2, 3, 4, 5);


You can access individual elements of the array using the array name followed by the index of the element in square brackets. Here's an example:

print $my_array[0]; # prints 1


You can also use the scalar function to get the number of elements in an array:

print scalar(@my_array); # prints 5


Read more »

Labels:

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  

Monday 15 April 2024

Component Iteration in React JSX Strategies for Seamless Repetition

 Transitioning from traditional templating to JSX in React can be a bit perplexing, especially when you’re accustomed to looping constructs like for. In JSX, which ultimately compiles down to JavaScript function calls, you need to employ a different approach to achieve repetitive rendering. Let’s explore various techniques to repeat components in JSX.

Read more »

Labels:

Friday 15 November 2019

perl array - what is?

Perl Arrays:

Array is a collection of ordered elements, which is assigned by more than one elements like scalars,strings,numbers,floating points etc.

Creating Arrays:

Method 1:


@array=(8,1,3,-6,2);

print @array;

Read more »

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: