in reply to Format text problem

I would also like to note here that unless there is zero possibility of $text changing in format, you might want to make your sub a bit more adaptable - something like the following:
use strict; use warnings; my $player = 'Bingo'; my $parent = 'Bob'; my $memberid = 'sad324gsg'; print format_email_text(30,50, "Player Name", $player, "Parent's Name", $parent, "Member ID", $memberid); sub format_email_text { my ($name, $val, $out, $nd, $vd); $nd = shift; $vd = shift; while ($name = shift) { $val = shift; $out .= sprintf('%-'.$nd.'s %-'.$vd.'s', "$name:", $val) . "\n +"; } return $out; }
This would allow you to change the dimensions of the columns and the names and quantity of fields without having to rewrite the sub itself.

Replies are listed 'Best First'.
Re^2: Format text problem
by b310 (Scribe) on Apr 03, 2005 at 10:37 UTC
    Hi,

    I applied your suggestion to my script.

    The variables $player to $event line up perfectly. All the variables starting with $street are not lining up at all.

    This is the subroutine that generates the result:
    sub send_confirmation_email { my $text = format_email_text(30,50, "Player Name", $player, "Parent's Name", $parent, "Member ID", $memberid, "Email Address", $email, "Camp Date(s)", $dates, "Camp Event", $event, "Street", $street, "City", $city, "State", $state, "Zip Code", $zip, "Payment Method", $payment, "Message", $message); my %mail = ( From => "info\@anywhwere.com", To => $email, Subject => "Registration", Message => "" ); my $page; $mail{Message} = <<EOF; This is the information you submitted. $text EOF sendmail (%mail) or $page .= p (escapeHTML ("Oops, failure sending mail to $mai +l{To}")); return (defined ($page) ? $page : ""); }

    Plus, this is the subroutine you gave me:
    sub format_email_text { my ($name, $val, $out, $nd, $vd); $nd = shift; $vd = shift; while ($name = shift) { $val = shift; $out .= sprintf('%-'.$nd.'s %-'.$vd.'s', "$name:", $val) . "\n +"; } return $out; }

    Any ideas why some of the variables are appearing differently?

    Also, can you explain to me what your code is doing? I'm not sure what this line is doing:
    sprintf('%-'.$nd.'s %-'.$vd.'s',
    Thanks.
      Any ideas why some of the variables are appearing differently?
      No. Perhaps it's something that's happinging after the fact. Try printing out $text before you construct the mail and seeing if it layed out correctly.
      I'm not sure what this line is doing:
      sprintf('%-'.$nd.'s %-'.$vd.'s',
      Rathar than ask us to explain everything you could possibly being having trouble understanding, it would more efficient if you could be more precise about what about this you are finding difficult to understand.
      • Something documentation for the sprintf()
      • The effect of '-' in printf templates?
      • Concatenation of strings using the . operator?
      • Something else...?