Perform regular expression on a PERL PDL variable

377 Views Asked by At

Is it possible to perform a regular expression on a n-dimension PDL variable?

For example I can add 100 to all the elements by doing

$a1 = pdl [1,2]; 
print $a1 + 100;

However what if my array was a bunch of strings that I would like to perform some task on. For example this fails:

$a = pdl ['suze','david'];
$a =~ s/suze/female/;
print $a;

Not sure if this is even possible, but thanks in advance.

2

There are 2 best solutions below

0
On

The point is, that PDL is a perl extension that is designed for scientific and bulk numeric data processing and display. So its really not made for String manipulating. When you try to iterate through a piddle:

use strict;
use warnings;
use PDL;

my $a = pdl ['suze','david'];
print $_ . "\n" foreach ($a->list);

You will get:

Argument "suze" isn't numeric in subroutine entry at Basic/Core/Core.pm.PL (i.e. PDL::Core.pm) line 1296, <DATA> line 207.
0
0
Argument "david" isn't numeric in subroutine entry at Basic/Core/Core.pm.PL (i.e. PDL::Core.pm) line 1296, <DATA> line 207.

When you take a deeper look into the POD, you will find:

$a = pdl(SCALAR|ARRAY REFERENCE|ARRAY|STRING);

For the constructor and the followed text for STRING

The string version of pdl also allows you to use the strings bad, inf, and nan, and it will insert the values that you mean (and set the bad flag if you use bad). You can mix and match case, though you shouldn't.

Back to your main problem, why do you use PDL for strings? - Why not using a simple Array?

use strict;
use warnings;
use Data::Dumper;

my $a = ['suze','david'];
s/suze/female/ for @{$a};
print Dumper $a;

Output:

$VAR1 = [
          'female',
          'david'
        ];
1
On

As a follow-up to Paulchenkiller's answer, PDL has rudimentary support for strings via PDL::Char. However, each string in a PDL::Char array must have the same length, or be null padded. Your example changes the length of the first entry to something longer than you had previously allocated. This is one reason you cannot apply a regex against a PDL::Char.

Furthermore, Perl's strings have almost no extraneous memory and looping over the elements in an array oughtn't be too costly when you're applying a regex to each member of the array.