Author:
erics , August 19th, 2011
A small step forwards in understanding how Perl handles hashes as arguments. This biggest difference is that the first way “slurps” in ALL passed values in @_ into the %args hash. The second (cooler) way, pulls in the arguments hash as a single scalar variable, allowing multiple variables to be passed via @_, if you […]
Categories: How-To's , Technology Tags: Argument , Hash , howto , perl , Subroutine , tips
| No comments
Author:
erics , May 18th, 2011
use Tie :: IxHash ;
my $ myHash = { } ;
tie ( % { $ myHash } , 'Tie::IxHash' ) ;
$ myHash -> { 'a' } = 'apple' ;
$ myHash -> { 'b' } = 'blueberry' ;
Tie:IxHash CPAN Manual Page
Categories: How-To's , Technology Tags: Hash , IxHash , perl , Tie , Tie::IxHash
| No comments
Author:
erics , May 15th, 2011
Coming from the Perl world, I wanted to emulate the following code in JavaScript:
my $ myHash = {
'foo' = > 'Hello' ,
'bar' = > 'World' ,
} ;
foreach my $ myKey ( keys % $ myHash ) {
print $ myKey , '=' , $ myHash -> { $ myKey } ;
}
What I found is that JavaScript uses “Objects” to contain hash-style data:
// Define the hash object on the fly
var myHash = {
foo : "Hello" ,
bar : "World"
} ;
// loop through all keys
for ( var myKey in myHash ) {
alert ( myKey + '=' + myHash [ myKey ] ) ;
}
// access a key individually
alert ( 'foo=' + Hash . foo ) ;
alert ( 'bar=' + Hash . bar ) ;
Also, one may use variables as key names by using the square-bracket notation, which evaluates variables first:
var myVar = 'test' ;
//These two lines are equivalent as long as myVar is 'test':
myHash [ myVar ] = 'theValue' ;
myHash . test = 'theValue' ;
Categories: How-To's , Technology Tags: array , Hash , Iterate , javascript , Keys , Loop , Object , perl
| No comments
Author:
erics , May 5th, 2011
my $ value = $ self -> myFunction ( ) -> { 'result' } ;
Where :
sub myFunction {
my $ self = shift ;
. . . code here . . .
return {
'status' = > 0 ,
'result' = > 'Hello World' ,
} ;
}
Categories: How-To's , Technology Tags: Hash , howto , perl , Return , Subroutine
| No comments