Tuesday, August 27, 2013

How do you select 10 random rows in PostgreSQL?

SELECT *
FROM <TableName>
ORDER BY RANDOM()
LIMIT 10

How do you select top N records in PostgreSQL?

SELECT *
FROM <TableName>
LIMIT N;

Monday, August 19, 2013

How could you select only a row in Perl-DBI?

use strict;
use warnings;

use DBI;

my $dbh=DBI->connect("dbi:Pg:database=DATABASE_NAME;host=HOSTNAME;port=5678", 'USERNAME', 'PASSWORD')
or die "Can't connect: ", $DBI::errstr;
$dbh->{RaiseError} = 1;

my $sql = q( select *
from <TABLENAME>
where <CONDITION>);

my @row = $dbh->selectrow_array($sql);

print join (",", @row) . "\n";

$dbh->disconnect();

Wednesday, August 14, 2013

What is the best way to traverse through hashes in Perl?

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

while (my ($Frequency, $Station) = each %FM) {
    print "$Frequency =  $Station\n";   
}

Thursday, August 8, 2013

What are the nitty-gritties for extensions of perl programs?

  1. Technically, the ".pl" extension must be used for Perl libraries, not for executable Perl programs.
  2. For executable, you should either specify ".plx" or no file extension at all if  OS allows it.
  3. Perl modules must use the ".pm" file extension.
It is good coding paradigm to use only alphanumeric characters and underscores in Perl script file-names, and to start those file-names with a letter (or underscore), similar to how you would start variable names.

What are the aliases for Regular Expressions?

Regular Expressions are also referred as "regexen" or "regexes" in the plural ("regex" singular). An alternate version of "regex" is "regexp".

What are the differences between "GET" and "POST"?

POST & GET are HTTP request methods to establish communications between clients and servers.

GET:
  • GET method exposes parameters being used in the address bar. This can be useful to bookmark the page.
  • The request gets cached by the browser which can be a potential threat while dealing with sensitive information like passwords.
  • GET method is not suitable for very large variable values (max URL length is 2048 chars).
  • It supports ASCII only.
  • GET requests should only be used to retrieve data.

POST:
  • POST method encapsulates information passed to the server.
  • The variables can be of almost any length (no limits on the amount of information to send).
  • It is impossible to bookmark the page.
  • It supports ASCII & Binary data as well.
  • POST requests should be used only to send data.

How can we map country, state & cities in a hash?

my %geo = (
INDIA => {
OR => [ qw ( Bhubaneswar Cuttack Rourkela Brahmapur Sambalpur Balasore Puri Bhadrak Baripada ) ],
AP => [ qw ( Adilabad Adoni Amravati Anantapur Cuddapah Eluru Guntur Hyderabad Karimnagar Khammam Kothagudem Kurnool Lepakshi NagarjunaSagar Nagarjunakonda Nalgonda Nellore Nizamabad Puttaparthi Srikakulam Tirupati Vijayawada Visakhapatnam Warangal ) ],
MH => [ qw (Mumbai Pune Aurangabad Nashik Kolhapur Nagpur Amaravati Ahmadnagar Thane Chandrapur Solapur) ],
GJ => [ qw ( Ahmedabad Ankleshwar Atul Anand Ambaji Bharuch Baroda Bilimora Bhavnagar Dwarka gandhinagar Jamnagar Junagadh Kutch Mehsana Morbi Patan Nadiad Navsari Porbandar Palanpur Rajkot Surat Valsad Vasad Vapi) ],
},
);

foreach my $country (keys %geo)
{
  print " Country : " . "$country \n";
  foreach my $state (keys %{$geo{$country}})
  {
    print " State : " . "$state\n City : ";
    foreach $city ( @{$geo{$country}->{$state}} )
    {
      print $city . "\t" ;
    }
    print "\n";
  }
}

What is the difference between "implode" and "explode" in PHP?

"explode" splits a string into fragments specified by delimiter whereas "implode" conglomerates words separated by delimiter to form a string.

<html>
<head>
<title>Difference between explode and implode.</title>
</head>
<body>
<?php
$mNumber = "+91-988-620-2991";
$fragments = explode("-", $mNumber);
echo "Country Code : $fragments[0]";

echo '<br>';

$string = array("My", "name", "is", "Rabindra.");
$fullString = implode(" ", $string);
echo $fullString;
?>
</body>
</html>

Tuesday, August 6, 2013

Where does PHP store global variables?

PHP stores all global variables in an array called $GLOBALS[index].

<?php
$x=5;
$y=10;

$GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; //Same as "$y = $x + $y;"

echo $y;
?>