How can I use rcols PDL in a pass-by-reference routine?

In particular, I want to use rcols with the PERLCOLS option.

Here is what I want to do:

my @array; getColumn(\@array, $file, 4); # get the fourth column from file 

I can do this if I use \@array , but for backward compatibility I would prefer not to. Here's how I would do it using array-ref-ref:

 sub getColumn { my ($arefref, $file, $colNum) = @_; my @read = rcols $file, { PERLCOLS => [$colNum] }; $$arefref = $read[-1]; return; } 

But I don’t see how to make a routine that takes a ref array as an argument without saying something like @$aref = @{$read[-1]} , which, afaict, copies each element individually.

PS: after reading the PDL::IO::Misc documentation , it looks like the perl array should be $read[0] , but it is not.

PERLCOLS - an array of column numbers to be read into perl arrays and not a teenager. Any columns not specified in the explicit list of read columns will be returned after the explicit columns. (default is B).

I am using PDL v2.4.4_05 with Perl v5.10.0 created for x86_64-linux-thread-multi

+4
source share
2 answers

I do not understand why this did not work:

 my $arr_ref; getColumn( $arr_ref, $file, 4 ); sub getColumn { my ( $arr_ref, $file, $colNum ) = @_; my @read = rcols, $file, { PERLCOLS => [ $colNum ] }; # At this point, @read is a list of PDLs and array references. $arr_ref = $read[-1]; } 

Looking at the rcols() documentation, it looks like if you add the PERLCOLS option, it will return any column that you request as an array reference, so you just have to assign it a reference to the array you passed into.

And as for the documentation question, I understand that you did not specify any explicit columns, so rcols() will first return all the columns in the file as PDL, and then return the Perl columns you requested arrayrefs, so yourref goes to $read[-1] .

+1
source

I find that part of the difficulty with using rcols is that the user launches PDL-2.4.4, while the version of rcols docs was from PDL-2.4.7, which may have a skew in functionality. With the current version of PDL-2.4.10, it is easy to use rcols to read in a single column of data in the form of a perl array, which is returned via arrayref:

 pdl> # cat data 1 2 3 4 1 2 3 4 1 2 3 4 pdl> $col = rcols 'data', 2, { perlcols=>[2] } ARRAY(0x2916e60) pdl> @{$col} 3 3 3 

Note that in the current version, the perlcols parameter allows perlcols to specify the type of column output, rather than just adding a perl-style column at the end.

Use pdldoc rcols or do help rcols in the PDL shell to see additional documentation. A good resource is the perldl mailing list .

+1
source

All Articles