How do we preserve the data as three unique arrays? An array can only contain scalars. Luckily we know that a reference is a scalar. Let's create an array of array references!
# we have separate arrays we want to combine
my @austin_toys = qw(perl pizzastone pilobolus);
my @justin_toys = ('hack saw', 'boots', 'grout');
my @reinhard_toys = ('OED', 'oatmeal', 'orange juice');
# we could do this: (an array of array refs)
my @toys = ( \@austin_toys, \@justin_toys, \@reinhard_toys );
print "Reinhard's 1st toy is a $toys[2]->[0]\n";
print "Reinhard's 1st toy is a $toys[2][0]\n";
# or this: (an array ref of array refs)
my $toys = [ \@austin_toys, \@justin_toys, \@reinhard_toys ];
print "Reinhard's 1st toy is a $toys->[2][0]\n";
Tuesday, September 25, 2007
Arrays of Arrays
Wednesday, September 19, 2007
Occurence of each value in a list.
Usual approach is
@list=(1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1);
for (@list) {
$count++ if $_ eq "3";
}
We can smarten this by using grep function.
$count = grep $_ eq "3", @list;
But when we need the number of 4 or 5, we must repeat the above again.
This can be made easy by hash in a single line as,
my %count;
$count{$_}++ for @list;
foreach $key (keys %count){
print $key."\t".$count{$key}."\n";
}
Tuesday, September 18, 2007
Regex - Replacement Count
When you bind a variable to a substitution, the expression as a whole will return the number of substitutions made, e.g. ,
$replacements =$myvar =~s/foo/bar/g;
$replacements will give you the total count of replacements.
Fast Way..
Need a fast way of getting rid of the second half of an array?
Simply divide $#array by two, where array is the name of the @array.
$#array /= 2;
Tuesday, September 11, 2007
Shortcuts with the Arrow Notation
Perl provides an alternate and easier-to-read syntax for accessing array or hash elements: the ->[ ] notation. For example, given the array's reference, you can obtain the second element of the array like this:
$rarray = \@array;
print $rarray->[1] ; # The "visually clean" way
instead of the approaches we have seen earlier:
print $$rarray[1]; # Noisy
#or
print ${$rarray}[1];
Similarly, you can use the ->{ } notation to access an element of a hash table:
$rhash = \%hash;
print $rhash->{"k1"};
#instead of ........
print $$rhash{"k1"};
# or
print ${$rhash}{"k1"};
Thursday, September 6, 2007
10 Perl modules for JAVA
Java
This module provides a framework to start a local, or connect to a remote, Java Virtual Machine (JVM). It includes support for VM localization, object creation, method invocation, event listeners and loops, and exceptions.
Use this module when you need to connect to a local or remote JVM through Perl.
Inline::Java
This module is one of the most useful for Java developers switching over to Perl, because it allows them to embed Java code as-is into a Perl script. This code is then automatically compiled and invoked as needed by the Perl interpreter. The module includes support for environment variables, type casting, shared JVMs, arrays, objects and exceptions.
Use this module when you need to embed Java code directly in a Perl script.
Java::Import
This module makes it easy to import and access Java classes from Perl. It includes the ability to call static methods, pass arguments and process return values, work with Java-based data structures, and handle exceptions.
Use this module when you need to access a Java class from a Perl script.
Java::Build
This module provides an alternative to Ant, the traditional Java build tool. It combines Perl's scripting abilities with the standard Java build/package toolset.
Use this module when you require greater control over the build/package process for a Java class.
Java::Swing
The Swing toolkit provides sophisticated GUI controls (toolbars, buttons, and selectors) for Java applications. This module provides an object-based interface to core Swing API calls, and includes support for extended components, listeners and other wrappers.
Use this module when you need to access Java Swing functions through Perl.
JDBC
This module provides an interface to various database systems using JDBC. It includes support for database connections, query execution and result set retrieval, prepared statements and error handling.
Use this module when you need to connect to an RDBMS through a JDBC "pipe", perhaps to make use of a custom JDBC driver class.
Java::SJ
This module provides a framework to customize Java VM configuration, making it possible to run multiple VMs, each with a different startup configuration and service profile. Configurations are expressed in XML.
Use this module when you need to run multiple Java VMs simultaneously on the same system.
Java::JVM::Classfile
This module reads and parses JVM classfiles and expresses the information within them as a series of objects. Various methods are available to retrieve specific information about class methods, properties and visibility.
Use this module to retrieve detailed information about the objects and classes defined in a JVM classfile.
Template::Plugin::Java
This module provides a framework for describing Java classes in XML and then transforming these XML descriptions into Java source code. The XML description may include descriptions of variables and methods, and includes support for type casting and arrays. Two operation modes are supported: command-line, and embedded within a template.
Use this module when you need to create class templates in a standard, easily-parseable format, perhaps for use on different platforms.
Java::JCR::Jackrabbit
This module makes it possible to connect to, and manipulate, a Jackrabbit (JCR) content repository through Perl. It includes support for custom nodes.
Use this module when you need to access a Jackrabbit content repository.