in reply to Printing non existing hash values as "missing"

sub my_sprintf { return defined $_[1] ? sprintf @_ : 'MISSING'; } my %h = ( A => 1.23435 ); my $out = my_sprintf "%.3E", $h{A}; warn $out; $out = my_sprintf "%.3E", $h{B}; warn $out;

Replies are listed 'Best First'.
Re^2: Printing non existing hash values as "missing"
by Anonymous Monk on Jun 10, 2015 at 13:43 UTC
    Oops....
    Unlike "printf", "sprintf" does not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful - sprintf.
    LOL... who actually knew that? I mean, ******* ****, what the ****!
    sub my_sprintf { return defined $_[1] ? sprintf $_[0], $_[1] : 'MISSING'; }
      That's why I started to get all 2s... :) Thanks
Re^2: Printing non existing hash values as "missing"
by Anonymous Monk on Jun 10, 2015 at 12:47 UTC
    Thank you, just perfect!