Increment in nested loop

T

Tony Lissner

This is a shortened version.
Anyone have any ideas how to get the
inner loop to increment.

Source file unknown number of lines.
Read the file and print two lines per
page until EOF

This is what I want.
Page N
Headers
Two lines on each page

Page 1
Headers
25,Fred,Nerk
23,Foo,Bar

Page 2
Headers
05,perl,v5.8.8
blank line

This is what I get.

Page 1
Headers
25, Fred, Nerk
25, Fred, Nerk


#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my $page_num = 1;

while (<DATA>) {

my ($num, $fname, $lname) = split ',';

print "\nPage $page_num\nHeaders\n";

foreach (1 .. 2) {

print "$num, $fname, $lname";

}
$page_num++;
}

__DATA__
25,Fred,Nerk
23,Foo,Bar
05,perl,v5.8.8
 
J

Jürgen Exner

Tony said:
This is a shortened version.
Anyone have any ideas how to get the
inner loop to increment.

Source file unknown number of lines.
Read the file and print two lines per
page until EOF

This is what I want.
Page N
Headers
Two lines on each page

Page 1
Headers
25,Fred,Nerk
23,Foo,Bar

Page 2
Headers
05,perl,v5.8.8
blank line

This is what I get.

Page 1
Headers
25, Fred, Nerk
25, Fred, Nerk


#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my $page_num = 1;

while (<DATA>) {
my ($num, $fname, $lname) = split ',';
print "\nPage $page_num\nHeaders\n";
foreach (1 .. 2) {
print "$num, $fname, $lname";
}

You don't do anything with $_ in the inner loop and even have only constants
in the loop condition, therefore this loop is the same as a double
print "$num, $fname, $lname";
print "$num, $fname, $lname";

If you want to print additional lines in the inner loop then you need to
read and split those additional lines in the inner loop, too. That approach
is possible, but IMO not very elegant.

Intead you may want to reconsider your algorithm and read, split(), and
print() each line as you are already doing in the outer while() loop but
injecting that page header at every other line by a simple if() statement:

if ($. % 2 == 0) # Note: $. is the current INPUT_LINE_NUMBER
{print "Header goes here\n";}

jue
 
R

Ron Bergin

This is a shortened version.
Anyone have any ideas how to get the
inner loop to increment.

Source file unknown number of lines.
Read the file and print two lines per
page until EOF

This is what I want.
Page N
Headers
Two lines on each page

Page 1
Headers
25,Fred,Nerk
23,Foo,Bar

Page 2
Headers
05,perl,v5.8.8
blank line

This is what I get.

Page 1
Headers
25, Fred, Nerk
25, Fred, Nerk

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my $page_num = 1;

while (<DATA>) {

my ($num, $fname, $lname) = split ',';

print "\nPage $page_num\nHeaders\n";

foreach (1 .. 2) {

print "$num, $fname, $lname";

}
$page_num++;
}

__DATA__
25,Fred,Nerk
23,Foo,Bar
05,perl,v5.8.8

When I run your code, it outputs exactly what you say you want it to
output.
Here's the output I get.

Page 1
Headers
25, Fred, Nerk
25, Fred, Nerk

Page 2
Headers
23, Foo, Bar
23, Foo, Bar

Page 3
Headers
05, perl, v5.8.8
05, perl, v5.8.8

So, what is it not doing that you want?
 
J

Jürgen Exner

Ron said:
When I run your code, it outputs exactly what you say you want it to
output. Here's the output I get.

Page 1
Headers
25, Fred, Nerk
25, Fred, Nerk
So, what is it not doing that you want?

And this was the desired output:
The OP forgot to read the next line before printing it.

jue
 
R

Ron Bergin

And this was the desired output:


The OP forgot to read the next line before printing it.

jue

Hmmm, I'm just waking up and it looks like I mis-read what was desired.
 
T

Tony Lissner

Jürgen Exner said:
Intead you may want to reconsider your algorithm and read, split(), and
print() each line as you are already doing in the outer while() loop but
injecting that page header at every other line by a simple if() statement:

if ($. % 2 == 0) # Note: $. is the current INPUT_LINE_NUMBER
{print "Header goes here\n";}

jue

Thanks jue and Ron for your suggestions. This should do what I wanted.

#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {

# Start line count from zero
my $zero_base_lc = $. - 1;

my ($num, $fname, $lname) = split ',';

if ($zero_base_lc % 2 == 0) {
print "\nNew Page\nHeaders\n";
}
print "$num, $fname, $lname";
}

__DATA__
25,Fred,Nerk
23,Foo,Bar
05,perl,v5.8.8
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top