Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am trying to put today's date into a blank field 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
The code I have is
open(IN, "$vPath3$vName5") or die 'Could not open file for reading';
open(OUT, ">$vPath3$vName6") or die 'Could not open file for writing';

foreach $line (<IN>)
{
@field = split(/\,/, $line);
$numb = substr($field[10],0,1); # first digit of number
$find = $field[1];
$fond = $field[19];
chomp($date = 'date "+%y%m%d"');
$line =~ s/^$fond/$date/;
if ($numb == 1) # PO number starts with a 1
{
$line =~ s/$find/0120/;
print OUT $line;
}

This is working to find the first digit of the PO number and insert the company number into field 1.
Field 19 is blank and I am trying into insert today's date in the YYYYMMDD format.
The result is
date "+%y%m%d"H,0120, 241350,GHXOWENS810,0992732,,,,,,1555255,,,,,,,,20090828

The value date "+%y%m%d" is being placed before the current value of field[0].

Can somebody help me?
 
Why are you using a regex when you now exactly what field you want to replace?
just do
$field[1] = '0120';
$field[19] = $date;

You should also move your
chomp($date = 'date "+%y%m%d"');
out of the foreach loop as your rechecking the date for every single line in your file, and I'm assuming you really only want to check it once.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I thought this was looking like a great solution, but all the company numbers are now 0800 and the date is still not populating.

I figure I am doing something wrong that is probably very basic.

I including the code.

#!/usr/bin/perl -w

#### program variables

# $sys="ocfp";
$sys="ocft";

$vPath1="/usr/lawson/lawson/bpm/emailattachments/";
$vPath2="/usr/lawson/lawson/$sys/edi/in/810/";
$vPath3="/usr/lawson/lawson/$sys/work/MA540CSV/";
$vName1="GHX810.txt";
$vName2="GHX810.out";
$vName3="ghxComp.txt";
$vName4="ghxInv.txt";
$vName5="MA540CSV";
$vName6="MA540CSV.txt";
$fname = "";
$TimeStamp = get_timestamp();
$date = get_date();

#### remove old files - general clean-up

$rc=system("rm /usr/lawson/lawson/bpm/emailattachments/ghxInv.txt");
$rc=system("rm /usr/lawson/lawson/bpm/emailattachments/ghxComp.txt");

#### fix the company numbers

open(IN, "$vPath3$vName5") or die 'Could not open file for reading';
open(OUT, ">$vPath3$vName6") or die 'Could not open file for writing';

foreach $line (<IN>)
{
@field = split(/\,/, $line);
$numb = substr($field[10],0,1); # first digit of number

if ($numb == 1) # PO number starts with a 1
{
$field[1] = '0120';
$field[19] = $date;
print OUT $line;
}
elsif ($numb == 7) # PO number starts with a 7
{
$field[1] = '0710';
$field[19] = $date;
print OUT $line;
}
elsif ($numb == 8) # PO number starts with a 8
{
$field[1] = '0800';
$field[19] = $date;
print OUT $line;
}
}
close(IN);
close(OUT);

$oldfile = "$vPath3$vName5";
$newfile = "$vPath3$vName6";

# delete oldfile
$rc=system("rm $oldfile");

# copy newfile to oldfile
if ($rc==0) {
$rc=system("cp $newfile $oldfile");
}
# delete newfile so only oldfile remains
$rc=system("rm $newfile");

#### create ghxInv.txt record

open(IN, "$vPath3$vName5") or die 'Could not open file for reading';
open(OUT, ">>$vPath1$vName4") or die 'Could not open file for writing';

foreach $line (<IN>)
{
@field = split(/\,/, $line);
#### format of output is Company,Invoice,PO_number
if ($field[0] eq "H")
{
$vLn = $field[1] . "," . $field[4] . "," . $field[10] . "\n";
print OUT $vLn;
}
}
close(IN);
close(OUT);

#### Now that the CSV file has companies, build a list of
#### comp with no dups & count

open(IN, "$vPath3$vName5") or die 'Could not open file for reading';

$arow = 0;
$array[0] = ([0,0]);

foreach $line (<IN>)
{
@field = split(/\,/, $line);
$numb = substr($field[10],0,1); # first digit of number
if ($field[0] eq "H")
{
$vTot++;
$f_index = 0;
$comp = $field[1];
for ($i= 0; $i < @array; $i++)
{
if ($array[$i][0] eq $comp)
{
$f_index = $i;
last;
}
}
if ($i < @array)
{
$array[$i][1]++; # found & $f_index is index
}
else
{
$count = 1;
$array[$arow] = ([$comp,$count]); # not found so add to array
$arow++;
}
}
}
close(IN);

#### Array is built - now sort it

@sorted_array = sort { $a->[0] <=> $b->[0] } @array;

#### Parse MA540CSV by Company

open(MyInFile, "/usr/lawson/lawson/$sys/work/MA540CSV/MA540CSV");

foreach $line (<MyInFile>)
{
@field = split(/\,/, $line);
if ($field[1] == "0120")
{
open (MyOutFile, ">>/usr/lawson/lawson/$sys/work/MA540CSV/MA540120");
print MyOutFile $line;
close(MyOutFile);
}
elsif ($field[1] == "0710")
{
open (MyOutFile, ">>/usr/lawson/lawson/$sys/work/MA540CSV/MA540710");
print MyOutFile $line;
close(MyOutFile);
}
elsif ($field[1] == "0800")
{
open (MyOutFile, ">>/usr/lawson/lawson/$sys/work/MA540CSV/MA540800");
print MyOutFile $line;
close(MyOutFile);
}
}
close(MyInFile);

#### now remove the main input file

$rc=system("rm /usr/lawson/lawson/$sys/work/MA540CSV/MA540CSV");

#### Now print the list & add date/time stamp to the end

$rc=system("rm $vPath1$vName3");

open (MyRptFile, ">>$vPath1$vName3");
print MyRptFile "Total number of records read is ";
print MyRptFile $vTot;
print MyRptFile "\n";
print MyRptFile "Company Count ";
print MyRptFile "\n";
print MyRptFile "**************************************";
print MyRptFile "\n";
for ($k= 0; $k < @sorted_array; $k++)
{
print MyRptFile $sorted_array[$k][0];
print MyRptFile " ";
print MyRptFile $sorted_array[$k][1];
print MyRptFile "\n";
}
print MyRptFile "**************************************";
print MyRptFile "\n";
print MyRptFile $TimeStamp;
print MyRptFile "\n";

close(MyRptFile);

#### The report is done so get the log from the ED502 run

@files= </usr/lawson/lawson/system/joblog/ed502-81*>;

foreach $file (@files)
{
$fname = $file;
}

$rc=system("rm $vPath1$vName1");
$rc=system("cp $fname $vPath1$vName1");

## save the raw data so it can be parsed and sent to EDM
## $rc=system("rm $vPath2$vName2");

sub get_timestamp
{
($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$mon++;
if ($mon < 10) { $mon = "0$mon"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }
$year=$year+1900;

return $year . '/' . $mon . '/' . $mday . '__' . $hour . ':' . $min . ':' .
$sec;
}

sub get_date
{
($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$mon++;
if ($mon < 10) { $mon = "0$mon"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }
$year=$year+1900;

return $year . $mon . $mday;
}
 
You're modifying the contents of the @field array, and then printing out the unmodified $line. Perhaps print OUT join(',',@field); is what you really want?

Annihilannic.
 
Going back to the OP, rather than using a cascade of IFs, use a hash - this allows you to handle errors more gracefully too
Perl:
my %companyCodes = {1 => '0120', 7 => '0710', 8 => '0800'};

foreach (<IN>) {
   chomp;
   my @fields = split /,/;
   my $code = substr($fields[10], 0, 1);
   $fields[1] = exists $companyCodes{$code} ? $companyCodes{$code} : '????';
   $fields[19] = $date;
   print OUT join(',', @fields), "\n";
}
There are a lot of things wrong with the program you posted - it reads and writes the file so many times, and the last step where you split the output into multiple files with an OPEN/CLOSE for every record makes me wince. I don't have time to do a full critique just now, but for starters:[ol][li]use strict and use warnings are your friends. Put them at the start of the program.[/li][li]Consider using Text::CSV_XS to parse the CSV data, it's more reliable than split[/li][li]Think about reading the whole file into an array of arrays at the start - after that, all your enrichment and summarisation can be done in memory, which will make it massively quicker[/li][/ol]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I am old programmer that is trying to learn new tricks, so I still develop code one part at a time. Sorry that that made you wince.

I tried your code and the date works, but the company number is ????

H,????, 241350,GHXOWENS810,0992732,,,,,,1555255,,,,,,,,20090828,2009094

There seems to be an issue with one line of code.

$fields[1] = exists $companyCodes{$code} ? $companyCodes{$code} : '????';

I am working on translating just what this is doing.

Could somebody tell what is wrong?
 
It is checking the hash to see if you have defined that company code, if it exists in the hash it sets it to the value, if it doesn't it sets it to ????. You probably need to define something there for when a new company number comes along you haven't thought of.. I'd probably just die right there instead or have it print out some warnings on that one, but you can do what ever is comfortable.



To help you out do a
print "$code\n";
right before that line and see if what it is getting is present in the hash.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
The solution was very simple,

$fields[1] = $companyCodes{$code};
 
unless it doesn't exist in your hash and then it's going to give out blank results.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You are 100% correct. Thank you for the help

This is working great
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top