Increment in nested loop

Increment in nested loop

am 02.12.2007 17:30:18 von 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 () {

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

Re: Increment in nested loop

am 02.12.2007 18:15:38 von jurgenex

Tony Lissner wrote:
> 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 () {
> 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

Re: Increment in nested loop

am 02.12.2007 18:18:27 von Ron Bergin

On Dec 2, 8:30 am, Tony Lissner wrote:
> 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 () {
>
> 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?

Re: Increment in nested loop

am 02.12.2007 18:28:54 von jurgenex

Ron Bergin wrote:
> On Dec 2, 8:30 am, Tony Lissner wrote:
[...]
> 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:
>> Page 1
>> Headers
>> 25,Fred,Nerk
>> 23,Foo,Bar

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

jue

Re: Increment in nested loop

am 02.12.2007 18:44:22 von Ron Bergin

On Dec 2, 9:28 am, "Jürgen Exner" wrote:
> Ron Bergin wrote:
> > On Dec 2, 8:30 am, Tony Lissner wrote:
> [...]
> > 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:
>
> >> Page 1
> >> Headers
> >> 25,Fred,Nerk
> >> 23,Foo,Bar
>
> 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.

Re: Increment in nested loop

am 03.12.2007 06:34:08 von Tony Lissner

Jürgen Exner wrote:
> Tony Lissner wrote:
>> 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
>>
>> my $page_num = 1;
>>
>> while () {
>> my ($num, $fname, $lname) = split ',';
>> print "\nPage $page_num\nHeaders\n";
>> foreach (1 .. 2) {
>> print "$num, $fname, $lname";
>> }
>
> 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 () {

# 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

Re: Increment in nested loop

am 03.12.2007 18:44:27 von glex_no-spam

Tony Lissner wrote:
> Jürgen Exner wrote:

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

That's not needed, simply change your if()..

> if ($zero_base_lc % 2 == 0) {

if ( $. % 2 == 1 )

> print "\nNew Page\nHeaders\n";
> }
[...]