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

The closest I got to was something like
use PDL; my $u = pdl [1,2,3,4]; my $dim = 4; for(my $i=0; $i<$dim; $i++) { print $u->flat->index($i), "\n"; }
Also as I convert the list [1,2,3,4] to the piddle $u, can I get back a list (or a list of lists for a matrix) from $u?

Replies are listed 'Best First'.
Re: How to iterate through a PDL piddle?
by zentara (Cardinal) on Mar 10, 2011 at 14:31 UTC
    You need a dog, not puppies. :-) Does this work?
    #!/usr/bin/perl use strict; use warnings; use PDL::LiteF; #dog dosn't work on huge values my $u = pdl [1,2,3,4]; my $dim = 4; print join "\t", $u->dog; print "\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks @zentara. It works! I can't believe that it works though. There should be a more obvious way like  $u->list which gives an array reference. Or a custom iterator on a piddle which can replace  foreach.
        If you feel the urge to use an iterator in PDL, quite often that is a sign you could instead use "array programming" type constructs (see https://en.wikipedia.org/wiki/Array_programming), which lets all the processing be done inside the fast C loops, rather than going backwards and forwards between Perl and PDL.
        Errr..... there IS a 'list' function $u->list acts the same as $u->dog except list breaks the piddle up into individual elements while dog breaks it up into N-1-dimensional slices. In this particular case, where $u is one-dimensional, $u->list is identical to $u->dog
A reply falls below the community's threshold of quality. You may see it by logging in.