?
Solved

Take pieces from one file, add them up and put them in another file.

Posted on 2003-03-30
5
Medium Priority
?
208 Views
Last Modified: 2010-03-05
0
Comment
Question by:alexswebsite
  • 3
  • 2
5 Comments
 

Author Comment

by:alexswebsite
ID: 8236309
HERE IS THE QUESTION....there is a problem with the ask a question page...


I'm trying to write a program that does the following:
 > opens the file $ProfileData, of which each line represents a different person, with data about that person divided by the | symbol
 > goes through each line of that file, calculating the age of that person by subtracting the year they were born from this year
 > creates a new file, called age.txt that sumarizes the ages, with one row for each age, and after a | symbol, it show the total number of people from the $ProfileData file that are that age.

For Example:  If this is $ProfileData :

person1|hisname|2002|other crap|
person2|hername|1998|more about her|
person3|hername|2002|other stuff|
person3|anothername|2002|more|
person4|name|1998|etc.|
person5|thisname|1993|stuff|

then, age.txt should look like:

1|3
5|2
10|1

because there are 3 people that are 1 year old, 2 that are 5 years old and 1 that is 10 years old.

My attempt at figuring this out is below.  It quazi works.  You can see the results at http://www.absolutcollectors.com/community/stats/age.txt

It doesn't seem to ever do the &addOne; function below.  I also think there is some way that this could be done using more arrays to save work, but I'm not sure how to do that.

Thanks in advance for any help!
Alex




&buildAgeFile;

sub buildAgeFile {

      open (PROFILES,"$ProfileData");
      @profiles = <PROFILES>;
      close (PROFILES);

      foreach $line (@profiles) {
            @entry = split(/\s*\|\s*/,"$line");

            $AGE = $this_year - $entry[9];

            $STATFILE = $AgeStats;
            $MATCHTHIS = $AGE;
            &checkIfExists;
      }

      &sortFile;
      &showFile;
}

sub checkIfExists {

      # Requires $STATFILE
      # Requires $MATCHTHIS

      open (STATFILE,"$STATFILE");
      @statfile = <STATFILE>;
      close (STATFILE);

      foreach $line (@statfile) {
            @stat = split(/\s*\|\s*/,"$line");

            if ($stat[0] eq '$MATCHTHIS') {

                  &addOne;
                  $MATCHED = "yes";

            }
      }
      
      if (!$MATCHED) {
            &addFirst;
      }

      flock(AGE,8);
      close (AGE);


}


sub addOne {

      open (STATFILEIN,"$STATFILE");
      @stats = <STATFILEIN>;
      close (STATFILEIN);

      open(STATFILEOUT, ">$STATFILE");
      flock(STATFILEOUT,2);

      foreach $line (@stats) {
            @position = split(/\s*\|\s*/,"$line");

            if ($position[0] eq '$MATCHTHIS') {
                  $NEW_COUNT = $position[1] + 1;
                  print STATFILEOUT "$MATCHTHIS|$NEW_COUNT|\n";
            } else {
                  print STATFILEOUT "$line";
            }
      }

      flock(STATFILEOUT,8);
      close (STATFILEOUT);
}

sub addFirst {

      # Requires $STATFILE
      # Requires $MATCHTHIS

      open (STATFILEIN,"$STATFILE");
      @stats = <STATFILEIN>;
      close (STATFILEIN);

      open(STATFILEOUT, ">$STATFILE");
      flock(STATFILEOUT,2);

      print STATFILEOUT "$MATCHTHIS|1|\n";

      foreach $line (@stats) {
            print STATFILEOUT "$line";
      }

      flock(STATFILEOUT,8);
      close (STATFILEOUT);
}

sub sortFile {

      # Requires $STATFILE

   

      open (INPUT, "$STATFILE");

      while (<INPUT>)  {


            # remove new line character and ending spaces if exist:

            $_ =~ s/\n//;

            $_ =~ s/\s+$//;


            # if the line is not empty, add it to the array of arrays (@AofA)

            if ($_) {

                  @TempA = split(/\|/,$_);

                  $elcount = undef;          

                  foreach $Element (@TempA) {

                        $AofA[$count][$elcount]     = $Element;

                        $elcount++;

                  };

                  $count++;

            };

      };

      close (INPUT);



      # Now Let's sort this by the first element

      @result= sort {$a->[0] cmp $b->[0]} @AofA;



      # let's rebuild the list

      foreach (@result) {

            $refer = $_;

            @entry = @$refer;

            push (@ResultList,("$entry[0]|$entry[1]|\n"));

      };



      # now lets re-create the original file:

      open (OUTPUT, ">$STATFILE");

      foreach (@ResultList) {

            print OUTPUT $_;    

      };

      close (OUTPUT);

}

sub showFile { open(STATFILE, "$STATFILE"); @statfile = <STATFILE>; print @statfile; close(STATFILE); }
0
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 8236510
$STATFILE = "age.txt";
$this_year=(localtime)[5]+1900;
open (PROFILES,"<$ProfileData") or die "can't open $ProfileData $!";
while( <PROFILES> ){
    $agecount{$this_year - (split/\|/)[2]}++;
}
close PROFILES;
open OUTPUT,">$STATFILE" or die "Can't open $STATFILE $!";
for( sort {$a<=>$b} keys %agecount ){
     print OUTPUT "$_|$agecount{$_}\n";
}
0
 

Author Comment

by:alexswebsite
ID: 8236957
wow, that was a lot easier than I was making it.  

Thanks very much!
Alex
0
 

Author Comment

by:alexswebsite
ID: 8237050
How can I make sure that the OUTPUT file is cleared out completely before new data is added to it?
0
 
LVL 85

Expert Comment

by:ozo
ID: 8237092
open OUTPUT,">$STATFILE";#should clear out the OUTPUT file completely
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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

621 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