Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

sprintf problem

by walkingthecow (Friar)
on Aug 19, 2009 at 18:47 UTC ( [id://789918]=perlquestion: print w/replies, xml ) Need Help??

walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks,

I am trying to write a simple script to clean the indents in my code. However, it does not seem to work (I believe there is something wrong with my sprintf statement). Basically, the code will indent each block 4 spaces. The code is below:

#!/usr/bin/perl use strict; use warnings; my $spaces=0; while (chomp(my $line=<>)) { $line =~ s/^\s+|\s+$//g; if ($line =~ /}/) { $spaces-=4; } my $padded = sprintf("%${spaces}s",$line); print "$spaces:$padded\n"; if ($line =~ /{/) { $spaces+=4; } }


Here's the input:
#!/usr/bin/perl my $blah=1; my $blah2=3; if ($blah1 =~ blah2) { then do this; and if not { then do this as well; and then this { blah; } return it; } talk }


And here's the output:
0:#!/usr/bin/perl 0: 0:my $blah=1; 0:my $blah2=3; 0: 0:if ($blah1 =~ blah2) { 4:then do this; 4:and if not { 8:then do this as well; 8:and then this { 12: blah; 8: } 8:return it; 4: } 4:talk 0:}

Replies are listed 'Best First'.
Re: sprintf problem
by kennethk (Abbot) on Aug 19, 2009 at 18:59 UTC
    Assuming you want to reinvent the wheel regardless of what jrsimmon says, your issue is that %8s does not mean 8 spaces followed by the string; rather it means set the minimum width of the field at 8, padding the front with spaces as necessary. For your purposes, you'd want something more like:

    #!/usr/bin/perl use strict; use warnings; my $spaces=0; while (chomp(my $line=<DATA>)) { $line =~ s/^\s+|\s+$//g; if ($line =~ /}/) { $spaces-=4; } my $padded = ' ' x $spaces . $line; print "$spaces:$padded\n"; if ($line =~ /{/) { $spaces+=4; } }

    avoiding the sprintf all together. More info at sprintf - search for "(minimum) width".

Re: sprintf problem
by jrsimmon (Hermit) on Aug 19, 2009 at 18:51 UTC
    Why not use a proven tool like perltidy rather than reinventing the wheel?
Re: sprintf problem
by ikegami (Patriarch) on Aug 19, 2009 at 18:59 UTC

    The number between "%" and "s" determines the minimum total size of the field, not the size of the padding. Padding will be added to expand the string being formatted to take up the entire field.

    Prefixing your string with the result of ' ' x ($level * 4) is what you are actually asking for.

Re: sprintf problem
by biohisham (Priest) on Aug 20, 2009 at 00:29 UTC
    I would draw your attention to one fact about sprintf function, it results in formatted strings, Perl checks contexts of a variable to determine whether it is a number or a string and hence it even sometimes implicitly apply sprintf(). What I am saying is sprintf() return value is a string, even if we used it on numbers as in:
    $variable=sprintf "%0.2f", 3.1415;
    the value of $variable would be a formatted-string until you tell Perl to treat it as a number through numeric manipulation or using the int() function to truncate the floating part all of it.

    Now that said, read what kennethk has linked to his reply and :)


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://789918]
Approved by jrsimmon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2025-03-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (59 votes). Check out past polls.