Tuesday, July 30, 2013

How do you access elements of hash containing an array and a hash?

my %employee = ( 
CSV_NAME => 'EMPLOYEE',
columns => [    
[EMP_ID => 'ID'],  
[EMP_NAME => 'ENAME'],   
],);

print $employee{CSV_NAME};
print "\n";
print map $_->[0], @{$employee{columns}};
print "\n";
print map $_->[1], @{$employee{columns}};

Friday, July 26, 2013

How do you send emails with attachment in UNIX?

uuencode <FileName> <FileName> | mailx -s "Subject" <eMailId> [-c CC_Address] [-b BCC_Address]

mutt -s "<Subject>" -a <Attachment_FileName> <eMailId> [-c CC_Address] < [/dev/null | MailBodyFileName]

How do you keep watching on a directory in UNIX?

watch -n 60 -d ls -lrt

Monday, July 22, 2013

How could you make variables exportable in ".pm" file?

use Exporter qw (import);

our @EXPORT_OK = ();

sub  _exportable(@)
{
     push @EXPORT_OK, @_;
}

our $dir_bin="/home/usr/bin";
_exportable '$dir_bin';

Thursday, July 18, 2013

Where can you format your xml and test the XPath online?

How do you permanently set colors for VI/VIM editor?

1. Go to your HOME directory using the following command
cd ~
or
cd $HOME
2. Create a file ".vimrc" (if not present in HOME directory) by typing
vi .vimrc
3. Thereafter make the following entries in insert mode
set nu
syntax on
highlight normal cterfg=white ctermbg=black
4. Save and exit

To temporarily override the attributes within vi, you may use
:set bg = [dark | light]
:set background = [dark | light]
:synatx [on | off]
:colorscheme [nature | desert | torte | pablo | evening]

Wednesday, July 17, 2013

How do you obtain last word of string in perl?

my $string = "www.regen.com/issn/vol/isssue/art";
$string =~ m/([^\/]+)$/;
print $1;

How do you access last element in split() in perl?

my $uri = "readytips.blogspot.in";
my $domain = ( split /\./, $uri )[-1];
print $domain;

How do you replace strings in URLs in perl?

use URI::Split qw(uri_split uri_join);

$lhost='readytips.blogspot.in';

my $uri='http://www.blogspot.in/2013/07/how-do-you-extract-string-between-two.html';
my ($protocol, $host, $path, $query, $fragment) = uri_split($uri);
my $uri = uri_join($protocol, $lhost, $path, $query, $fragment);

print $uri;

How do you extract string between two key words in perl?

my $title = "<locator>readytips.blogspot.in</locator>";
$title =~m/<locator>([\s\S]+?)<\/locator>/;
print $1;
$title =~m/<locator>(.*?)<\/locator>/;
print $1;
$title =~m/<locator>(.*+)<\/locator>/;
print $1;
$title =~ /<locator>\s+(.*)\s+<\/locator>/;
print $1;

Friday, July 12, 2013

How do you find the key of a value in hash in Perl?

%FM = (
              'Radio City' => '91.1',
              'Big FM' => '92.7',
              'Red FM' => '93.5',
              'Radio One' => '94.3',
              'Radio Mirchi' => '98.3',
              'Fever' => '104.0',
              'Oye FM' => '104.8',
              'FM Rainbow' => '107.1',
              'FM Gold' => '100.7',
             );

my ($key) = grep{ $FM{$_} eq '92.7' } keys %FM;
print $key;

Wednesday, July 3, 2013

How could you populate keys and values of a config file into hash of hashes?

$ini="config.ini";
my %conf = ();

open (INI, "$ini") || die "Can't open $ini: $!\n";
while (<INI>)
{
  chomp;
  if (/^\s*\[(\w+)\].*/)
  {
        $header = $1;
  }
  elsif (/^\W*(\w+)=?(\w+)\W*(#.*)?$/)
  {
    #put them into hash
    $conf{$header}{$1} = $2; 
  }
}
close (INI);

print $conf{'QA'}->{'ip'} . "\n";
---------------------------------------------------------
config.ini:

[DEV]
url=http://readytips.blogspot.in/dev/
ip=127.0.0.0
[QA]
url=http://readytips.blogspot.in/qa/
ip=127.0.0.1
[PROD]
url=http://readytips.blogspot.in/live/
ip=127.0.0.2

Tuesday, July 2, 2013

How do you access individual keys and values of a hash of array

%attributes = (
    'OS_List' =>  [ 'Linux', 'Windows', 'MAC' ],
    'Script_List' => [ 'Perl', 'Python', 'Ruby' ],
);

print join (",", @{$attributes{'OS_List'}}) . "\n";

print @{$attributes{'OS_List'}}[1] . "\n"; #Output : Windows