diff --git a/Basic/Core/Core.pm b/Basic/Core/Core.pm index cc2b49b4b..671f092e4 100644 --- a/Basic/Core/Core.pm +++ b/Basic/Core/Core.pm @@ -3267,46 +3267,47 @@ sub PDL::type { return PDL::Type->new($_[0]->get_datatype); } ##################### Printing #################### -# New string routine +=head2 string + +=for ref + +Convert ndarray to string, optionally using a C format. + +=for usage + + print $x; # overloaded + print $x->string; # explicit method call + print $x->string("%5d"); # providing sprintf format + +=cut $PDL::_STRINGIZING = 0; sub PDL::string { - my($self,$format)=@_; - my $to_return = eval { - if($PDL::_STRINGIZING) { - return "ALREADY_STRINGIZING_NO_LOOPS"; - } - local $PDL::_STRINGIZING = 1; - my $ndims = $self->getndims; - if($self->nelem > $PDL::toolongtoprint) { - return "TOO LONG TO PRINT"; - } - if ($ndims==0) { - if ( $self->badflag() and $self->isbad() ) { - return "BAD"; - } else { - my @x = $self->at(); - return ($format ? sprintf($format, $x[0]) : "$x[0]"); - } - } - return "Null" if $self->isnull; - return "Empty[".join("x",$self->dims)."]" if $self->isempty; # Empty ndarray - local $sep = $PDL::use_commas ? "," : " "; - local $sep2 = $PDL::use_commas ? "," : ""; - if ($ndims==1) { - return str1D($self,$format); - } - else{ - return strND($self,$format,0); - } - }; - if ($@) { - # Remove reference to this line: - $@ =~ s/\s*at .* line \d+\s*\.\n*/./; - PDL::Core::barf("Stringizing problem: $@"); - } - return $to_return; + my ($self,$format) = @_; + my $to_return = eval { + return "ALREADY_STRINGIZING_NO_LOOPS" if $PDL::_STRINGIZING; + local $PDL::_STRINGIZING = 1; + return "Null" if $self->isnull; + return "Empty[".join("x",$self->dims)."]" if $self->isempty; + return "TOO LONG TO PRINT" if $self->nelem > $PDL::toolongtoprint; + my $ndims = $self->getndims; + if ($ndims==0) { + return "BAD" if $self->badflag and $self->isbad; + my $x = $self->sclr; + return $format ? sprintf($format, $x) : "$x"; + } + local $sep = $PDL::use_commas ? "," : " "; + local $sep2 = $PDL::use_commas ? "," : ""; + return str1D($self,$format) if $ndims==1; + return strND($self,$format,0); + }; + if ($@) { + # Remove reference to this line: + $@ =~ s/\s*at .* line \d+\s*\.\n*/./; + PDL::Core::barf("Stringizing problem: $@"); + } + return $to_return; } ############## Section/subsection functions ################### diff --git a/Changes b/Changes index f244e8c0c..7450a2d35 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ +- document PDL::string (#459) - thanks @vadim-160102 for report + 2.085 2024-01-30 - switch FFT code to use heap, not VLA (#436) - thanks @HaraldJoerg for report - add methods PDL::Trans::{address,name,flags,flags_vtable,vaffine,offs,incs,ind_sizes,inc_sizes}