www.webdeveloper.com
Results 1 to 8 of 8

Thread: Perl printf()

  1. #1
    Join Date
    May 2006
    Location
    Manchester England
    Posts
    36

    Perl printf()

    I understand how printf works with numbers but i am a bit confused when it is used with strings.

    in:
    %-w.dx

    I uderstand that % is the field specifier marker, and a minus sign means to align to the left, and that w is the TOTAL width of field.
    My problem arises when ther is a decimal point (as in above) and a value for d (which i have seen written as "The total allowable width of the field FOR STRINGS). I dont understand the difference between w and d (above).

    Example.
    printf ("%-15.5s", $stringValue);

    Here, the feild is 15 characters long, but what does the .5 mean.

    Thanx in advance
    my brain hurts..ALL THE TIME!!!!!

  2. #2
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    Example.
    printf ("%-15.5s", $stringValue);

    Here, the feild is 15 characters long, but what does the .5 mean.
    I'll try my best at explaining this.

    The -15 sets the minimum field width to 15, with padding applied on the right side of the string value (any "positive" integer would apply padding to the left side of the string value). The .5 (a precision control) denotes that the string value will be truncated to a length of 5.

    This may be better understood by running the following example in your browser and viewing the source to actually see the padding and control features doing their thing.
    Code:
    my $stringValue = "Isn't this fun?";
    
    printf "%-15.1s\n", $stringValue;
    printf "%-15.2s\n", $stringValue;
    printf "%-15.3s\n", $stringValue;
    printf "%-15.4s\n", $stringValue;
    printf "%-15.5s\n", $stringValue;
    printf "%-15.6s\n", $stringValue;
    printf "%-15.7s\n", $stringValue;
    printf "%-15.8s\n", $stringValue;
    printf "%-15.9s\n", $stringValue;
    printf "%-15.10s\n", $stringValue;
    printf "%-15.11s\n", $stringValue;
    printf "%-15.12s\n", $stringValue;
    printf "%-15.13s\n", $stringValue;
    printf "%-15.14s\n", $stringValue;
    printf "%-15.15s\n", $stringValue;
    *NOTE* I've added newlines for ease of readability.

  3. #3
    Join Date
    May 2006
    Location
    Manchester England
    Posts
    36
    Thanx Cyco, thats got it
    my brain hurts..ALL THE TIME!!!!!

  4. #4
    Join Date
    May 2006
    Location
    Manchester England
    Posts
    36

    Im stuck

    One more thing CyCo, how do i actualy run perl code in a browser. Ive been using msdos (im still learning you see)
    my brain hurts..ALL THE TIME!!!!!

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    There's a way to make it work in MSIE but you don not want to go there.

    It's best to run Perl from the command line but if you are developing something for the web then you will need a server, perhaps XAMPP.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    May 2006
    Location
    Manchester England
    Posts
    36

    thanx

    Cheers Charles, I can easily run it in a shell but I think i get it anyway.
    Thanx again.
    my brain hurts..ALL THE TIME!!!!!

  7. #7
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    You need access to a server, whether locally on your own machine or remotely through a host.
    Last edited by CyCo; 11-25-2006 at 04:29 PM. Reason: Already answered

  8. #8
    Join Date
    May 2006
    Location
    Manchester England
    Posts
    36
    Cheers CyCo
    my brain hurts..ALL THE TIME!!!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles