Perl format string to the same size

This little Perl-function formats some strings to the same length.

#!/usr/bin/perl
use strict;
#example data
my @arr = ("monday","tuesday","wednesday","thursday","friday","saturday","sunday");
#default
for(@arr){
    print $_.$/;
}
#formated to same size
for(@arr){
    print len_format($_,10).$/;
}
#format sub
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}

We could see the normal output then the formatted strings:

monday    
tuesday   
wednesday 
thursday  
friday    
saturday  
sunday    
    monday
   tuesday
 wednesday
  thursday
    friday
  saturday
    sunday

4 thoughts on “Perl format string to the same size”

  1. I was recommended this blog by my cousin. I’m not sure whether this post
    is written by him as no one else know such detailed about my problem.
    You are amazing! Thanks!

  2. Thanks for sharing your info. I truly appreciate your efforts and
    I will be waiting for your further write ups thanks once again.

  3. Wow, awesome blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is wonderful, as well
    as the content!

  4. It’s difficult to get educated people with this
    topic, however you could be seen as you know what you’re talking about!
    Thanks

Leave a Reply

Your email address will not be published.

5 − one =