5% assure discount from here only

Tuesday, August 7, 2012

(31) How to concatenate hashref or merge hashref?

Solution:

Perl provide  very easy way to concatenate or merge hashref data.

my $a = {1 => 2, 3 => 4};
my $b = {5 => 6, 7 => 8};

my $c = {%$a, %$b};

print Dumper($c);

Output will be like this :-

$VAR1 = {
 '1' => '2',
 '3' => '4',
 '5' => '6',
 '7' => '8',
};

Tuesday, July 10, 2012

(30) How to read and print the content of file, using line number?

Solution:

Sometime we need to print or show content from file on basis of line number.

Suppose we have a file, which contains 1000 of lines and we need to print the content from line number 30 to 50.

Following example will show, how can we do this :-

open FILE, "/location/filename";
while(<FILE>){
if($. > 29 && $. < 51){ # Here $. represents the line number
print $_; # Here $_ represents the content
}
}
close FILE;