Would be possible to have it without a loop
What's wrong with loops? They're low in fat, high in fiber... oh, nevermind.
use warnings; use strict; my $info = send_values({ name => 5, address => 6, id => 7, pic => 8, }); print $info; sub send_values { my ($args) = @_; my @params = qw( name address pic id zip phone ); my $values = "<results>\n"; for (@params) { $values .= " <$_>$args->{$_}</$_>\n" if exists $args->{$_ +}; } $values .= "</results>\n"; return $values; } __END__ <results> <name>5</name> <address>6</address> <pic>8</pic> <id>7</id> </results>

You could do the above without a loop, but it wouldn't be as scalable or as easy to maintain. For example, you would have to type 'address' 4 times instead of once:

$values .= " <address>$args->{address}</address>\n" if ex +ists $args->{address};

In reply to Re^3: Subroutine question on use of uninitialized value. by toolic
in thread Subroutine question on use of uninitialized value. by Anonymous Monk

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.