Saturday, 7 September 2013

perl does not find every occurance in file

perl does not find every occurance in file

can anybody tell me why this happens? I search this file for a string, but
it doesn't give me everything. I checked it with grep. This is code I use:
#!/usr/bin/perl -w
use strict;
use warnings;
sub init {
my @MyArg = @_;
$numofargs = @MyArg;
if ($numofargs < 1 || $numofargs > 5 ) {
usage();
}
if ($numofargs == 1) {
if ($MyArg[0] =~ /^\-[\w]$/) {
usage();
}
else {
$argum[0] = $MyArg[-1];
}
}
if ($numofargs > 1) {
if ($MyArg[0] =~ /^\-[hamy]$/) {
$opt= shift (@MyArg);
@argum = @MyArg;
}
else {
@argum = @MyArg;
return 1;
}
}
}
sub printline {
$Occurances += 1;
@MyLine = split (/","/, $line);
$MyDate = shift(@MyLine);
$FormatDate = substr($MyDate,1,10);
if ($FormatDate =~ /^199|^200|^201|^202/) {
$FormatDate = substr($MyDate,7,2) . "-" . substr($MyDate,5,2)
. "-" . substr($MyDate,1,4); # formaatje dd-mm-jjjj
}
unshift (@MyLine, $FormatDate);
print join(" ", @MyLine),"\n";
$newval=$MyLine[6];
if ($MyLine[5] eq "bij") {
$newval =~ s/(\d+),(\d+)/$1\.$2/;
$sum += $newval;
}
else {
$newval =~ s/(\d+),(\d+)/$1\.$2/;
$subtr += $newval;
}
}
########################################################################################
init (@ARGV);
if (defined $opt) {
print "Optie is $opt\n";
exit 1;
}
chomp @argum;
my $FileName = "/home/xxxx/scripts/csv/fakefilename.csv";
open (INFILE, $FileName) || die "File cannot be opened.\n";
$line=<INFILE>;
@MyLine = split (/","/, $line);
print join("\t", @MyLine),"\n";
while (defined $line) {
chomp ($line);
#$line =~ tr/A-Z/a-z/;
if (defined $argum[0]) {
#print "debugline at ifdef $argum[0]\n";
if ($line =~ m/$argum[0]/i) {
printline ();
$line=<INFILE>;
}
elsif (defined $argum[1]) {
if ($line =~ /$argum[1]/) {
printline;
$line=<INFILE>;
}
elsif (defined $argum[2]) {
if ($line =~ /$argum[2]/) {
printline;
$line=<INFILE>;
}
elsif (defined $argum[3]) {
if ($line =~ /$argum[3]/) {
printline;
$line=<INFILE>;
}
}
}
}
}
$line=<INFILE>;
}
if (defined $Occurances) {
print "\n$Occurances found.\n";
}
if (defined $sum) {
printf("Totaal added = E %6.2f\n", $sum);
}
else{
print("Totaal added = Nada!\n");
}
if (defined $subtr) {
printf("Totaal off = E %6.2f\n", $subtr);
}
else {
print("Totaal off = Nada!\n");
}
close (INFILE);
When I run the code, it gives output, but not enough. Some lines are
missing Can someone help me? Thanx in advance!

No comments:

Post a Comment