### Jared Seaman
### 7/12/99
### 3 File-Reading Facilities; 
###   one will read the file & return its contents as a string
###   the other will read a file & return each line as an element in an array
###   New, 7/12: file_into_sorted_array

1;


##########################READ_IN_FILE
#This subroutine will read the entirity of a file,
#& return all the contents as a string.
sub read_in_file ()
  {
    my $infile = $_[0]; #the file we're reading from; the argument of the subprogram
    my $contents; #contains the contents of the file; will be returned.
    my $incoming; #a temporary storage variable, for reading lines in.
    local (*phile);
    open (phile, "<$infile") or die "ZAM!  Reading in ".$infile." didn't work for Read_I_File"; #open file
    #Now, let's get all the contents of this file in the string
    #While there's stuff left in the file..
    while ($incoming = <phile>)
      {#read it, & put it into contents:
        $contents = $contents.$incoming;
      }
    #When it's all read in, return it:
    return $contents;
  }

#%%%%%%%%%%%%%%%%%%
##########################FILE_INTO_ARRAY
#This subroutine will read the entirity of a file,
#putting each (chomped) line into an array, AND SETTING THE 0th ELEMENT
#TO BE THE LENGTH OF THE ARRAY
sub file_into_array ()
  {
    my $infile = $_[0]; #the file we're reading from; the argument of the subprogram
    my @contents; #contains the contents of the file; will be returned.
    my $incoming; #a temporary storage variable, for reading lines in.
    my $i = 0; #an iterator
    local (*phile);
    open (phile, "<$infile") or die "ZAM!  Reading in ".$infile." didn't work for F_I_ARRAY"; #open file
    #Now, let's get all the contents of this file in the string
    #While there's stuff left in the file..
    while ($incoming = <phile>)
      {#read it, & put it into the array where it belongs:
        $i++;
        $contents[$i] = chomp($incoming);
      }
    #When it's all read in, put the iterator in at spot 0 & return it:
    $contents[0] = $i;
    return @contents;
  }

#%%%%%%%%%%%%%%%%%%%
##########################FILE_INTO_SORTED_ARRAY
#This subroutine will read the entirity of a file,
#putting each line into an array, sorted by priority; within priority, it is sorted
#by the order it appeared in the file.
sub file_into_sorted_array ()
  {
    my $infile = $_[0]; #the file we're reading from; the argument of the subprogram
    #the regular expression we're using to extract the thing we
    #should sort the elements by.  $2 is the value we keep.
    my $reggy = $_[1];
    #These 2 arrays help insure that, no matter how Perl's Sort is implemented,
    #when 2 rules of the same priority appear in a file, the one appearing earlier
    #in the file will be run first.
    my @sort_aid;
    my @neg_sort_aid;

    my @contents; #contains the contents of the file; will be returned.
    my $incoming; #a temporary storage variable, for reading lines in.
    my $i, $j, $k, $l = 0; #iterators/random math-assisting variables
    local (*phile);
    open (phile, "<$infile") or die "ZAM!  Reading in ".$infile." didn't work for F_I_ARRAY"; #open file
    #Now, let's get all the contents of this file in the string
    #While there's stuff left in the file..
    while ($incoming = <phile>)
      {#read it, & put it into the array:
        #If we have a match with the regular expression telling us what to sort by, 
        #we append that to the beginning of the variable & write it into the array.
        #If not, we append "0" to the beginning of the variable & write it into the array.
        #if ($infile =~ /$reggy/)
        chomp($incoming);
        #let's hardcode the pattern match
        if ($incoming =~ /$reggy/)
          {
            $j = $2;
            if ((!$j) || ($j eq "-")) {$j = 0;}
          }
        #Now, we make sure that the first occurence of priority X will be numerically
        #first, the 2nd will be 2nd, etc.
        if ($j < 0)
          {
            #k is the number recorded in the negative value of j; it is
            #the number of times j has already occured.
            $k = $neg_sort_aid[($j*-1)];
            #we increment the number of times rule priority $j has been used.
            $neg_sort_aid[($j*-1)]++;
          }
        else #if j isn't negative, we read from the positive array
          {
            #k is the number recorded in the value of j; it is
            #the number of times j has already occured.
            $k = $sort_aid[$j];
            #& increment it
            $sort_aid[$j]++;
          }
        $l = 1;
        #Ok, hak explaination: we get #.1 for the 1st priority #, you get #.01 for
        #the 2nd priority #, etc.
        
        $l = $l/(2*($k+1));
        #we add $l, so the earlier occurance of any given priority is a higher number
        $j = $j+$l;
        #now we append the priority at the front of the string, so it will be sorted right
        #once we've read everything in.
        $contents[$i] = $j." ".$incoming;
        $i++;
      }#close while
    #When it's all read in, put the iterator in at spot 0 & return it:
    sub headnum {$a <=>$b;}
    @contents = sort headnum @contents;
    #returns it with the largest value first.
    @contents = reverse @contents;
    
    #Now let's trim the numbers off the front of the rules...
    $i = @contents; #i = # of elements in contents
    #The numbers are removed by this while loop.
    while ($i > -1)
      {
        $contents[$i] =~ s/(-|)\d+?\.\d+? //;
        $i--;
      } 
    return @contents;
  }#closes file-to-sorted-array

