MatthewF
asked on
Modification to read and write file script -- Adam
I have a script, thanks Adam, that reads a write to a file The file is pound (#) delimited and it contains a list of filenames and 5 sets of numbers, ie
Tom#$120,200#$154,001#$121 ,234#$234, 123#$345,1 23
John#$123,200#$188,061#$13 4,254#$244 ,155#$333, 133
Etc&
I need to do a few things:
Open this file and check is a previously set variable $input_file is any of the first field,
If true check is another previously set variable, $figure is equal to any of the 5 sets of numbers.
If true print out a statement $figure is equal to previous figure
If not true pop off the first figure and write the new number to the end of the line.
For example
If the input file was Tom and the figure was 121,234, just a print out statement $figure is equal to previous figure
Or
For example if the input file was John and the figure was $677,061, the new line for John would be
John#$188,061#$134,254#$24 4,155#$333 ,133#$677, 061
The Change I need is to add another field, one that would not be changed, much like the first field is not, ie
John#FATAL#$188,061#$134,2 54#$244,15 5#$333,133 #$677,061
the script
my $dbfile="/temp_ee/dbfile.t xt";
my $input_file="John";
my $figure='$677,061';
my @data;
my $found;
#Read data
open(IN, "$dbfile") or die "could not open input file: $!\n";
while(<IN>) {
chomp;
push @data, [split /#/];
next unless $data[-1]->[0] eq $input_file;
$found=$#data;
for(my $i=1; $i<=$#{$data[-1]}; $i++) {
if($data[-1]->[$i] eq $figure) {
print "$figure is equal to previous figure\n";
exit;
}
}
}
close(IN);
unless(defined($found)) {
die "input_file '$input_file' not found\n";
}
#remove first number
print "Updating $input_file with $figure\n";
splice(@{$data[$found]}, 1, 1);
push @{$data[$found]}, $figure;
open(OUT, ">$dbfile") or die "Could not open output file: $!\n";
foreach (@data) {
print OUT join("#", @{$_}) . "\n";
}
close(OUT);
Tom#$120,200#$154,001#$121
John#$123,200#$188,061#$13
Etc&
I need to do a few things:
Open this file and check is a previously set variable $input_file is any of the first field,
If true check is another previously set variable, $figure is equal to any of the 5 sets of numbers.
If true print out a statement $figure is equal to previous figure
If not true pop off the first figure and write the new number to the end of the line.
For example
If the input file was Tom and the figure was 121,234, just a print out statement $figure is equal to previous figure
Or
For example if the input file was John and the figure was $677,061, the new line for John would be
John#$188,061#$134,254#$24
The Change I need is to add another field, one that would not be changed, much like the first field is not, ie
John#FATAL#$188,061#$134,2
the script
my $dbfile="/temp_ee/dbfile.t
my $input_file="John";
my $figure='$677,061';
my @data;
my $found;
#Read data
open(IN, "$dbfile") or die "could not open input file: $!\n";
while(<IN>) {
chomp;
push @data, [split /#/];
next unless $data[-1]->[0] eq $input_file;
$found=$#data;
for(my $i=1; $i<=$#{$data[-1]}; $i++) {
if($data[-1]->[$i] eq $figure) {
print "$figure is equal to previous figure\n";
exit;
}
}
}
close(IN);
unless(defined($found)) {
die "input_file '$input_file' not found\n";
}
#remove first number
print "Updating $input_file with $figure\n";
splice(@{$data[$found]}, 1, 1);
push @{$data[$found]}, $figure;
open(OUT, ">$dbfile") or die "Could not open output file: $!\n";
foreach (@data) {
print OUT join("#", @{$_}) . "\n";
}
close(OUT);
my $input_file="John";
my $figure='$677,061';
my $found;
{local $^I=".bak"; local @ARGV=($dbfile);
while( <> ){
if( /^\Q$input_file\E# ){
$found=$.;
if( /#\Q$figure\Q#/ ){
print "$figure is equal to previous figure\n";
}else{
s/#[^#]*#(.*)/#FATAL#$1#$f igure/;
}
}
print;
}
}
die "input_file '$input_file' not found\n" unless $found;
my $figure='$677,061';
my $found;
{local $^I=".bak"; local @ARGV=($dbfile);
while( <> ){
if( /^\Q$input_file\E# ){
$found=$.;
if( /#\Q$figure\Q#/ ){
print "$figure is equal to previous figure\n";
}else{
s/#[^#]*#(.*)/#FATAL#$1#$f
}
}
print;
}
}
die "input_file '$input_file' not found\n" unless $found;
Do you need a script to add the field, or the above script to be modifed to work with an already added field?
ASKER
adam,
the second field willbe there as part of the orginal line. Howver, it may not always have the word FATAL. I just want to copy whatever word tis there.
the second field willbe there as part of the orginal line. Howver, it may not always have the word FATAL. I just want to copy whatever word tis there.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
print "Updating $input_file with $figure and $second_field\n";