Read and write to a file

Posted on 2007-09-28
Last Modified: 2010-03-05
I have a file I need to read and write to. The file is pound (#) delimited and it contains a list of filenames and 5 sets of numbers, ie


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


For example if the input file was John  and the figure was $677,061,  the new line for John would be
Question by:MatthewF
    LVL 39

    Accepted Solution

    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>) {
          push @data, [split /#/];
          next unless $data[-1]->[0] eq $input_file;
          for(my $i=1; $i<=$#{$data[-1]}; $i++) {
                if($data[-1]->[$i] eq $figure) {
                      print "$figure is equal to previous figure\n";

    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";
    LVL 17

    Expert Comment

    I'm a little confused about what you're trying to do -- you say you only have one pound delimited file to read, but then speak of "If the input file was Tom..." and "...if the input file was John...".  See if this is close:

    #!/usr/bin/perl -w
    use strict;

    my (%data, @order, $match);

    #my $figure = '$121,234';
    #my $person = 'Tom';

    my $figure = '$677,061';
    my $person = 'John';

    open (IN, "org_file.txt") or die;
    while (<IN>) {
        my @line = split /#/;
        chomp (@line);
        push @order, $line[0];
        $data{$line[0]} = [@line[1 .. $#line]];

    if (exists $data{$person}) {
        for (@{$data{$person}}) {
            if ($_ eq $figure) {
            $match = $figure;
        if ($match) {
            print "$person already has $match\n";
        } else {
            $data{$person}[-1] = "$figure";

    open (OUT, ">new_file.txt") or die;

    for (@order) {
        print OUT "$_#", join ("#", @{$data{$_}}), "\n";

    You need to give it the person you want and the figure you're looking to match.  If, for example, you feed it:

    my $figure = '$121,234';
    my $person = 'Tom';

    It prints out "Tom already has $121,234", and org_file.txt and new_file.txt are identical.  If instead you feed it:

    my $figure = '$677,061';
    my $person = 'John';

    It doesn't print out anything, but new_file.txt becomes:


    while org_file.txt remains:


    Is this close?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now