Archive for May, 2007

Finding an Intersection Between Arrays in Perl

Thursday, May 17th, 2007

As explained on perl monks and Pete Kruckenberg’s post…I needed a similar function that returns an intersection between two arrays in Perl.


#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my @array1 = (1, 2, 3);
my @array2 = (2, 3, 4);
my %original = ();
my @isect = ();

map { $original{$_} = 1 } @array1;
@isect = grep { $original{$_} } @array2;

print Dumper(@isect);

#outputs
#$VAR1 = 2;
#$VAR2 = 3;

The above code works with duplicates, meaning that duplicates in @array2 will be added as individual elements in the array @isect.

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)

Correctly submitting a blog post with a trackback url

Tuesday, May 15th, 2007

This post references Paid Links and the Effect on Google and should show up as a trackback in the original posts comment section.

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)

Renaming JavaScript Objects

Friday, May 4th, 2007
javascript:var foo = {baz: 'bizzer'};
var bar = foo;
bar.baz = 'bazzer';
alert(foo.baz + ' == ' + bar.baz);

As you can see the object’s property of ‘baz’ is updated in both foo object and bar object (a copy or reference to foo).

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)