Tuesday, 31 January 2023

Perl Data Structures - Accessing Arrays of Arrays

Method 1:

@array=(["apple","orange"],

               ["Mango","Banana"],

               ["Papaya","Peach"]

);

 

$arrayref = $array[1];

 print ${$arrayref}[1];

Read more »

Labels:

Perl Datastructures - Hash of Hash

Perl hash of hashes is a data structure that consists of a hash that contains other hashes as values. Each hash contained in the main hash can be accessed using a unique key.

Here's an example of how to create a hash of hashes in Perl:

my %HoH = (
   'John' => {
      'age' => 35,
      'occupation' => 'Programmer',
      'salary' => 50000,
   },
   'Mary' => {
      'age' => 27,
      'occupation' => 'Designer',
      'salary' => 40000,
   },
   'Mark' => {
      'age' => 42,
      'occupation' => 'Manager',
      'salary' => 70000,
   },
);
Read more »

Labels:

Perl Data structures - Hash of Arrays

Method 1:

$HoA = {
    flintstones => [ "fred", "barney" ],
    jetsons => [ "george", "jane", "elroy" ],
    simpsons => [ "homer", "marge", "bart" ],
    };

foreach (keys %{$HoA})
{
    print "$_ => @{$HoA->{$_}}\n";
}
Read more »

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:

Sunday, 15 January 2023

Perl Interview Questions and Answers - March 2023 Updated 2

 Find Lowest Missing Numbers in perl

The given program is a Perl script designed to find the lowest missing numbers from an array of integers. The script begins by initializing an array of integers named "@arr", which contains a set of random integers. The script then creates a hash called "%hash" that maps each integer in "@arr" to the value 1.

Next, the script finds the maximum integer in "@arr" using the "sort" function and saves it to a variable named "$max". It also sets the value of the minimum integer to -1, and initializes an empty array called "@missing" to store the missing numbers.

Read more »

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, 10 January 2023

How to Upgrade CPAN in Latest versions using perl?

 


Method1:
 
perl -MCPAN -e "upgrade /(.\*)/"

Method2:
 
in cpan shell type "cpan upgrade /(.*)/"

Method3:
 
cpan-outdated -p | cpanm

Method4:
 
cpanm App::cpanoutdated

Labels: , , , ,

Thursday, 5 January 2023

what is the use of ref() in perl?


Perl provides the ref() function so that you can check the reference type before dereferencing a reference.

1.SCALAR
2.ARRAY,
3.HASH,
4.CODE,
5.REF,
6.GLOB,
7.LVALUE,
8.FORMAT,
9.IO,
10.VSTRING,
 
these are the return values of ref() reference function.

Labels: , , , , , , ,

Sunday, 1 January 2023

Without changing the original,how do we perform a replacement on a string using perl regex?



Method1:
 
(my $newstring = $oldstring) =~ s/foo/bar/g;

Method2:
 
my $newstring = $oldstring =~ s/foo/bar/gr;   

Method3:
 
if it is an arrays:
my @a = ('appl', 'appl bal', 'fruit?');
my @b = map { s/appl/apple/; $_ } map { $_ } @a;
 

Labels: , , , , , ,