function value_out = byname ( action, name, value_in )

%*****************************************************************************80
%
%% byname() controls a set of named persistent data.
%
%  Discussion:
%
%    Three values are stored, named ALPHA, BETA and GAMMA.
%
%    Note that strings should be delimited by DOUBLE QUOTES, not SINGLE QUOTES!
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 May 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string ACTION (case insensitive, only first character important)
%    "Get"     get the value;
%    "Print"   print the value;
%    "Reset"   reset value to default;
%    "Set"     set the value;
%
%    string NAME, the name of the parameter (case insensitive)
%    "ALPHA"
%    "BETA"
%    "GAMMA"
%    "*" (all variables)
%    NAME is required for "Get" and "Set".  
%    NAME, if omitted, is assumed "*" for "Print" and "Reset".
%
%    VALUE_IN.
%    Only used for "Set" command.
%
%  Output:
%
%    VALUE_OUT.
%    If NAME was specified on input, and was not "*", then
%    VALUE_OUT is the current value of the corresponding variable.
%    Otherwise, VALUE_OUT is empty.
%
  persistent alpha
  persistent alpha_default
  persistent beta
  persistent beta_default
  persistent gamma
  persistent gamma_default

  if ( isempty ( alpha_default ) )
    alpha_default = 1.0;
    alpha = alpha_default;
  end

  if ( isempty ( beta_default ) )
    beta_default = 2.0;
    beta = beta_default;
  end

  if ( isempty ( gamma_default ) )
    gamma_default = 3.0;
    gamma = gamma_default;
  end
%
%  No arguments: default "print *"
%
  if ( nargin < 1 )
    action = "p";
    name = "*";
  end
%
%  Reject bad action.
%
  action2 = lower ( action{1}(1) );
  if ( ~ ( action2 == "g" | action2 == "p" | action2 == "r" | action2 == "s" ) )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'byname: Fatal error!\n' );
    fprintf ( 1, '  Legal actions are "g", "p", "r", "s", not "%s"\n', action );
    error ( 'byname - Fatal error!' )
  end

  if ( ( action2 == "p" | action2 == "r" ) & nargin < 2 )
    name = "*";
  end
%
%  Reject bad name.
%  MATLAB is fussy about checking string equality.
%
  name2 = lower ( name );
  match = ...
    ( name2 == "alpha" ) | ...
    ( name2 == "beta" ) | ...
    ( name2 == "gamma" ) | ...
    ( name2 == "*" ) ;

  if ( ~ match )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'byname: Fatal error!\n' );
    fprintf ( 1, '  Legal names are "alpha", "beta", "gamma", "*", not "%s"\n', name );
    error ( 'byname - Fatal error!' )
  end
%
%  Reject missing value.
%
  if ( action2 == "s" & nargin < 3 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'byname: Fatal error!\n' );
    fprintf ( 1, '  "set, name" command but no "value".\n' );
    error ( 'byname - Fatal error!\n' );
  end
%
%  Carry out requested action.
%
  value_out = [];
  if ( action2 == "g" )
    if ( name2 == "alpha" )
      value_out = alpha;
    elseif ( name2 == "beta" )
      value_out = beta;
    elseif ( name2 == "gamma" )
      value_out = gamma;
    end
  elseif ( action2 == "p" )
    if ( name2 == "alpha" | name2 == "*" )
      disp ( [ "alpha", " = ", num2str(alpha) ] );
    end
    if ( name2 == "alpha" )
      value_out = alpha;
    end
    if ( name2 == "beta" | name2 == "*" )
      disp ( [ "beta", " = ", num2str(beta) ] );
    end
    if ( name2 == "beta" )
      value_out = beta;
    end
    if ( name2 == "gamma" | name2 == "*" )
      disp ( [ "gamma", " = ", num2str(gamma) ] );
    end
    if ( name2 == "gamma" )
      value_out = gamma;
    end
  elseif ( action2 == "r" )
    if ( name2 == "alpha" | name2 == "*" )
      alpha = alpha_default;
    end
    if ( name2 == "alpha" )
      value_out = alpha;
    end
    if ( name2 == "beta" | name2 == "*" )
      beta = beta_default;
    end
    if ( name2 == "beta" )
      value_out = beta;
    end
    if ( name2 == "gamma" | name2 == "*" )
      gamma = gamma_default;
    end
    if ( name2 == "gamma" )
      value_out = gamma;
    end
  elseif ( action2 == "s" )
    if ( name2 == "alpha" )
      alpha = value_in;
      value_out = alpha;
    end
    if ( name2 == "beta" )
      beta = value_in;
      value_out = beta;
    end
    if ( name2 == "gamma" )
      gamma = value_in;
      value_out = gamma;
    end
  end

  return
end

