awk referring to an array element











up vote
4
down vote

favorite












#!/usr/bin/env bash
awk '
BEGIN {
arr[A]=1;
arr[B]=1;
arr[C]=1;
arr[E]=1;
arr[J]=8;
arr[Q]=10;
print arr[J]
}'


the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].



Also, like in the script above, I don't want to assign values to arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and common value as the other parameter to a function that handles the logic of assigning them value.










share|improve this question









New contributor




HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    4
    down vote

    favorite












    #!/usr/bin/env bash
    awk '
    BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
    }'


    the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].



    Also, like in the script above, I don't want to assign values to arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and common value as the other parameter to a function that handles the logic of assigning them value.










    share|improve this question









    New contributor




    HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      #!/usr/bin/env bash
      awk '
      BEGIN {
      arr[A]=1;
      arr[B]=1;
      arr[C]=1;
      arr[E]=1;
      arr[J]=8;
      arr[Q]=10;
      print arr[J]
      }'


      the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].



      Also, like in the script above, I don't want to assign values to arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and common value as the other parameter to a function that handles the logic of assigning them value.










      share|improve this question









      New contributor




      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      #!/usr/bin/env bash
      awk '
      BEGIN {
      arr[A]=1;
      arr[B]=1;
      arr[C]=1;
      arr[E]=1;
      arr[J]=8;
      arr[Q]=10;
      print arr[J]
      }'


      the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].



      Also, like in the script above, I don't want to assign values to arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and common value as the other parameter to a function that handles the logic of assigning them value.







      awk array






      share|improve this question









      New contributor




      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 5 hours ago





















      New contributor




      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      HarshvardhanSharma

      556




      556




      New contributor




      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      HarshvardhanSharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          Array indexes are either integers or strings in awk. What you are doing here are using variables that have not yet been initialised. Their values are therefore empty.



          You get the latest value assigned to the array because each assignment is overwriting the previous value. Using print arr[""] would also give you 10 back.



          Instead, use strings, as in arr["A"]=1.



          For your last issue: There is no real facility for initialising an awk array from the command line, but you may pass an "encoded" value that you "decode" in your BEGIN block (for example) to extract the keys and values for an array.



          Example which passes a specially delimited list as a single string and parses it to extract the indexes and values to use:



          awk -v vals="A=1:B=1:C=1:E=1:J=8:Q=10" '
          BEGIN {
          n = split(vals, v, ":")
          for (i = 1; i <= n; ++i) {
          split(v[i], a, "=")
          arr[a[1]] = a[2]
          }

          print arr["J"]
          }'


          Using separate keys and values:



          awk -v keys="A:B:C:E:J:Q" -v vals="1:1:1:1:8:10" '
          BEGIN {
          nk = split(keys, k, ":")
          nv = split(vals, v, ":")

          if (nk != nv) exit 1

          for (i = 1; i <= nk; ++i)
          arr[k[i]] = v[i]

          print arr["J"]
          }'


          This is quite a limited way of passing an "array" into awk, but it works for simple values that one has complete control over. The examples would break for any data that embeds colons (and equal signs for the 1st example) in the actual data.



          Passing data like this also means backslashes in the data will have to be treated specially (n will be a newline, so to pass the two character string n, you would have to use "\n" or '\n').



          Also related:




          • Set awk array on command line?




          As an aside, you can write a "pure awk script" like this:



          #!/usr/bin/awk -f

          BEGIN {
          # some initialisations
          }

          some_expression { some code }

          END {
          # more here
          }





          share|improve this answer























            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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            HarshvardhanSharma is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487654%2fawk-referring-to-an-array-element%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            10
            down vote



            accepted










            Array indexes are either integers or strings in awk. What you are doing here are using variables that have not yet been initialised. Their values are therefore empty.



            You get the latest value assigned to the array because each assignment is overwriting the previous value. Using print arr[""] would also give you 10 back.



            Instead, use strings, as in arr["A"]=1.



            For your last issue: There is no real facility for initialising an awk array from the command line, but you may pass an "encoded" value that you "decode" in your BEGIN block (for example) to extract the keys and values for an array.



            Example which passes a specially delimited list as a single string and parses it to extract the indexes and values to use:



            awk -v vals="A=1:B=1:C=1:E=1:J=8:Q=10" '
            BEGIN {
            n = split(vals, v, ":")
            for (i = 1; i <= n; ++i) {
            split(v[i], a, "=")
            arr[a[1]] = a[2]
            }

            print arr["J"]
            }'


            Using separate keys and values:



            awk -v keys="A:B:C:E:J:Q" -v vals="1:1:1:1:8:10" '
            BEGIN {
            nk = split(keys, k, ":")
            nv = split(vals, v, ":")

            if (nk != nv) exit 1

            for (i = 1; i <= nk; ++i)
            arr[k[i]] = v[i]

            print arr["J"]
            }'


            This is quite a limited way of passing an "array" into awk, but it works for simple values that one has complete control over. The examples would break for any data that embeds colons (and equal signs for the 1st example) in the actual data.



            Passing data like this also means backslashes in the data will have to be treated specially (n will be a newline, so to pass the two character string n, you would have to use "\n" or '\n').



            Also related:




            • Set awk array on command line?




            As an aside, you can write a "pure awk script" like this:



            #!/usr/bin/awk -f

            BEGIN {
            # some initialisations
            }

            some_expression { some code }

            END {
            # more here
            }





            share|improve this answer



























              up vote
              10
              down vote



              accepted










              Array indexes are either integers or strings in awk. What you are doing here are using variables that have not yet been initialised. Their values are therefore empty.



              You get the latest value assigned to the array because each assignment is overwriting the previous value. Using print arr[""] would also give you 10 back.



              Instead, use strings, as in arr["A"]=1.



              For your last issue: There is no real facility for initialising an awk array from the command line, but you may pass an "encoded" value that you "decode" in your BEGIN block (for example) to extract the keys and values for an array.



              Example which passes a specially delimited list as a single string and parses it to extract the indexes and values to use:



              awk -v vals="A=1:B=1:C=1:E=1:J=8:Q=10" '
              BEGIN {
              n = split(vals, v, ":")
              for (i = 1; i <= n; ++i) {
              split(v[i], a, "=")
              arr[a[1]] = a[2]
              }

              print arr["J"]
              }'


              Using separate keys and values:



              awk -v keys="A:B:C:E:J:Q" -v vals="1:1:1:1:8:10" '
              BEGIN {
              nk = split(keys, k, ":")
              nv = split(vals, v, ":")

              if (nk != nv) exit 1

              for (i = 1; i <= nk; ++i)
              arr[k[i]] = v[i]

              print arr["J"]
              }'


              This is quite a limited way of passing an "array" into awk, but it works for simple values that one has complete control over. The examples would break for any data that embeds colons (and equal signs for the 1st example) in the actual data.



              Passing data like this also means backslashes in the data will have to be treated specially (n will be a newline, so to pass the two character string n, you would have to use "\n" or '\n').



              Also related:




              • Set awk array on command line?




              As an aside, you can write a "pure awk script" like this:



              #!/usr/bin/awk -f

              BEGIN {
              # some initialisations
              }

              some_expression { some code }

              END {
              # more here
              }





              share|improve this answer

























                up vote
                10
                down vote



                accepted







                up vote
                10
                down vote



                accepted






                Array indexes are either integers or strings in awk. What you are doing here are using variables that have not yet been initialised. Their values are therefore empty.



                You get the latest value assigned to the array because each assignment is overwriting the previous value. Using print arr[""] would also give you 10 back.



                Instead, use strings, as in arr["A"]=1.



                For your last issue: There is no real facility for initialising an awk array from the command line, but you may pass an "encoded" value that you "decode" in your BEGIN block (for example) to extract the keys and values for an array.



                Example which passes a specially delimited list as a single string and parses it to extract the indexes and values to use:



                awk -v vals="A=1:B=1:C=1:E=1:J=8:Q=10" '
                BEGIN {
                n = split(vals, v, ":")
                for (i = 1; i <= n; ++i) {
                split(v[i], a, "=")
                arr[a[1]] = a[2]
                }

                print arr["J"]
                }'


                Using separate keys and values:



                awk -v keys="A:B:C:E:J:Q" -v vals="1:1:1:1:8:10" '
                BEGIN {
                nk = split(keys, k, ":")
                nv = split(vals, v, ":")

                if (nk != nv) exit 1

                for (i = 1; i <= nk; ++i)
                arr[k[i]] = v[i]

                print arr["J"]
                }'


                This is quite a limited way of passing an "array" into awk, but it works for simple values that one has complete control over. The examples would break for any data that embeds colons (and equal signs for the 1st example) in the actual data.



                Passing data like this also means backslashes in the data will have to be treated specially (n will be a newline, so to pass the two character string n, you would have to use "\n" or '\n').



                Also related:




                • Set awk array on command line?




                As an aside, you can write a "pure awk script" like this:



                #!/usr/bin/awk -f

                BEGIN {
                # some initialisations
                }

                some_expression { some code }

                END {
                # more here
                }





                share|improve this answer














                Array indexes are either integers or strings in awk. What you are doing here are using variables that have not yet been initialised. Their values are therefore empty.



                You get the latest value assigned to the array because each assignment is overwriting the previous value. Using print arr[""] would also give you 10 back.



                Instead, use strings, as in arr["A"]=1.



                For your last issue: There is no real facility for initialising an awk array from the command line, but you may pass an "encoded" value that you "decode" in your BEGIN block (for example) to extract the keys and values for an array.



                Example which passes a specially delimited list as a single string and parses it to extract the indexes and values to use:



                awk -v vals="A=1:B=1:C=1:E=1:J=8:Q=10" '
                BEGIN {
                n = split(vals, v, ":")
                for (i = 1; i <= n; ++i) {
                split(v[i], a, "=")
                arr[a[1]] = a[2]
                }

                print arr["J"]
                }'


                Using separate keys and values:



                awk -v keys="A:B:C:E:J:Q" -v vals="1:1:1:1:8:10" '
                BEGIN {
                nk = split(keys, k, ":")
                nv = split(vals, v, ":")

                if (nk != nv) exit 1

                for (i = 1; i <= nk; ++i)
                arr[k[i]] = v[i]

                print arr["J"]
                }'


                This is quite a limited way of passing an "array" into awk, but it works for simple values that one has complete control over. The examples would break for any data that embeds colons (and equal signs for the 1st example) in the actual data.



                Passing data like this also means backslashes in the data will have to be treated specially (n will be a newline, so to pass the two character string n, you would have to use "\n" or '\n').



                Also related:




                • Set awk array on command line?




                As an aside, you can write a "pure awk script" like this:



                #!/usr/bin/awk -f

                BEGIN {
                # some initialisations
                }

                some_expression { some code }

                END {
                # more here
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 4 hours ago

























                answered 5 hours ago









                Kusalananda

                119k16225367




                119k16225367






















                    HarshvardhanSharma is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    HarshvardhanSharma is a new contributor. Be nice, and check out our Code of Conduct.













                    HarshvardhanSharma is a new contributor. Be nice, and check out our Code of Conduct.












                    HarshvardhanSharma is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487654%2fawk-referring-to-an-array-element%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu