Limit line for perl

Hey guys, can help me out with this?

How do i limit output for xml to 50 character?
i tried *below* but doesnt work, it still print more than 50 characters.

Thanks in advance

printf "%-50s", "$testline\n";

You could use a function like that below (UNTESTED CODE)

sub limit{
my ($string, $limit)=shift;
return length($string)>$limit ? substr($string, 0, $limit) : $string;
}
...
printf limit($testline, 50);
1 Like
print substr($testline, 0, 50), "\n"
1 Like

you can use substr function

printf "%s\n", substr $testline,0,50;
1 Like

substr work great.. thanks alot guys