Friday 25 October 2019

Perl Hashes - What is?

Perl Hash: 

Perl hash is un-ordered collection of elements, hash will not retrieve elements based on its index value instead it retrieves elements based on keys.

Hash will be created using curly {} braces and ( ) parenthesis and hash keys will never have repeated elements because it is unique formatting records.

Based on keys able to query faster results in large data. sort will be used for ordered and un-ordered  results.and we can  also perform numeric sorting on same results.

Read more »

Labels: ,

Tuesday 8 October 2019

how many methods for reference creation in perl?

Method 1:

$ref=\"HelloHi";
print $$ref;                     #HelloHi


Method 2:

$var=5;
$ref="var";
print "$$ref";                 #5


Method 3:

@a=(1,2,3);
$ref="a";
print "@$ref";                 #1 2 3


Method 4:

%h=(a,b);
$ref="h";
print "$$ref{a}";            #b


Method 5:

sub name
{
    print "Hi Lets Crack\n";
}

$ref="name";
&{$ref};                        #Hi Lets Crack


Method 6:

$var=5;
$ref=\$var;
print $$ref;                 #5


Method 7:

@a=(1,2,3);
$ref=\@a;
print "@$ref";              #1 2 3


Method 8:

%h=(a=>b);
$ref=\%h;
print "$$ref{a}\n";      #b


Method 9:

sub name
{
    print "Hi Lets Crack\n";
}
$ref=\&name;
&$ref;                           #Hi Lets Crack


Method 10:

sub name
{
    my $a=shift;
    print $a "Hi Lets Crack\n";
}
open(FILE,">a.txt");
name(\*FILE);     


Method 11:

@a=(1,2,3);
@b=(4,5,6);
@array= name(\@a,\@b);
print @array;       


Method 12:

$a="hellohi";
$ref=\substr($a,0,1);
print $$ref;              #h


Method 13:

$ref=[1,2,3];
print "@$ref";         #1 2 3


Method 14:

$ref=[qw/1 2 3/];
print "@$ref";         #1 2 3


Method 15:

$ref=[qq/1 2 3/];
print "@$ref";         #1 2 3


Method 16:

$ref={a=>A,
b=>B};
print $$ref{a};         #A


Method 17:

$ref= sub {print "Hello\n"};
&$ref;                      #Hello


Method 18:

$var=5;
$ref=*var{SCALAR};
print $$ref;                #5


Method 19:

$var = 1;
${*var{SCALAR}} = 5;
print $var;                  #5


Method 20:

sub name
{
    print  "Hi Lets Crack\n";
}
$ref=*name{CODE};
&$ref;                       #Hi Lets Crack



kaavannan perl blogspot

Labels: , , , , , ,

Wednesday 11 September 2019

How to print the elements of single hash in Perl without using sort builtin function in many methods?


Method 1:
                           %final=(9,2,1,4,7,6);
                           print "@{[%final]}";

Method  2:
                          %final=(9,2,1,4,7,6);
                          print %final,"\n";

Method  3:
                           %final=(9,2,1,4,7,6);
                           print "$_ $final{$_}\n" for (keys %final);

Method  4:
                           %final=(9,2,1,4,7,6);
                           while (my ($k,$v)=each %final){print "$k $v\n"}

Method  5:
                           %final=(9,2,1,4,7,6);
                           print "$_ => $final{$_}\n" for (sort keys %final);

Method  6:
                     
                          %final=(9,2,1,4,7,6);
                          @d=%final;
                           print @d;

Method  7:
                          %final=(9,2,1,4,7,6);
                          print map { "$_ $final{$_}\n" } keys %final;

Method  8:
                           %final=(9,2,1,4,7,6);
                           print map {$_ . " "} %final, "\n";

Method  9:

                           %final=(9,2,1,4,7,6);
                           foreach(keys %final) { print "$_   $final{$_}\n"; }

Method  10:
                           %final=(9,2,1,4,7,6);
                           map {print "$_  $final{$_}\n"; } keys %final;

Method 11:
                           %final=(9,2,1,4,7,6);
                           print "$_ $final{$_}\n" for keys %final;

kaavannan perl blogspot

Labels: , , , , ,