Skip to content

View the data in a piddle a la Data::Dumper

Zaki Mughal [sivoais] edited this page Feb 2, 2015 · 3 revisions

FIXME: PDL has a ->string method that does this, but also handles columns widths so that the data is aligned along the columns.

Sometimes you want to just view the data in piddle, but you don't have PDL::IO::Browser installed. What to do? From Craig DeForest comes this gem. It only looks like a piece of barbed wire, but it is even sharper.

    sub bigstring {
        my($pdl, $fmt, $lev) = @_;
        
        $fmt = "%g" unless($fmt);
        my $s=" "x$lev;
        
        if($pdl->ndims > 1) {
               $s .= join("\n", "[", (map { bigstring($_,$fmt,$lev+1) } dog $pdl), $s."]");
        }
        elsif ($pdl->ndims) {
               $s .= "[ " . join(" ",map { sprintf($fmt,$_) } $pdl->list) . " ]";
        }
        else {
               $s = sprintf($fmt,$pdl->at(0));
        }
        
        return $s;
    }
    
    print bigstring($bighonkingpdl,"%5g");
Clone this wiki locally