Detect optional function argument (array)

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question

























    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






    share|improve this question























      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






      share|improve this question













      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








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 26 at 11:31
























      asked Jun 26 at 2:38









      Steven Penny

      2,31121535




      2,31121535




















          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.






          share|improve this answer



















          • 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










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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.






          share|improve this answer



















          • 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














          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.






          share|improve this answer



















          • 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












          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.






          share|improve this answer















          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.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 26 at 11:39


























          answered Jun 26 at 6:51









          oliv

          84927




          84927







          • 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












          • 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







          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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Christian Cage

          How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?