Return the index of an element which matches a value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have an array -



val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)


I am trying to find the index of all the elements whose values are equal to 0



How can i achienve this?



This is what i have tried -



for ((i = 1; i <= 10; i++)); do
if [ "$i" -eq "0" ]; then
echo "Index: $i, value: $val[i]"
fi
done


The output should be



Index: 3, value: 0
Index: 9, value: 0









share|improve this question

























    up vote
    0
    down vote

    favorite












    I have an array -



    val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)


    I am trying to find the index of all the elements whose values are equal to 0



    How can i achienve this?



    This is what i have tried -



    for ((i = 1; i <= 10; i++)); do
    if [ "$i" -eq "0" ]; then
    echo "Index: $i, value: $val[i]"
    fi
    done


    The output should be



    Index: 3, value: 0
    Index: 9, value: 0









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an array -



      val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)


      I am trying to find the index of all the elements whose values are equal to 0



      How can i achienve this?



      This is what i have tried -



      for ((i = 1; i <= 10; i++)); do
      if [ "$i" -eq "0" ]; then
      echo "Index: $i, value: $val[i]"
      fi
      done


      The output should be



      Index: 3, value: 0
      Index: 9, value: 0









      share|improve this question













      I have an array -



      val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)


      I am trying to find the index of all the elements whose values are equal to 0



      How can i achienve this?



      This is what i have tried -



      for ((i = 1; i <= 10; i++)); do
      if [ "$i" -eq "0" ]; then
      echo "Index: $i, value: $val[i]"
      fi
      done


      The output should be



      Index: 3, value: 0
      Index: 9, value: 0






      linux bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 22 hours ago









      PepeHands

      51




      51




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          #!/bin/bash
          val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
          n=0
          for i in $val[@]; do
          [ $i -eq 0 ] && echo Index: $n, value: $i
          ((n++))
          done





          share|improve this answer



























            up vote
            2
            down vote













            If the array has gaps in it, it might be better to loop over the indices of the array instead:



            for i in "$!val[@]"
            do
            if [[ $val[i] -eq 0 ]]
            then
            echo "Index: $i, value: $val[i]"
            fi
            done


            So, if your array was like:



            val=([100]=327823 [54]=0 [787998]=377463287)


            You'd still get 54 as the index.



            This should also work for associative arrays (strings as indices instead of integers).






            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
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481375%2freturn-the-index-of-an-element-which-matches-a-value%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              #!/bin/bash
              val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
              n=0
              for i in $val[@]; do
              [ $i -eq 0 ] && echo Index: $n, value: $i
              ((n++))
              done





              share|improve this answer
























                up vote
                0
                down vote



                accepted










                #!/bin/bash
                val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
                n=0
                for i in $val[@]; do
                [ $i -eq 0 ] && echo Index: $n, value: $i
                ((n++))
                done





                share|improve this answer






















                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  #!/bin/bash
                  val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
                  n=0
                  for i in $val[@]; do
                  [ $i -eq 0 ] && echo Index: $n, value: $i
                  ((n++))
                  done





                  share|improve this answer












                  #!/bin/bash
                  val=(4196976 4601313 4242010 0 3581283 2392831 3176852 3205880 3794451 0 3627872)
                  n=0
                  for i in $val[@]; do
                  [ $i -eq 0 ] && echo Index: $n, value: $i
                  ((n++))
                  done






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 21 hours ago









                  Ipor Sircer

                  9,97211023




                  9,97211023






















                      up vote
                      2
                      down vote













                      If the array has gaps in it, it might be better to loop over the indices of the array instead:



                      for i in "$!val[@]"
                      do
                      if [[ $val[i] -eq 0 ]]
                      then
                      echo "Index: $i, value: $val[i]"
                      fi
                      done


                      So, if your array was like:



                      val=([100]=327823 [54]=0 [787998]=377463287)


                      You'd still get 54 as the index.



                      This should also work for associative arrays (strings as indices instead of integers).






                      share|improve this answer
























                        up vote
                        2
                        down vote













                        If the array has gaps in it, it might be better to loop over the indices of the array instead:



                        for i in "$!val[@]"
                        do
                        if [[ $val[i] -eq 0 ]]
                        then
                        echo "Index: $i, value: $val[i]"
                        fi
                        done


                        So, if your array was like:



                        val=([100]=327823 [54]=0 [787998]=377463287)


                        You'd still get 54 as the index.



                        This should also work for associative arrays (strings as indices instead of integers).






                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          If the array has gaps in it, it might be better to loop over the indices of the array instead:



                          for i in "$!val[@]"
                          do
                          if [[ $val[i] -eq 0 ]]
                          then
                          echo "Index: $i, value: $val[i]"
                          fi
                          done


                          So, if your array was like:



                          val=([100]=327823 [54]=0 [787998]=377463287)


                          You'd still get 54 as the index.



                          This should also work for associative arrays (strings as indices instead of integers).






                          share|improve this answer












                          If the array has gaps in it, it might be better to loop over the indices of the array instead:



                          for i in "$!val[@]"
                          do
                          if [[ $val[i] -eq 0 ]]
                          then
                          echo "Index: $i, value: $val[i]"
                          fi
                          done


                          So, if your array was like:



                          val=([100]=327823 [54]=0 [787998]=377463287)


                          You'd still get 54 as the index.



                          This should also work for associative arrays (strings as indices instead of integers).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 21 hours ago









                          Olorin

                          1,479112




                          1,479112



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481375%2freturn-the-index-of-an-element-which-matches-a-value%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?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?