Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk issue (return an array from user defined function)

I have this function (converting coordinate of an WGS84 geoid to cartesian coordinates...doesn't matter):

  function convert_geo_to_enu(coord_geo) { \
    xi=sqrt(1 - e*e*sin(coord_geo[1])*sin(coord_geo[2])); \
        \
        coord_enu[1]=(a/xi + coord_geo[3])*cos(coord_geo[1])*cos(coord_geo[2]); \
        print coord_enu[1] " hhh " ; \
        coord_enu[2]=(a/xi + coord_geo[3])*cos(coord_geo[1])*sin(coord_geo[2]); \
        coord_enu[3]=(a*(1-e*e)/xi0 + coord_geo[3])*sin(coord_geo[1]); \
    \
    return coord_enu \   # <-- here comes the problem
  } \


problem --> mawk: line 64: illegal reference to array coord_enu

What is the problem to return as an array? Is there any different syntax?

I could use:

  function convert_geo_to_enu(coord_geo, coord_enu) { \
       ...
       coord_enu[1]=...
       ...
  } \

or even:

  function convert_geo_to_enu(coord_geo) { \
       ...
       coord_enu[1]=...
       ...
  } \

and then just use the variable coord_enu as a global?

But it looks better with usage of return statement (esp. for me)

like image 264
static Avatar asked Sep 10 '12 10:09

static


1 Answers

A little late, but here's a solution I used:

I sent a variable I wanted filled as a parameter to the function:

function myfunct(result, array_size) {
    for(i = 1; i <= array_size; i++) {
        result[i] = ##whatever you want
    }
}

then, you can treat the result variable as an array outside of the function

hope this helps someone looking for an answer

like image 85
Mike Avatar answered Sep 27 '22 17:09

Mike