user2043978
user2043978

Reputation: 1

string alignment in perl / match alignment

I have two strings $dna1 and $dna2. Print the two strings as concatenated, and then print the second string lined up over its copy at the end of the concatenated strings. For example, if the input strings are AAAA and TTTT, print:

AAAATTTT
    TTTT 

this is a self exercise question .. not a homework ,

i tried using index #!/usr/bin/perl -w

$a ='AAAAAAAAAATTTTTTTTT';
$b ='TTTTTTTTTT';
print $a,"\n";
print ''x index($a,$b),$b,"\n"; 

but it is not working as needed .help please

Upvotes: 0

Views: 337

Answers (2)

TLP
TLP

Reputation: 67920

This is a fun little exercise. I did this:

perl -lwe'$a="AAAA"; $b="TTTT"; $c = $a.$b; $i = index($c,$b) + length($b); 
          print $c; printf "%${i}s\n", $b;'
AAAAAAATTTT
       TTTT

Note that generally speaking, using the variable names $a through $c is a bad idea, and only acceptable here because it is a one-liner. $a and $b are also reserved variable names used with sort.

Upvotes: 2

ikegami
ikegami

Reputation: 386676

Start by checking what index($a,$b) is returning... Perhaps you should pick a $b that's actually in $a!

Then realise that concatenating 10 instances of an empty string is an empty string, not 10 spaces.

Upvotes: 2

Related Questions