function file_labels_to_indices ( filename1, filename2, labels )

%*****************************************************************************80
%
%% file_labels_to_indices() creates a file of indices from one of labels.
%
%  Discussion:
%
%    A file contains N lines, each containing one or more integer values.
%
%    Line I begins with a unique integer, which is the I-th label.
%
%    Any subsequent values on line I are also labels.  Any such value
%    must also occur as the first entry on some other line J, and hence
%    is the J-th label.
%
%    A list of labels has already been compiled.  Now it is desired to
%    make a copy of the input file, in which each label has been replaced
%    by its index.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    01 March 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME1, the name of the input file.
%
%    string FILENAME2, the name of the output file.
%
%    integer LABELS(*), the list of labels.
%
  input = fopen ( filename1, 'rt' );

  if ( input < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'file_labels_to_indices - Error!\n' );
    fprintf ( 1, '  Could not open the file "%s".\n', filename1 );
    error ( 'file_labels_to_indices - Error!' );
  end

  output = fopen ( filename2, 'wt' );

  if ( input < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'file_labels_to_indices - Error!\n' );
    fprintf ( 1, '  Could not open the file "%s".\n', filename2 );
    error ( 'file_labels_to_indices - Error!' );
  end

  while ( true )

    line = fgets ( input );

    if ( line == -1 )
      break;
    end

    [ x, count ] = sscanf ( line, '%d' );

    for j = 1 : count
      ndx = find ( labels == x(j) );
      fprintf ( output, '  %d', ndx );
    end
    fprintf ( output, '\n' );

  end

  fclose ( input );
  fclose ( output );

  return
end
