Hi,
I'm struggling with a perl script that increments totals and outputs values when a new ref number is encountered.
my input looks something like:
REC>0002>000046>18000 >TSOP>M>18000 000046>D0
REC>0006>000077>18000 >TSOP>M>18000 000046>D0
REC>0006>000077>18000 >TSOP>C>18000 000046>D0
REC>0006>000077>18000 >TSOP>C>18000 000046>D0
REC>0005>000249>18000 >TSOP>M>18000 000046>D0
REC>0001>100041>18000 >TSOP>M>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0002>000146>18000 >TSOP>M>18000 000046>D0
REC>0001>000243>18000 >TSOP>M>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0003>100045>18000 >TSOP>M>18000 000046>D0
The second field is the number of pages, and the third field is the ref number. field six is the duplicate flag,
"M" = master, "C" = copy.
I have to output this data as follows:
Ref: 000046 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000077 totalOrig: 6 totalDups: 12 totalPrinted: 18
Ref: 000249 totalOrig: 5 totalDups: 0 totalPrinted: 5
Ref: 100041 totalOrig: 1 totalDups: 3 totalPrinted: 4
Ref: 000146 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000243 totalOrig: 1 totalDups: 4 totalPrinted: 5
Ref: 100045 totalOrig: 3 totalDups: 0 totalPrinted: 3
If the current ref num is the same as the previous, then add the number of pages to the number of duplicates if dup flag equals "C", and increment total pages by page count.
My script seems to be using the previous value of totalDups, and I'm not sure where I'm going wrong. My ouptput is as follows:
Ref: 000046 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000077 totalOrig: 6 totalDups: 0 totalPrinted: 6
Ref: 000249 totalOrig: 5 totalDups: 12 totalPrinted: 17
Ref: 100041 totalOrig: 1 totalDups: 0 totalPrinted: 1
Ref: 000146 totalOrig: 2 totalDups: 3 totalPrinted: 5
Ref: 000243 totalOrig: 1 totalDups: 0 totalPrinted: 1
Ref: 100045 totalOrig: 3 totalDups: 4 totalPrinted: 7
This has had me and a colleague stumped for two days now. Although we are both quite new to perl. The code is below.
Thanks in advance for any tips/pointers as to where I am going wrong.
@ARGV == 1 or die "Usage: $0 inputFile \n";
$prevRef = "XXX";
$totalOrig = 0;
$totalDups = 0;
$totalPrinted = 0;
open INPUTFILE, "$ARGV[0]" or die "error: $ARGV[0] - $!";
while (<INPUTFILE>) {
@recordArray = split (/>/, $_);
$numPages = $recordArray[1];
$refNum = $recordArray[2];
$dupInd = $recordArray[5];
if ($prevRef eq $refNum){
&incrementTots;
next;
}
else{
&incrementTots;
&printDetail;
}
$prevRef = $refNum;
}
#
# subroutine to increment totals
#
sub incrementTots{
if ($dupInd eq "M"){
$totalOrig += $numPages;
}
else{
$totalDups += $numPages;
}
$totalPrinted += $numPages;
}
#
# print a detail line
#
sub printDetail{
print "Ref: $refNum totalOrig: $totalOrig totalDups: $totalDups totalPrinted: $totalPrinted\n";
$totalOrig = 0;
$totalDups = 0;
$totalPrinted = 0;
}
I'm struggling with a perl script that increments totals and outputs values when a new ref number is encountered.
my input looks something like:
REC>0002>000046>18000 >TSOP>M>18000 000046>D0
REC>0006>000077>18000 >TSOP>M>18000 000046>D0
REC>0006>000077>18000 >TSOP>C>18000 000046>D0
REC>0006>000077>18000 >TSOP>C>18000 000046>D0
REC>0005>000249>18000 >TSOP>M>18000 000046>D0
REC>0001>100041>18000 >TSOP>M>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0001>100041>18000 >TSOP>C>18000 000046>D0
REC>0002>000146>18000 >TSOP>M>18000 000046>D0
REC>0001>000243>18000 >TSOP>M>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0001>000243>18000 >TSOP>C>18000 000046>D0
REC>0003>100045>18000 >TSOP>M>18000 000046>D0
The second field is the number of pages, and the third field is the ref number. field six is the duplicate flag,
"M" = master, "C" = copy.
I have to output this data as follows:
Ref: 000046 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000077 totalOrig: 6 totalDups: 12 totalPrinted: 18
Ref: 000249 totalOrig: 5 totalDups: 0 totalPrinted: 5
Ref: 100041 totalOrig: 1 totalDups: 3 totalPrinted: 4
Ref: 000146 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000243 totalOrig: 1 totalDups: 4 totalPrinted: 5
Ref: 100045 totalOrig: 3 totalDups: 0 totalPrinted: 3
If the current ref num is the same as the previous, then add the number of pages to the number of duplicates if dup flag equals "C", and increment total pages by page count.
My script seems to be using the previous value of totalDups, and I'm not sure where I'm going wrong. My ouptput is as follows:
Ref: 000046 totalOrig: 2 totalDups: 0 totalPrinted: 2
Ref: 000077 totalOrig: 6 totalDups: 0 totalPrinted: 6
Ref: 000249 totalOrig: 5 totalDups: 12 totalPrinted: 17
Ref: 100041 totalOrig: 1 totalDups: 0 totalPrinted: 1
Ref: 000146 totalOrig: 2 totalDups: 3 totalPrinted: 5
Ref: 000243 totalOrig: 1 totalDups: 0 totalPrinted: 1
Ref: 100045 totalOrig: 3 totalDups: 4 totalPrinted: 7
This has had me and a colleague stumped for two days now. Although we are both quite new to perl. The code is below.
Thanks in advance for any tips/pointers as to where I am going wrong.
@ARGV == 1 or die "Usage: $0 inputFile \n";
$prevRef = "XXX";
$totalOrig = 0;
$totalDups = 0;
$totalPrinted = 0;
open INPUTFILE, "$ARGV[0]" or die "error: $ARGV[0] - $!";
while (<INPUTFILE>) {
@recordArray = split (/>/, $_);
$numPages = $recordArray[1];
$refNum = $recordArray[2];
$dupInd = $recordArray[5];
if ($prevRef eq $refNum){
&incrementTots;
next;
}
else{
&incrementTots;
&printDetail;
}
$prevRef = $refNum;
}
#
# subroutine to increment totals
#
sub incrementTots{
if ($dupInd eq "M"){
$totalOrig += $numPages;
}
else{
$totalDups += $numPages;
}
$totalPrinted += $numPages;
}
#
# print a detail line
#
sub printDetail{
print "Ref: $refNum totalOrig: $totalOrig totalDups: $totalDups totalPrinted: $totalPrinted\n";
$totalOrig = 0;
$totalDups = 0;
$totalPrinted = 0;
}