?
Solved

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

Posted on 2003-03-30
5
Medium Priority
?
203 Views
Last Modified: 2010-03-05
0
Comment
Question by:alexswebsite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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 84

Expert Comment

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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

752 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