CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
string output

 



qwe
New User

Nov 28, 2009, 2:37 AM

Post #1 of 6 (574 views)
string output Can't Post

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

Post #2 of 6 (566 views)
Re: [qwe] string output [In reply to] Can't Post


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

Post #3 of 6 (545 views)
Re: [qwe] string output [In reply to] Can't Post

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

Post #4 of 6 (528 views)
Re: [toolic] string output [In reply to] Can't Post

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

Post #5 of 6 (520 views)
Re: [qwe] string output [In reply to] Can't Post


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



qwe
New User

Nov 30, 2009, 5:31 PM

Post #6 of 6 (503 views)
Re: [7stud] string output [In reply to] Can't Post

Thank you.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives