I finally got some time to start reading the (very nice) PDL-Book. I
started experimenting with get_dataref and upd_data. I believe that
they don't work fully with children piddles (for example, with slices),
only with the parents. For example, you can fetch the data but you
cannot update it when the pdl is a slice. This is illustrated by the
small program below, which uses get_dataref and upd_data to multiply a
pdl by two and then attempts to multiply a slice:

#!/usr/bin/env perl 
use v5.16;
use warnings;
use strict;
use PDL;
use PDL::NiceSlice;
my $a=sequence(10); #Initialize a pdl
say "\$a=$a";
my $aref=$a->get_dataref; #get ref to its data
say "\$\$aref contains ", join " ", unpack "d*", $$aref; 
$$aref=pack "d*", map 2*$_, unpack "d*", $$aref; #multiply by two
say "Now \$\$aref contains ", join " ", unpack "d*", $$aref;
$a->upd_data; #update pdl
say "Now \$a=$a"; 
my $b=$a->(0:-1:2); # take a slice
say "\$b=\$a->(0:-1:2)=$b"; 
my $bref=$b->get_dataref; #get ref to its data
say "\$\$bref contains ", join " ", unpack "d*", $$bref;
$$bref=pack "d*", map 2*$_, unpack "d*", $$bref; #multiply by two
say "Now \$\$bref contains ", join " ",unpack "d*", $$bref; 
$b->upd_data; #update pdl
say "But \$b is still $b";
say " and \$a is still $a"; 


whose output is

$a=[0 1 2 3 4 5 6 7 8 9]
$$aref contains 0 1 2 3 4 5 6 7 8 9
Now $$aref contains 0 2 4 6 8 10 12 14 16 18
Now $a=[0 2 4 6 8 10 12 14 16 18]
$b=$a->nslice([0,-1,2])=[0 4 8 12 16]
$$bref contains 0 4 8 12 16
Now $$bref contains 0 8 16 24 32
But $b is still [0 4 8 12 16]
 and $a is still [0 2 4 6 8 10 12 14 16 18]

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to