Referencing array elements by strings, and initialising arrays in awk












8















#!/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





























    8















    #!/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



























      8












      8








      8








      #!/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
















      #!/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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 28 '18 at 7:42









      Kusalananda

      132k17252416




      132k17252416










      asked Dec 12 '18 at 21:14









      HarshvardhanSharmaHarshvardhanSharma

      846




      846






















          1 Answer
          1






          active

          oldest

          votes


















          16














          Array indexes are either integers or quoted 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',
            autoActivateHeartbeat: false,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487654%2freferencing-array-elements-by-strings-and-initialising-arrays-in-awk%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









            16














            Array indexes are either integers or quoted 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






























              16














              Array indexes are either integers or quoted 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




























                16












                16








                16







                Array indexes are either integers or quoted 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 quoted 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 Dec 13 '18 at 8:43

























                answered Dec 12 '18 at 21:20









                KusalanandaKusalananda

                132k17252416




                132k17252416






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487654%2freferencing-array-elements-by-strings-and-initialising-arrays-in-awk%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

                    Bundesstraße 106

                    Verónica Boquete

                    Ida-Boy-Ed-Garten