Detect optional function argument (array)
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Consider this function:
function add_one(in_ar, each)
for (each in in_ar)
in_ar[each]++
I would like to modify it such that if a second array is provided, it would be
used instead of modifying the input array. I tried this:
function add_one(in_ar, out_ar, each)
if (out_ar)
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
add_one(q, z)
print z[3]
but I get this result:
fatal: attempt to use scalar `z' as an array
awk array function arguments
add a comment |Â
up vote
2
down vote
favorite
Consider this function:
function add_one(in_ar, each)
for (each in in_ar)
in_ar[each]++
I would like to modify it such that if a second array is provided, it would be
used instead of modifying the input array. I tried this:
function add_one(in_ar, out_ar, each)
if (out_ar)
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
add_one(q, z)
print z[3]
but I get this result:
fatal: attempt to use scalar `z' as an array
awk array function arguments
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Consider this function:
function add_one(in_ar, each)
for (each in in_ar)
in_ar[each]++
I would like to modify it such that if a second array is provided, it would be
used instead of modifying the input array. I tried this:
function add_one(in_ar, out_ar, each)
if (out_ar)
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
add_one(q, z)
print z[3]
but I get this result:
fatal: attempt to use scalar `z' as an array
awk array function arguments
Consider this function:
function add_one(in_ar, each)
for (each in in_ar)
in_ar[each]++
I would like to modify it such that if a second array is provided, it would be
used instead of modifying the input array. I tried this:
function add_one(in_ar, out_ar, each)
if (out_ar)
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
add_one(q, z)
print z[3]
but I get this result:
fatal: attempt to use scalar `z' as an array
awk array function arguments
edited Jun 26 at 11:31
asked Jun 26 at 2:38
Steven Penny
2,31121535
2,31121535
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
There are 2 problems in your script
- the variable
z
isn't initialized - the test
if(out_ar)
in your second code snippet is not suited for arrays
To solve the first problem, you need to assign an array element (like z[1]=1
) since there is no array declaration in awk. (You can't use similar statement like declare -A
as you would do in bash).
The second problem can be solved, provided you're using GNU awk, with the function isarray()
or typeof()
.
So your code should look like this:
function add_one(in_ar, out_ar, each)
if (isarray(out_ar))
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
z[1]=1
add_one(q, z)
print z[3]
I recommend looking at this page and this page.
1
Another way to makez
an array is to delete it:delete z
ordelete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)
â muru
Jun 26 at 6:56
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There are 2 problems in your script
- the variable
z
isn't initialized - the test
if(out_ar)
in your second code snippet is not suited for arrays
To solve the first problem, you need to assign an array element (like z[1]=1
) since there is no array declaration in awk. (You can't use similar statement like declare -A
as you would do in bash).
The second problem can be solved, provided you're using GNU awk, with the function isarray()
or typeof()
.
So your code should look like this:
function add_one(in_ar, out_ar, each)
if (isarray(out_ar))
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
z[1]=1
add_one(q, z)
print z[3]
I recommend looking at this page and this page.
1
Another way to makez
an array is to delete it:delete z
ordelete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)
â muru
Jun 26 at 6:56
add a comment |Â
up vote
1
down vote
There are 2 problems in your script
- the variable
z
isn't initialized - the test
if(out_ar)
in your second code snippet is not suited for arrays
To solve the first problem, you need to assign an array element (like z[1]=1
) since there is no array declaration in awk. (You can't use similar statement like declare -A
as you would do in bash).
The second problem can be solved, provided you're using GNU awk, with the function isarray()
or typeof()
.
So your code should look like this:
function add_one(in_ar, out_ar, each)
if (isarray(out_ar))
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
z[1]=1
add_one(q, z)
print z[3]
I recommend looking at this page and this page.
1
Another way to makez
an array is to delete it:delete z
ordelete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)
â muru
Jun 26 at 6:56
add a comment |Â
up vote
1
down vote
up vote
1
down vote
There are 2 problems in your script
- the variable
z
isn't initialized - the test
if(out_ar)
in your second code snippet is not suited for arrays
To solve the first problem, you need to assign an array element (like z[1]=1
) since there is no array declaration in awk. (You can't use similar statement like declare -A
as you would do in bash).
The second problem can be solved, provided you're using GNU awk, with the function isarray()
or typeof()
.
So your code should look like this:
function add_one(in_ar, out_ar, each)
if (isarray(out_ar))
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
z[1]=1
add_one(q, z)
print z[3]
I recommend looking at this page and this page.
There are 2 problems in your script
- the variable
z
isn't initialized - the test
if(out_ar)
in your second code snippet is not suited for arrays
To solve the first problem, you need to assign an array element (like z[1]=1
) since there is no array declaration in awk. (You can't use similar statement like declare -A
as you would do in bash).
The second problem can be solved, provided you're using GNU awk, with the function isarray()
or typeof()
.
So your code should look like this:
function add_one(in_ar, out_ar, each)
if (isarray(out_ar))
for (each in in_ar)
out_ar[each] = in_ar[each] + 1
else
for (each in in_ar)
in_ar[each]++
BEGIN
split("1 2 3 4 5", q)
z[1]=1
add_one(q, z)
print z[3]
I recommend looking at this page and this page.
edited Jun 26 at 11:39
answered Jun 26 at 6:51
oliv
84927
84927
1
Another way to makez
an array is to delete it:delete z
ordelete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)
â muru
Jun 26 at 6:56
add a comment |Â
1
Another way to makez
an array is to delete it:delete z
ordelete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)
â muru
Jun 26 at 6:56
1
1
Another way to make
z
an array is to delete it: delete z
or delete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)â muru
Jun 26 at 6:56
Another way to make
z
an array is to delete it: delete z
or delete z[""]
, etc. Also see: unix.stackexchange.com/q/359592/70524 (by OP)â muru
Jun 26 at 6:56
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451896%2fdetect-optional-function-argument-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password