Tuesday 6 March 2012

Perl Hash Examples

I have a friend at work who occasionally writes in Perl and so has been learning the language over quite a while, picking up new bits when he needs to write something else. I'm generally his Perl sounding-board (among other topics) and he's getting pretty good at writing things from scratch himself now, even asking questions on things I've not done with Perl, not that I'm a massively advanced Perl programmer myself.

One of the things my friend regularly needs help with and I think confuses a lot of people with Perl is with variables, and in particular hashes. People often say to me they're not sure when to use dollar, percent or at-sign in their Perl variables. I knocked together some pretty noddy code to illustrate the various ways a hash can be used in Perl code as an example for him and thought it might be useful to a wider audience so posting it here.

The source code is below with the output from running this code presented afterwards. Syntax highlighting was done courtesy of perltidy.

#!/usr/bin/perl -w
#
# Some noddy hash examples
# Note: the syntactical differences can be quite subtle
# Note: hashes don't have guaranteed ordering (run this code)

use strict;

################################################################################
# lets play with a hash
################################################################################

# define a variable as a hash with some content
my %hash = (
    'a' => 1,
    'b' => 2,
    'c' => 3
);

print "print the hash value assigned to the key 'a'\n";
print $hash{"a"}."\n\n";

print "print all of the hash values\n";
foreach my $key (keys %hash) {
  print $hash{$key}."\n";
}

print "\nprint all of the hash values in order\n";
foreach my $key (sort keys %hash) {
  print $hash{$key}."\n";
}

print "print all of the hash values with associated keys\n";
foreach my $key (keys %hash) {
  print $key . " is " . $hash{$key}."\n";
}

print "\nprint all of the hash values with associated keys in order\n";
foreach my $key (sort keys %hash) {
  print $key . " is " . $hash{$key}."\n";
}

# get rid of the hash so we're 100% sure we're not using it below!
undef %hash;


################################################################################
# lets play with a hash reference
################################################################################

# define a variable as a hash reference with some content
my $hashref = {
    'a' => 1,
    'b' => 2,
    'c' => 3
};

print "\n\nprint the hash reference value assigned to the key 'a'\n";
print $hashref->{"a"}."\n\n";

print "print all of the hash reference values\n";
foreach my $key (keys %{$hashref}) {
  print $hashref->{$key}."\n";
}

print "\nprint all of the hash reference values in order\n";
foreach my $key (sort keys %{$hashref}) {
  print $hashref->{$key}."\n";
}

print "print all of the hash reference values with associated keys\n";
foreach my $key (keys %{$hashref}) {
  print $key . " is " . $hashref->{$key}."\n";
}

print "\nprint all of the hash reference values with associated keys in order\n";
foreach my $key (sort keys %{$hashref}) {
  print $key . " is " . $hashref->{$key}."\n";
}


The output from running this code is below:

print the hash value assigned to the key 'a'
1

print all of the hash values
3
1
2

print all of the hash values in order
1
2
3
print all of the hash values with associated keys
c is 3
a is 1
b is 2

print all of the hash values with associated keys in order
a is 1
b is 2
c is 3


print the hash reference value assigned to the key 'a'
1

print all of the hash reference values
3
1
2

print all of the hash reference values in order
1
2
3
print all of the hash reference values with associated keys
c is 3
a is 1
b is 2

print all of the hash reference values with associated keys in order
a is 1
b is 2
c is 3

Thursday 1 March 2012

Failing to Invent

We IBM employees are encouraged, indeed incented, to be innovative and to invent.  This is particularly poignant for people like myself working on the leading edge of the latest technologies.  I work in IBM emerging technologies which is all about taking the latest available technology to our customers.  We do this in a number of different ways but that's a blog post in itself.  Innovation is often confused for or used interchangeably with invention but they are different, invention for IBM means patents, patenting and the patent process.  That is, if I come up with something inventive I'm very much encouraged to protect that idea using patents and there are processes and help available to allow me to do that.


This comic strip really sums up what can often happen when you investigate protecting one of your ideas with a patent.  It struck me recently while out to dinner with friends that there's nothing wrong with failing to invent as the cartoon above says Leibniz did.  It's the innovation that's important here and unlucky for Leibniz that he wasn't seen to be inventing.  It can be quite difficult to think of something sufficiently new that it is patent-worthy and this often happens to me and those I work with while trying to protect our own ideas.

The example I was drawing upon on this occasion was an idea I was discussing at work with some colleagues about a certain usage of your mobile phone [I'm being intentionally vague here].  After thinking it all through we came to the realisation that while the idea was good and the solution innovative, all the technology was already known available and assembled in the way we were proposing, but used somewhere completely different.

So, failing to invent is no bad thing.  We tried and on this particular occasion decided we could innovate but not invent.  Next time things could be the other way around but according to these definitions we shouldn't be afraid to innovate at the price of invention anyway.