in reply to The joys of bad code
Came across some code I just had to share. How many steps is this programmer from good, idiomatic perl?
sub padfield { my ( $value, $fieldlength ) = @_; while ( length $value < $fieldlength ) { my $pad = '0'; $value = join( '', $pad, $value ); } return $value; }
- A creative use of join(). But why not just use simple string contcatenation: $value = $pad . $value ?
- You could use the string repetition operator $value = ("0" x ($fieldlength - length $value )) . $value.
- Or you could use take the right substring of an over-padded string.
- But even a C or Java programmer should know printf padded formats: return sprintf "%0$fieldlengthd", $value.
--
TTTATCGGTCGTTATATAGATGTTTGCA
|
---|
In Section
Meditations