roark has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Being new to PDL, I was wondering how can I replace non-existant values with 0, while reading them.

Suppose I have the following tab seperated text file (a.txt):

1 0 0.1000 0.1000 0.8000
1 0 0.1000 0.1000 0.2750 0.5250
2 0 0.1000 0.1000 0.8000

then I use the following perl function:


($x, $y, @arr_I) = rcols 'a.txt', { COLSEP => "\t",PERLCOLS => 0 } ;
$x includes the first column and $y includes the second. @arr_I includes colums 3,4,5. However I would like it to include the 6 the column as well with ( 0, 0.5250, 0).

I was wondering if there is any way to do that. Thanks in advance!

  • Comment on PDL rcols : Replacing missing values with 0

Replies are listed 'Best First'.
Re: PDL rcols : Replacing missing values with 0
by Eily (Monsignor) on Aug 16, 2013 at 23:36 UTC

    You could do that with an array slice:

    use Data::Dumper; my (@array, $scalar); ($scalar, @array[0..3]) = qw/a b c d/; print Dumper \@array;
    $VAR1 = [ 'b', 'c', 'd', undef ];

    Since it's to read a file with *custom separator* separated values, maybe Text::CSV could suit you as well.

Re: PDL rcols : Replacing missing values with 0
by aitap (Curate) on Aug 18, 2013 at 11:36 UTC

    Try specifying the column numbers you want to read as rcols 'filename', $column_number, $another_column_number, { OPTIONS => 'values' }:

    pdl> print `cat test.ts`; 1 0 0.1000 0.1000 0.8000 1 0 0.1000 0.1000 0.2750 0.5250 2 0 0.1000 0.1000 0.8000 pdl> ($a, $b, @rest) = rcols 'test.ts', 0..5, { COLSEP => "\t", PERLCO +LS => [0] }; print pdl($a), $b, pdl @rest; Reading data into piddles of type: [ Double Double Double Double Doubl +e ] Read in 3 elements. [1 1 2] [0 0 0] [ [ 0.1 0.1 0.1] [ 0.1 0.1 0.1] [ 0.8 0.275 0.8] [ 0 0.525 0] ] pdl>
    Is this what you want?

    Edit: changed the way of printing the gathered values a bit

      Yes, thanks a lot!!
Re: PDL rcols : Replacing missing values with 0
by etj (Priest) on May 22, 2022 at 01:15 UTC
    $PDL::undefval would potentially be useful; it allows you to specify what to use if an undef (or ragged i.e. non-existent) value is found when turning Perl arrays/values into an ndarray.