Dereferencing hash array is same as array dereferencing
it is used in some place like .json replys in website
Dereferencing hash-array
my %hash = %$location;
my $point=$hash{'point'};
my %hash = %$point;
my $coo=$hash{'coordinates'};
Perl
here you can get some logical details about your program
Search This Blog
Tuesday, February 1, 2011
join query for multiple table s
We can create more than one join in a query as follows:
SELECT a.Column1, b.Column1, c.Column1
FROM TableA a INNER JOIN
TableB b ON a.AID = b.AID INNER JOIN
TableC c ON b.BID = c.BID
ORDER BY a.Column1, b.Column1
The same query could be rewritten as:
SELECT a.Column1, b.Column1, c.Column1
FROM TableA a JOIN TableB b JOIN TableC c
WHERE a.AID = b.AID
AND b.BID = c.BID
ORDER BY a.Column1, b.Column1
Joining Three or More Tables
SQL Server 2008 R2
Other Versions
* SQL Server "Denali"
* SQL Server 2008
* SQL Server 2005
Although each join specification joins only two tables, FROM clauses can contain multiple join specifications. This allows many tables to be joined for a single query.
The ProductVendor table of the AdventureWorks2008R2 database offers a good example of a situation in which joining more than two tables is helpful. The following Transact-SQL query finds the names of all products of a particular subcategory and the names of their vendors:
Copy
SELECT p.Name, v.Name
FROM Production.Product p
JOIN Purchasing.ProductVendor pv
ON p.ProductID = pv.ProductID
JOIN Purchasing.Vendor v
ON pv.BusinessEntityID = v.BusinessEntityID
WHERE ProductSubcategoryID = 15
ORDER BY v.Name;
Here is the result set.
Name Name
--------------------------------------------------------
LL Mountain Seat/Saddle Chicago City Saddles
ML Mountain Seat/Saddle Chicago City Saddles
HL Mountain Seat/Saddle Chicago City Saddles
LL Road Seat/Saddle Chicago City Saddles
ML Road Seat/Saddle Chicago City Saddles
HL Road Seat/Saddle Chicago City Saddles
LL Touring Seat/Saddle Chicago City Saddles
ML Touring Seat/Saddle Chicago City Saddles
HL Touring Seat/Saddle Chicago City Saddles
HL Touring Seat/Saddle Expert Bike Co
ML Touring Seat/Saddle Expert Bike Co
LL Touring Seat/Saddle Expert Bike Co
HL Road Seat/Saddle First Rate Bicycles
LL Mountain Seat/Saddle First Rate Bicycles
ML Mountain Seat/Saddle First Rate Bicycles
LL Road Seat/Saddle Hill's Bicycle Service
ML Road Seat/Saddle Hill's Bicycle Service
HL Mountain Seat/Saddle Hybrid Bicycle Center
(18 row(s) affected)
Notice that one of the tables in the FROM clause, ProductVendor, does not contribute any columns to the results. Also, none of the joined columns, ProductID and VendorID, appear in the results. Nonetheless, this join is possible only by using ProductVendor as an intermediate table.
The middle table of the join, the ProductVendor table, can be called the translation table or intermediate table, because ProductVendor is an intermediate point of connection between the other tables involved in the join.
When there is more than one join operator in the same statement, either to join more than two tables or to join more than two pairs of columns, the join expressions can be connected with AND or with OR.
SELECT a.Column1, b.Column1, c.Column1
FROM TableA a INNER JOIN
TableB b ON a.AID = b.AID INNER JOIN
TableC c ON b.BID = c.BID
ORDER BY a.Column1, b.Column1
The same query could be rewritten as:
SELECT a.Column1, b.Column1, c.Column1
FROM TableA a JOIN TableB b JOIN TableC c
WHERE a.AID = b.AID
AND b.BID = c.BID
ORDER BY a.Column1, b.Column1
Joining Three or More Tables
SQL Server 2008 R2
Other Versions
* SQL Server "Denali"
* SQL Server 2008
* SQL Server 2005
Although each join specification joins only two tables, FROM clauses can contain multiple join specifications. This allows many tables to be joined for a single query.
The ProductVendor table of the AdventureWorks2008R2 database offers a good example of a situation in which joining more than two tables is helpful. The following Transact-SQL query finds the names of all products of a particular subcategory and the names of their vendors:
Copy
SELECT p.Name, v.Name
FROM Production.Product p
JOIN Purchasing.ProductVendor pv
ON p.ProductID = pv.ProductID
JOIN Purchasing.Vendor v
ON pv.BusinessEntityID = v.BusinessEntityID
WHERE ProductSubcategoryID = 15
ORDER BY v.Name;
Here is the result set.
Name Name
--------------------------------------------------------
LL Mountain Seat/Saddle Chicago City Saddles
ML Mountain Seat/Saddle Chicago City Saddles
HL Mountain Seat/Saddle Chicago City Saddles
LL Road Seat/Saddle Chicago City Saddles
ML Road Seat/Saddle Chicago City Saddles
HL Road Seat/Saddle Chicago City Saddles
LL Touring Seat/Saddle Chicago City Saddles
ML Touring Seat/Saddle Chicago City Saddles
HL Touring Seat/Saddle Chicago City Saddles
HL Touring Seat/Saddle Expert Bike Co
ML Touring Seat/Saddle Expert Bike Co
LL Touring Seat/Saddle Expert Bike Co
HL Road Seat/Saddle First Rate Bicycles
LL Mountain Seat/Saddle First Rate Bicycles
ML Mountain Seat/Saddle First Rate Bicycles
LL Road Seat/Saddle Hill's Bicycle Service
ML Road Seat/Saddle Hill's Bicycle Service
HL Mountain Seat/Saddle Hybrid Bicycle Center
(18 row(s) affected)
Notice that one of the tables in the FROM clause, ProductVendor, does not contribute any columns to the results. Also, none of the joined columns, ProductID and VendorID, appear in the results. Nonetheless, this join is possible only by using ProductVendor as an intermediate table.
The middle table of the join, the ProductVendor table, can be called the translation table or intermediate table, because ProductVendor is an intermediate point of connection between the other tables involved in the join.
When there is more than one join operator in the same statement, either to join more than two tables or to join more than two pairs of columns, the join expressions can be connected with AND or with OR.
Referencing and dereferencing array in perl
in perl we want pass more than one array in function means,first we reference the array element into scalar variable.then we pass the variable through function parameter.
Inside function we must dereference the scalar variable into array
Example for Referencing and dereferencing the array
my(@id,@name,@age);
push(@id,101);
push(@name,'hari');
push(@age,23);
push(@id,102);
push(@name,20);
push(@id,103);
push(@name,24);
#referencing array
my $id=\@id;
my $name=\@name;
my $age=\@age;
#function calling
&detail($id,$name,$age);
sub detail
{
my $id=shift;
my $name=shift;
my $age=shift;
#dereferencing array
@ids=@$id;
@name=@$name;
@age=@$age;
foreach @ids(my $val)
{
print $id;
}
}
Inside function we must dereference the scalar variable into array
Example for Referencing and dereferencing the array
my(@id,@name,@age);
push(@id,101);
push(@name,'hari');
push(@age,23);
push(@id,102);
push(@name,20);
push(@id,103);
push(@name,24);
#referencing array
my $id=\@id;
my $name=\@name;
my $age=\@age;
#function calling
&detail($id,$name,$age);
sub detail
{
my $id=shift;
my $name=shift;
my $age=shift;
#dereferencing array
@ids=@$id;
@name=@$name;
@age=@$age;
foreach @ids(my $val)
{
print $id;
}
}
encode/decode in perl
URL addresses only accepts alphanumeric characters and some punctuation symbols, like parenthesis and underscore.
If you need to use any other symbol (like space) you have to URL encode it using a percent sign followed by the two hexadecimal digits that represents that digit in the ASCII table.
For example, the space symbol is character 32 (hexadecimal 20) in the ASCII table, so it's expressed as %20.
In Perl, the easiest way to URL encode a string is to use uri_escape() function from URI::Escape module. This function converts all the unsafe symbols of a string to its URL encode representation.
Conversely, uri_unescape() converts a URL encoded string to its normal representation.
#!/usr/bin/perl
use URI::Escape;
my $string = "Hello world!";
my $encode = uri_escape($string);
print "Original string: $string\n";
print "URL Encoded string: $encode\n";
If you need to use any other symbol (like space) you have to URL encode it using a percent sign followed by the two hexadecimal digits that represents that digit in the ASCII table.
For example, the space symbol is character 32 (hexadecimal 20) in the ASCII table, so it's expressed as %20.
In Perl, the easiest way to URL encode a string is to use uri_escape() function from URI::Escape module. This function converts all the unsafe symbols of a string to its URL encode representation.
Conversely, uri_unescape() converts a URL encoded string to its normal representation.
#!/usr/bin/perl
use URI::Escape;
my $string = "Hello world!";
my $encode = uri_escape($string);
print "Original string: $string\n";
print "URL Encoded string: $encode\n";
unshift and shift (push /pop) in perl
In perl array handling using unshift and shift,this is same like as push and pop in array
Difference between (Push & Pop) and (Shift & Unshift)
To find out if the effect of push differs from the effect of unshift, try them both and see.
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl
use strict;
use warnings;
my @pusharray = ('original', 'contents');
push @pusharray, $_ foreach(1..10);
print "array contains @pusharray\n";
my @unshiftarray = ('original', 'contents');
unshift @unshiftarray, $_ foreach(1..10);
print "array contains @unshiftarray\n";
#!/usr/bin/perl use strict; use warnings; my @pusharray = ('original', 'contents'); push @pusharray, $_ foreach(1..10); print "array contains @pusharray\n"; my @unshiftarray = ('original', 'contents'); unshift @unshiftarray, $_ foreach(1..10); print "array contains @unshiftarray\n";
Output:
array contains original contents 1 2 3 4 5 6 7 8 9 10
array contains 10 9 8 7 6 5 4 3 2 1 original contents
Difference between (Push & Pop) and (Shift & Unshift)
To find out if the effect of push differs from the effect of unshift, try them both and see.
Perl Syntax (Toggle Plain Text)
#!/usr/bin/perl
use strict;
use warnings;
my @pusharray = ('original', 'contents');
push @pusharray, $_ foreach(1..10);
print "array contains @pusharray\n";
my @unshiftarray = ('original', 'contents');
unshift @unshiftarray, $_ foreach(1..10);
print "array contains @unshiftarray\n";
#!/usr/bin/perl use strict; use warnings; my @pusharray = ('original', 'contents'); push @pusharray, $_ foreach(1..10); print "array contains @pusharray\n"; my @unshiftarray = ('original', 'contents'); unshift @unshiftarray, $_ foreach(1..10); print "array contains @unshiftarray\n";
Output:
array contains original contents 1 2 3 4 5 6 7 8 9 10
array contains 10 9 8 7 6 5 4 3 2 1 original contents
grep
* grep BLOCK LIST
* grep EXPR,LIST
This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.
1. @foo = grep(!/^#/, @bar); # weed out comments
or equivalently,
1. @foo = grep {!/^#/} @bar; # weed out comments
Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables. Similarly, grep returns aliases into the original list, much as a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a foreach , map or another grep) actually modifies the element in the original list. This is usually something to be avoided when writing clear code.
If $_ is lexical in the scope where the grep appears (because it has been declared with my $_ ) then, in addition to being locally aliased to the list elements, $_ keeps being lexical inside the block; i.e., it can't be seen from the outside, avoiding any potential side-effects.
* grep EXPR,LIST
This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions.
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.
1. @foo = grep(!/^#/, @bar); # weed out comments
or equivalently,
1. @foo = grep {!/^#/} @bar; # weed out comments
Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables. Similarly, grep returns aliases into the original list, much as a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a foreach , map or another grep) actually modifies the element in the original list. This is usually something to be avoided when writing clear code.
If $_ is lexical in the scope where the grep appears (because it has been declared with my $_ ) then, in addition to being locally aliased to the list elements, $_ keeps being lexical inside the block; i.e., it can't be seen from the outside, avoiding any potential side-effects.
Tuesday, December 21, 2010
Threads in perl...
Thread Module
Every thread module includes
Use threads
Class methods
new() –create a new threads
create()-create a new thread
self() – Return my current thread object
list() – Return list of thread objects
Thread objects methods
join – propagate errors (harvest thread)
eval – trap errors (harvest a thread)
equal –compare two threads for identity
tid – return the internal thread id
Creation of threads
Using two methods we can create threads
1.New()
2.Create()
New() method
$thr=threads->new()
use threads;
$thr = threads->new(\&sub1);
sub sub1
{
print "welcome to all \n";
}
Every thread module includes
Use threads
Class methods
new() –create a new threads
create()-create a new thread
self() – Return my current thread object
list() – Return list of thread objects
Thread objects methods
join – propagate errors (harvest thread)
eval – trap errors (harvest a thread)
equal –compare two threads for identity
tid – return the internal thread id
Creation of threads
Using two methods we can create threads
1.New()
2.Create()
New() method
$thr=threads->new()
use threads;
$thr = threads->new(\&sub1);
sub sub1
{
print "welcome to all \n";
}
Subscribe to:
Posts (Atom)