TIMTOWTDI, it also depends on exactly which of the values you're trying to print is undef - since I can't tell from your post I'm going to guess it's $A->{$key}->{$key2}, but if not, you will need to adjust the tests in the code examples below accordingly.

Solution 1: Disable those warnings altogether. Note that warnings can be useful in debugging, so this may not be the best unless you know what you're doing.

# ... foreach my $key2 (@aminos) { no warnings 'uninitialized'; printf OUT ("$forprint ", $A->{$key}->{$key2}); } # ...

Solution 2: Don't print at all if the value is not defined.

foreach my $key2 (@aminos) { printf OUT ("$forprint ", $A->{$key}->{$key2}) if defined $A->{$key}->{$key2}; }

Solution 3: Use the defined-or operator // to replace the undef value with the empty string. (Only available in Perl 5.10 and above)

foreach my $key2 (@aminos) { printf OUT ("$forprint ", $A->{$key}->{$key2} // ''); }

Solution 4: Skip the loop iteration entirely if the value is undefined. (similar idea to Solution 2)

foreach my $key2 (@aminos) { next unless defined $A->{$key}->{$key2}; printf OUT ("$forprint ", $A->{$key}->{$key2}); }

In reply to Re: handling printf 0 error by Anonymous Monk
in thread handling printf 0 error by madM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.