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:}

In reply to sprintf problem by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.