Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    appending (s)printf output to string

    I'm parsing some data and organizing it, and now I need to capture it inside a variable.

    I've used printf in a manner like this to organize the data:
    printf("%-30s %18s %18s\n", "$a", "$b", "$c\n");

    Now I have a variable that's storing a string, and I want to append the organized data to the variable.

    I tried something like $result.printf("%-30s %18s %18s\n", "$a", "$b", "$c\n");
    and it doesn't work. I tried sprintf too.

    Any ideas?

    Thanks, S

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    solved

    I had to place the sprint statement in a variable first, like this:

    $result = printf("%-30s %18s %18s\n", "$a", "$b", "$c\n");

    And then concatenate the two variables like this:

    $result .= $final_result;

    That solved it.

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,838
    Thanks
    2
    Thanked 160 Times in 155 Posts
    sprintf is what you're needing.

    Code:
    $result = 'some string value';
    
    $result .= sprintf("%-30s %18s %18s\n", $a, $b, $c);


  •  

    Posting Permissions

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