#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - field      *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
#  Print a specified column No. from a table.
#
#  Takes a column number and outputs the corresponding columns of the 
#  input table. If two numbers are specified, then outputs the
#  corresponding range of columns. If the second number is lesser
#  than the first, then all columns starting from the first column
#  number to the end of table are printed. If no columns or if a non
#  numeric or out-of-range column is specified for the first field, then
#  nothing is printed to STDOUT. The same happens if a non-numeric
#  column is specified for the second field.
#
#  This NoSQL operator reads a table on STDIN and writes a table to STDOUT.
#
########################################################################

########################################################################
# BEGIN block
########################################################################

BEGIN \
{
  NULL = ""; FS = OFS = "\t";
  if ( ! split( __nosql_args, range, " " ) ) exit
  if ( range[2] == NULL ) range[2] = range[1]

  # Make sure values are numeric.

  if ( ( range[1]/1 ) != range[1] || ( range[2]/1 ) != range[2]) exit
  if ( range[1] < 0 ) exit
  if ( range[2] < 0 ) range[2] = NULL
}

########################################################################
# Main loop
########################################################################

NR == 1 \
{
  if ( range[1] > NF ) exit
  if ( range[2] == NULL || range[2] > NF ) range[2] = NF
  if ( range[2] < range[1] ) range[2] = NF
}

{ 
  out_rec = $range[1]
  for ( i = range[1]+1; i <= range[2]; i++ ) out_rec = out_rec OFS $i
  print out_rec
}

