in reply to Format text problem

Hi,
Once you hit a return statement in a subroutine it never returns back again so everything after the first return is ignored. It never actually gets to the 2nd and 3rd return statements. Try returning an array that contains the Player Name, Parents Name and Member ID.
Try something like
sub format_email_text { my @arr; $arr[0]=sprintf("%-30s %-50s","Player Name:",$player); $arr[1]=sprintf("%-30s %-50s","Parent's Name:",$parent); $arr[2]=sprintf("%-30s %-50s","Member ID:",$memberid); return (@arr); } my @vals = format_email_text(); print "Player Name : " . $vals[0] . "\n"; print "Parent Name : " . $vals[1] . "\n"; print "Member ID : " . $vals[2] . "\n";


Regards Paul

Replies are listed 'Best First'.
Re^2: Format text problem
by ikegami (Patriarch) on Apr 02, 2005 at 20:20 UTC

    True, but why not do this:

    sub format_email_text { return join("\n", sprintf("%-30s %-50s", "Player Name:", $player), sprintf("%-30s %-50s", "Parent's Name:", $parent), sprintf("%-30s %-50s", "Member ID:", $memberid), ); } ... $text = format_email_text(); ...

    It would probably be cleaner if the data was passed as arguments, too:

    sub format_email_text { my ($player, $parent, $memberid) = @_; return join("\n", sprintf("%-30s %-50s", "Player Name:", $player), sprintf("%-30s %-50s", "Parent's Name:", $parent), sprintf("%-30s %-50s", "Member ID:", $memberid), ); } ... $text = format_email_text($player, $parent, $memberid); ...
      Hi,

      In reply to your second method, the result that appears in my email looks like: print format_email_text(Minnie Mouse, Mickey Mouse, 78787);

      Each variable should appear underneath each other.
        I adjusted it so the result is saved in $text instead of being printed.
      Hi,

      It worked perfectly. Oh, thank you so much.

      Can you explain to me what the "join" portion of the code is doing?

      Thanks for your help.

        join($sep, $string1, $string2, $string3)
        is the same thing as
        $string1 . $sep . $string2 . $sep . $string

        In other words, it takes a list and/or array of strings, and combines them into one long string. The first argument is a seperator. It's inserted bewteen every string in the list. The core perl functions (including join) are documented in perlfunc.

Re^2: Format text problem
by b310 (Scribe) on Apr 02, 2005 at 20:22 UTC
    Hi,

    Thank you for the reply. I tried your suggestion and it worked, sort of. This is the result that I'm getting from my email.

    print "Player Name : " . Player Name: Mickey Mouse . " ";

    print "Parent Name : " . Parent's Name: Donald Duck . " ";

    print "Member ID : " . Member ID: 78787 . " ";

    The result should appear and line up like:
    Player Name: value
    Parent's Name: value

    I'm not sure what's going on here. Any ideas? Thank you so much.