?
Solved

Modification to read and write file script -- Adam

Posted on 2007-10-04
5
Medium Priority
?
201 Views
Last Modified: 2010-03-05
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,123
John#$123,200#$188,061#$134,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#$244,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,254#$244,155#$333,133#$677,061





the script
my $dbfile="/temp_ee/dbfile.txt";


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);


0
Comment
Question by:MatthewF
  • 2
  • 2
5 Comments
 

Author Comment

by:MatthewF
ID: 20016408
I forgot to mention I need to have that second field printed out as well

print "Updating $input_file with $figure and $second_field\n";
0
 
LVL 85

Expert Comment

by:ozo
ID: 20017871
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#$figure/;
    }
 }
 print;
}
}
die "input_file '$input_file' not found\n" unless $found;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20017958
Do you need a script to add the field, or the above script to be modifed to work with an already added field?
0
 

Author Comment

by:MatthewF
ID: 20021142
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.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 20022902
my $dbfile="/temp_ee/dbfile.txt";


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=2; $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]}, 2, 1);
push @{$data[$found]}, $figure;

open(OUT, ">$dbfile") or die "Could not open output file: $!\n";
foreach (@data) {
      print OUT join("#", @{$_}) . "\n";
}
close(OUT);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question