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

Hi Monks
Does anyone know of an easy was to read in a text file and feed it directly into a PDL matrix? The documenation that I have read so far doesnt seem to be very forthcoming. The data file is arranged in columns as follows:
0.6490, 0.0330 0.1150, 0.0290 0.0070, 0.3570 0.0630, 0.0760 0, 0.0400 0.0120, 0.0620 0.1530, 0.4020
Any advice much appreciated.

Replies are listed 'Best First'.
Re: reading files in PDL matrix
by reneeb (Chaplain) on Aug 15, 2005 at 11:11 UTC
    The PDL::IO::Misc-module and its rcol-method should meet your needs!
Re: reading files in PDL matrix
by anonymized user 468275 (Curate) on Aug 15, 2005 at 11:22 UTC
    I think the more appropriate module is PDL::Matrix
    use PDL::Matrix; open my $fh, "<file" or die $!; my (@left, @right) = ((),()); while( <$fh> ) { chop; my ( $left, $right ) = split ','; push @left, $left; push @right, $right; } my $m = PDL::Matrix->pdl([\@left,\@right]);

    One world, one people

Re: reading files in PDL matrix
by holli (Abbot) on Aug 15, 2005 at 11:17 UTC
    I don't have PDL installed, but this should do it:
    my $m = [[],[]]; while ( <DATA> ) { chomp; my @l = split /,\s+/; push @{$m->[0]}, $l[0]; push @{$m->[1]}, $l[1]; } $m = PDL::Matrix->pdl($m); __DATA__ 0.6490, 0.0330 0.1150, 0.0290 0.0070, 0.3570 0.0630, 0.0760 0, 0.0400 0.0120, 0.0620 0.1530, 0.4020


    holli, /regexed monk/