return multiple values from an awk function? 
Author Message
 return multiple values from an awk function?

I know how to write an awk function that returns a single value.
Is it possible to return multiple values from an awk function?

If it is, could someone show me how to write a trivial function
that returns the square and cube of a number?



Sat, 24 Sep 2005 23:52:44 GMT  
 return multiple values from an awk function?

Quote:

> I know how to write an awk function that returns a single value.
> Is it possible to return multiple values from an awk function?

> If it is, could someone show me how to write a trivial function
> that returns the square and cube of a number?

   This sounds suspiciously like homework.

   Note that:

eval `echo "square=4 cube=8"`

   will assign 4 to $square and 8 to $cube

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Sun, 25 Sep 2005 00:09:39 GMT  
 return multiple values from an awk function?

Quote:


>> I know how to write an awk function that returns a single value.
>> Is it possible to return multiple values from an awk function?

>> If it is, could someone show me how to write a trivial function
>> that returns the square and cube of a number?

>    This sounds suspiciously like homework.

>    Note that:

> eval `echo "square=4 cube=8"`

>    will assign 4 to $square and 8 to $cube

   OOPS! I thought I was still in comp.unix.shell.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Sun, 25 Sep 2005 00:12:16 GMT  
 return multiple values from an awk function?

Quote:

> I know how to write an awk function that returns a single value.
> Is it possible to return multiple values from an awk function?

No you cannot return more than one value from a function.

So if you *really* want to return more than one you have to
cheat. One way of cheating is to use one or more global
variables (possibly a global array if there are lots of values
to be "returned"). Another is to combine the return values
into one, perhaps as a string or mathematically.

John.



Sun, 25 Sep 2005 00:21:10 GMT  
 return multiple values from an awk function?
Quote:


>>I know how to write an awk function that returns a single value.
>>Is it possible to return multiple values from an awk function?

> No you cannot return more than one value from a function.

> So if you *really* want to return more than one you have to
> cheat. One way of cheating is to use one or more global
> variables (possibly a global array if there are lots of values
> to be "returned"). Another is to combine the return values
> into one, perhaps as a string or mathematically.

> John.

However, since arrays passed as parameters CAN be modified,
use them.


Sun, 25 Sep 2005 03:15:43 GMT  
 return multiple values from an awk function?

% I know how to write an awk function that returns a single value.
% Is it possible to return multiple values from an awk function?

Not really, but for numerics, you can concatenate them together and
split them out in the caller. For instance

  $0 = sqcb($1)
  square = $1
  cube = $2

  function sqcb(a)
  {
    return a*a " " a*a*a
  }

If the contents of $0 can't be overwritten, you can assign to a string
and use split on it.

--

Patrick TJ McPhee
East York  Canada



Sun, 25 Sep 2005 23:15:53 GMT  
 return multiple values from an awk function?


Quote:


> % I know how to write an awk function that returns a single value.
> % Is it possible to return multiple values from an awk function?

> Not really, but for numerics, you can concatenate them together and
> split them out in the caller. For instance

>   $0 = sqcb($1)
>   square = $1
>   cube = $2

>   function sqcb(a)
>   {
>     return a*a " " a*a*a
>   }

> If the contents of $0 can't be overwritten, you can assign to a string
> and use split on it.

As Martin Cohen reminds us, arrays are passed by reference
so can be used to pass values back from functions, which,
now he mentions it, is used in my own scripts but in general
it seems an odd thing to do. If the OP does this, then probably
the thing to actually *return* is the number of array elements
(where a negative return can indicate an error).

Each day I run a few dozen awk programs over hundreds
of megabytes of data but there are only a couple of functions
which do this. Certainly the idea of a combined cube/square
function seems contrived, but perhaps the OP was merely
using it as an artificial example.

John.



Sun, 25 Sep 2005 23:36:39 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. multiple values in a return from function

2. multiple return values for functions

3. Return value from awk snipet via korn shell subroutine

4. returning a value from awk

5. Q: Reading Return Value from AWK Program

6. ???Eiffel idiom for multiple return values???

7. Tuples, iterators, and multiple return values in Eiffel?

8. Wait ms Timer Multiple returning inaccurate timer value

9. Multiple return values

10. multiple return values

11. Multiple return values

12. Newbie on multiple return values

 

 
Powered by phpBB® Forum Software