Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > $lineNumber +=1 does not increment

Reply
Thread Tools

$lineNumber +=1 does not increment

 
 
William
Guest
Posts: n/a
 
      01-30-2006
I am trying to parse a file with the following code:

#!/usr/bin/perl -w

use strict;

# reads in all lines from diff.txt
open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );
my @fileContent = <FD>;
close ( FD );

my $lineNumber = 1; # file starts with line 1
my $C_previousPortfolio;
my $D_newPortfolio;

foreach my $line ( @fileContent ) {
print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
# skip line if line number is odd
if ( 1 == ($lineNumber % 2) ) {
next;
}
else {
if ( $line =~ m/\>/ ) {
( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
$line );
}
}
$lineNumber += 1;
}

File content:
0a1
> "2nd D difference"

3a5
> "IN 27 but not in 26 - D"


Current output:
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1
Use of uninitialized value at ./read_differences.pl line 15.
Use of uninitialized value at ./read_differences.pl line 15.
, , 1

Expected output:
, , 1
, 2nd D difference, 2
, , 3
, IN 27 but not in 26 - D, 4


Questions:
1) Why doesn't $lineNumber increment?


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-30-2006
William <(E-Mail Removed)> wrote in
news(E-Mail Removed) t.cs.uwaterloo.ca:

> I am trying to parse a file with the following code:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> # reads in all lines from diff.txt
> open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );
> my @fileContent = <FD>;
> close ( FD );


Please see the posting guidelines for this group to learn how to provide
the data your script needs to work.


> my $lineNumber = 1; # file starts with line 1
> my $C_previousPortfolio;
> my $D_newPortfolio;
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;
> }



I have no idea what this code is trying to do. I don't know what data you
are expecting to read.

Formatting your source better, and therefore respecting your reader pays
dividends for you as well.

> Questions:
> 1) Why doesn't $lineNumber increment?


Why should it? Stripping the parts that are not essential:

my $lineNumber = 1;

foreach my $line ( @fileContent ) {
if ( 1 == ($lineNumber % 2) ) {
next;
}
$lineNumber += 1;
}

The condition in the if statement above will be true upon entry to this
loop, and therefore it will never get to the statement where $lineNumber is
incremented.

Sinan
 
Reply With Quote
 
 
 
 
Dominique Crétel
Guest
Posts: n/a
 
      01-30-2006
$lineNumber start with value 1
In your foreach, you do a 'next' if ($lineNumber % 2) == 1
It's the case, so you jump in the next loop without increasing it's value !

William a écrit :
> my $lineNumber = 1; # file starts with line 1
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;

 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      01-30-2006
At 2006-01-30 09:54AM, William <(E-Mail Removed)> wrote:
> I am trying to parse a file with the following code:
>

[...]
> my $lineNumber = 1; # file starts with line 1
> my $C_previousPortfolio;
> my $D_newPortfolio;
>
> foreach my $line ( @fileContent ) {
> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;
> }


Logic error. Since $lineNumber starts as 1, this if expression always
evaluates true, and you call 'next' in every iteration of the loop. You
will never reach your increment statement.

> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;
> }

[...]
> Questions:
> 1) Why doesn't $lineNumber increment?


To fix, put the increment statement in a continue block so it is invoked
for each iteration of the loop even if you call 'next':

my $lineNumber = 1;
foreach my $line (...) {
if (1 == $lineNumber%2) {
next;
}
...
}
continue {
$lineNumber++;
}

--
Glenn Jackman
Ulterior Designer
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-30-2006

William wrote:
> I am trying to parse a file with the following code:
>
> #!/usr/bin/perl -w
>
> use strict;


Very, very good! However, it is these days considered better to type
use warnings;
rather than the -w


> # reads in all lines from diff.txt
> open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );


It's far better to use the three-argument form of open(), to use
lexical filehandles instead of global barewords, and to include the
*reason* the open failed. (And also, IMHO, to eliminate the needless
punctuation....)

open my $FD, '<', 'diff.txt' or die "Cannot open file diff.txt: $!"


> my @fileContent = <FD>;
> close ( FD );


Why are you reading the entire file into memory at once? Why not
process one line at a time?

> my $lineNumber = 1; # file starts with line 1


If you process the file one line at a time, Perl keeps track of this
value for you, in the $. variable.

> my $C_previousPortfolio;
> my $D_newPortfolio;
>
> foreach my $line ( @fileContent ) {


while (my $line = <$FD>) {

> print "$C_previousPortfolio, $D_newPortfolio, $lineNumber";
> # skip line if line number is odd
> if ( 1 == ($lineNumber % 2) ) {
> next;


Here, you have a problem. You've skipped and gone on to the next
iteration of your foreach loop, but you never incremented $lineNumber.
That is your bug.

> }
> else {
> if ( $line =~ m/\>/ ) {
> ( $C_previousPortfolio, $D_newPortfolio ) = split ( /\>/,
> $line );
> }
> }
> $lineNumber += 1;


If you want this statement to be executed each iteration of the loop,
regardless of any next; statements, put it in a continue{} block at the
end of the loop...

> }


continue {
$lineNumber ++;
}

Re-writing your entire loop:

while (my $line = <$FD>) {
print "$C_previousPortfolio, $D_newPortfolio, $.\n"
next if 1 == $. % 2;
if ($line =~ /(.*)>(.*)/) {
($C_previousPortfolio, $D_newPortfolio) = ($1, $2);
}
}


Hope this helps
Paul Lalli

 
Reply With Quote
 
William
Guest
Posts: n/a
 
      01-30-2006
On Mon, 30 Jan 2006, Paul Lalli wrote:

>
> William wrote:
>> I am trying to parse a file with the following code:
>>
>> #!/usr/bin/perl -w
>>
>> use strict;

>
> Very, very good! However, it is these days considered better to type
> use warnings;
> rather than the -w


I got an error when I tried use warnings. We had an old version of perl:
perl -v

This is perl, version 5.005_03 built for sun4-solaris

>
>
>> # reads in all lines from diff.txt
>> open ( FD, "diff.txt" ) || die ( "Cannot open file diff.txt" );

>
> It's far better to use the three-argument form of open(), to use
> lexical filehandles instead of global barewords, and to include the
> *reason* the open failed. (And also, IMHO, to eliminate the needless
> punctuation....)
>
> open my $FD, '<', 'diff.txt' or die "Cannot open file diff.txt: $!"


I got the following error:
Too many arguments for open at ./read_differences.pl line 11, near
""/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";"
Execution of ./read_differences.pl aborted due to compilation errors.


If I try:

open my $NEW_FD, "/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";

Can't use an undefined value as a symbol reference at
../read_differences.pl line 11.

Please advice.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-30-2006
William wrote:
> On Mon, 30 Jan 2006, Paul Lalli wrote:
> > use warnings;
> > rather than the -w

>
> I got an error when I tried use warnings. We had an old version of perl:
> perl -v
>
> This is perl, version 5.005_03 built for sun4-solaris


*shudder*

> > open my $FD, '<', 'diff.txt' or die "Cannot open file diff.txt: $!"

>
> I got the following error:
> Too many arguments for open at ./read_differences.pl line 11, near
> ""/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";"
> Execution of ./read_differences.pl aborted due to compilation errors.
>
>
> If I try:
>
> open my $NEW_FD, "/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";
>
> Can't use an undefined value as a symbol reference at
> ./read_differences.pl line 11.
>
> Please advice.


As you stated, you have an old version of Perl. My recommendations
were based on the assumption that you were using a Perl from this
decade.

I strongly advise you to upgrade.

Paul Lalli

 
Reply With Quote
 
William
Guest
Posts: n/a
 
      01-30-2006
On Mon, 30 Jan 2006, Paul Lalli wrote:

> William wrote:
>> On Mon, 30 Jan 2006, Paul Lalli wrote:
>>> use warnings;
>>> rather than the -w

>>
>> I got an error when I tried use warnings. We had an old version of perl:
>> perl -v
>>
>> This is perl, version 5.005_03 built for sun4-solaris

>
> *shudder*
>
>>> open my $FD, '<', 'diff.txt' or die "Cannot open file diff.txt: $!"

>>
>> I got the following error:
>> Too many arguments for open at ./read_differences.pl line 11, near
>> ""/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";"
>> Execution of ./read_differences.pl aborted due to compilation errors.
>>
>>
>> If I try:
>>
>> open my $NEW_FD, "/app_rre/home/mx_rre/scripts/Scenario/reports/rskpolicy/new_tmp.txt";
>>
>> Can't use an undefined value as a symbol reference at
>> ./read_differences.pl line 11.
>>
>> Please advice.

>
> As you stated, you have an old version of Perl. My recommendations
> were based on the assumption that you were using a Perl from this
> decade.
>
> I strongly advise you to upgrade.


I will need to have a discussion with my management first.
But I totally agree with you.

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      02-06-2006
William wrote:

> my $lineNumber = 1; # file starts with line 1
> foreach my $line ( @fileContent ) {
> if ( 1 == ($lineNumber % 2) ) {
> next;
> ...
> $lineNumber += 1;
> }


Your program would have worked if you started at zero and
did an unconditional pre-increment at the top of the if().

my $lineNumber;
foreach my $line (@fileContent) {
if (++$lineNumber % 2) {
print "$lineNumber is not a multiple of 2, skipping\n";
next;
} else {
print "$lineNumber is even\n"
split(...);
}
}

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Post increment ++ has higher precedence than pre increment ++. Why? Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM
post increment or pre increment? Peng Yu Perl Misc 7 11-23-2008 11:44 PM
How does one increment a final variable? Oliver Wong Java 5 01-27-2006 11:34 PM
why prefix increment is faster than postfix increment? jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM



Advertisments