W
William
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
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?
#!/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?