Suppose two lines of text correspond to each other word by word except for the punctuation marks. How do I make vertical alignment of them?
For example:
$line1 = "I am English in fact"; $line2 = "Je suis anglais , en fait";
I want the output to be like this:
I am English in fact Je suis anglais , en fait .
I've come up with the following code, based on what I've learnt from the answers to my previous questions posted on SO and the "Formatted Output with printf" section of Learning Perl.
use strict;
use warnings;
my $line1 = "I am English in fact";
my $line2 = "Je suis anglais , en fait.";
my @array1 = split " ", $line1;
my @array2= split " ", $line2;
printf "%-9s" x @array1, @array1;
print "\n";
printf "%-9s" x @array2, @array2;
print "\n";
It is not satisfying. The output is this:
I am English in fact Je suis anglais , en fait.
Can someone kindly give me some hints and suggestions to solve this problem?
Thanks :)
Updated
@ysth sent me on the right track! Thanks again:) Since I know what my own date looks like,for this sample, all I have to do is add the following line of code:
for ( my $i = 0; $i < @Array1 && $i < @Array2; ++$i ) {
if ( $Array2[$i] =~ /,/ ) {
splice( @Array1, $i, 0, '');
}
}
Learning Perl briefly mentions that splice function can be used to remove or add items in the middle of array. Now thanks, I've enlarged my Perl knowledge stock again :)