in reply to sprintf problem

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".