foreach $field (qw(Data_One Second_Part)) { # Isolate non-empty values my @values = grep { ! /^\s*$/ } param($field); next unless @values; # Skip if nothing available # Establish a header line. my $header = "First:"; $header = "Second:" if $field =~ /Second_Part/; # Print it all. $, is the output record separator for prints local $, = "\n"; print $header, @values, "\n"; }