Archive for the ‘Perl’ Category

Empty is not a non-digit character in Perl

Thursday, May 28th, 2009

empty is not a non-digit character “\D”
empty is not a digit character either “\d”

The wrong way to do it:


my $f;
$check = ($f =~ m/\D/ ) ? 'non-digit character' : 'assume digit';

The correct way to do it:


my $f;
$check = ($f =~ m/\d/ ) ? 'digit character' : 'assume non-digit';

References:

$ perldoc perlretut
$ perldoc perlre

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

Conditionally Checking Existence of a Value in Perl

Friday, June 1st, 2007

There are many half-baked methods used for properly testing whether or not a variable is defined. Following are a few examples of some good and not-so good methods with explanations.


if (exists $opts{'primary'} || exists $opts{'secondary'}) {
    $some_value = $opts{'primary'} || $opts{'secondary'};
}

The above logic is clean and easy to follow at first glance, although it may not be the most efficient way of executing the appropriate decision making block.

Considering the above is equivalent to the following block, you may notice a somewhat redundant conditional checking that ensues:


if (exists $opts{'primary'} || exists $opts{'secondary'}) {
    if ( exists $opts{'primary'} ) {
        $some_value = $opts{'primary'};
    } else {
        $some_value = $opts{'secondary'};
    }
}

Another added benefit is that if your value is “0″ or empty, it may actually not get assigned if you use the simple one line conditional assignment:


$some_value = $opt{’primary’} || $opt{’secondary’};

The “primary option” would be skipped if it is equal to “0″…which is in-fact a completely valid value, and demonstrates the “falsey-ness” and “truthy-ness” of specific values…more on that later.

Thanks to “Ani-_” in #perl for setting me straight on conditional checking.

So, to some it all up in one bullet-proof conditional block for assignment we write the following, which will also turn out to run faster because there is only one conditional check:


if ( exists $opt{'primary'} ) {
    $some_value = $opt{'primary'};
elsif ( exists $opt{'secondary'} ) {
    $some_value = $opt{'secondary'};
} else {
    return false;
}

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

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)