perl, how to populate a string with fix spaces

Posted on 2011-10-13
Last Modified: 2012-05-12
$fname = "James";
$lname = "Hill";
$status = "single";
# How to populate a string so that all the first name and last name line up.
# Assume I am using fix font.  Output would look like that.
"  James           Hill        single     "
"  Charles        Dancy    married   "
Question by:rgbcof
    LVL 10

    Accepted Solution

    Do you know the number of spaces you want or will it be based on the length of the first/last name on your list?
    If you do know the spacing ahead of time. It's easy using printf as in:
     perl -e 'printf("%-12s %-20s %-10s\n", "James","Hill","single")'

    Open in new window


    Author Closing Comment

    You solved it.  Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now