Home: Perl Programming Help: Beginner:
string output



qwe
New User

Nov 28, 2009, 2:37 AM


Views: 583
string output

Hallo,
I have two strings:

Code
my $a = AAAAAAAAAAAAAA; 
my $b = BBBBBBBBBBB;

and I would like get the following output:

Code
AAA 
AAA
AAA
AAA
AAB
BBB
BBB
BBB
B

How could I solve it?

Best regards,


7stud
Enthusiast

Nov 28, 2009, 7:40 AM


Views: 575
Re: [qwe] string output


Code
use strict; 
use warnings;
use 5.010;

my $a = 'AAAAAAAAAAAAAA';
my $b = 'BBBBBBBBBBB';
my $ab = $a . $b;

while ($ab =~ /(.{1,3})/g) {
say $1;
}

--output:--
AAA
AAA
AAA
AAA
AAB
BBB
BBB
BBB
B


Or,

Code
use strict; 
use warnings;
use 5.010;

my $a = 'AAAAAAAAAAAAAA';
my $b = 'BBBBBBBBBBB';

my $ab = $a . $b;
my $len = length $ab;

for (my $i=0; $i < $len; $i += 3) {
say substr $ab, $i, 3;
}



(This post was edited by 7stud on Nov 28, 2009, 7:56 AM)


toolic
User

Nov 28, 2009, 10:24 AM


Views: 554
Re: [qwe] string output

Here is another way, using the Core module Text::Wrap.


Code
use strict;    
use warnings;
use Text::Wrap;

$Text::Wrap::columns = 4;
my $a = 'AAAAAAAAAAAAAA';
my $b = 'BBBBBBBBBBB';
print wrap('', '', $a . $b);



qwe
New User

Nov 28, 2009, 5:35 PM


Views: 537
Re: [toolic] string output

Thank you for the solutions.

How could I do it without my $ab = $a.$b, because I would like read a file line by line and this file is very big?


7stud
Enthusiast

Nov 28, 2009, 9:50 PM


Views: 529
Re: [qwe] string output


Code
while (<$INFILE>) { 
print;
}



qwe
New User

Nov 30, 2009, 5:31 PM


Views: 512
Re: [7stud] string output

Thank you.