Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I render undefined values from printf in Perl?

Tags:

printf

perl

undef

I'm looking for an elegant way to denote undefined values in situations where formatted numbers usually render. I'll work up a small example. For starters, you of course can't use this:

#!/usr/bin/perl
use strict;
use warnings;
for my $s (1, 1.2, undef, 1.3) {
    printf "%5.2f\n", $s;
}

...because the 'use warnings' nails you with 'Use of uninitialized value...' on the third iteration. So the next step is something like this:

#!/usr/bin/perl
use strict;
use warnings;
for my $s (1, 1.2, undef, 1.3) {
    printf "%5.2f\n", $s//0;
}

And, boy, do I like the new 5.10 '//' operator, by the way. But that's really not what I want, either, because the value of $s isn't zero, it's undefined. What I really want is something like this:

#!/usr/bin/perl
use strict;
use warnings;
for my $s (1, 1.2, undef, 1.3) {
    printf "%5.2f\n", $s//q();
}

...but I can't because this generates the "Argument "" isn't numeric..." problem on the third value.

This brings me to the doorstep of my question. I can of course write code that checks every number I emit for defined-ness, and that creates a whole different non-%f-based printf format string, but, well, ...yuck.

Has anyone defined a nice way to deal with this type of requirement?

like image 624
Cary Millsap Avatar asked Jan 24 '23 20:01

Cary Millsap


1 Answers

I don't think there's anything yuck about it -- it's exactly what you want to do.

use strict;
use warnings;

my($raw) = [1, 1.2, undef, 1.3];
my($formatted) = [map((defined $_ ? sprintf('%5.2f', $_) : "(undef)"), @$raw)];

print '$formatted: <', join('> <', @{$formatted}), qq(>\n);
like image 83
Joe Casadonte Avatar answered Jan 26 '23 11:01

Joe Casadonte