Synchronized combination of for loops' output











up vote
1
down vote

favorite
1












I have two for loops:



for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d


I want the output be:



0
a
1
b
2
c
3
d


How to do it?










share|improve this question






















  • Looks like a nested loop is required.
    – Graham
    Nov 18 at 14:05










  • Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
    – Josef Klimuk
    Nov 18 at 14:12










  • Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
    – wjandrea
    Nov 18 at 14:18










  • @wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
    – Josef Klimuk
    Nov 18 at 14:34















up vote
1
down vote

favorite
1












I have two for loops:



for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d


I want the output be:



0
a
1
b
2
c
3
d


How to do it?










share|improve this question






















  • Looks like a nested loop is required.
    – Graham
    Nov 18 at 14:05










  • Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
    – Josef Klimuk
    Nov 18 at 14:12










  • Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
    – wjandrea
    Nov 18 at 14:18










  • @wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
    – Josef Klimuk
    Nov 18 at 14:34













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have two for loops:



for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d


I want the output be:



0
a
1
b
2
c
3
d


How to do it?










share|improve this question













I have two for loops:



for i in 0 1 2 3; do echo $i;done
for j in a b c d; do echo $j;done
0
1
2
3
a
b
c
d


I want the output be:



0
a
1
b
2
c
3
d


How to do it?







command-line bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 13:52









Josef Klimuk

551114




551114












  • Looks like a nested loop is required.
    – Graham
    Nov 18 at 14:05










  • Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
    – Josef Klimuk
    Nov 18 at 14:12










  • Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
    – wjandrea
    Nov 18 at 14:18










  • @wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
    – Josef Klimuk
    Nov 18 at 14:34


















  • Looks like a nested loop is required.
    – Graham
    Nov 18 at 14:05










  • Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
    – Josef Klimuk
    Nov 18 at 14:12










  • Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
    – wjandrea
    Nov 18 at 14:18










  • @wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
    – Josef Klimuk
    Nov 18 at 14:34
















Looks like a nested loop is required.
– Graham
Nov 18 at 14:05




Looks like a nested loop is required.
– Graham
Nov 18 at 14:05












Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12




Looks that with nested loop I will get 0 a b c d; 1 a b c d etc. 16 times
– Josef Klimuk
Nov 18 at 14:12












Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
– wjandrea
Nov 18 at 14:18




Do you need it to be more complex than just for i in 0 a 1 b 2 c 3 d ?
– wjandrea
Nov 18 at 14:18












@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34




@wjandrea, perlduck already pointed on it. Right, it is my fault, I need it to be more complex, since I want to use outputs of different loops, not just echoing the range...
– Josef Klimuk
Nov 18 at 14:34










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:



arr1=(0 1 2 3)
arr2=(a b c d)

for i in ${!arr1[@]}; do
echo ${arr1[i]}
echo ${arr2[i]}
done

# Output below:
0
a
1
b
2
c
3
d


Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.



The for-loop iterates over ${!arr1[@]}, which returns a list of all ([@]) indexes (!) of the array variable arr1. This can be used here because we assume that both arr1 and arr2 have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.



Accessing a specific element value of an array at the position defined by the value of the variable $i works by typing ${arr1[i]}.






share|improve this answer




























    up vote
    3
    down vote













    Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:



    #!/usr/bin/env bash

    declare -A items=(
    [0]=a
    [1]=b
    [2]=c
    [3]=d
    [four]=eeh
    );

    for key in "${!items[@]}"; do
    echo "key : $key";
    echo "value: ${items[$key]}";
    done


    Output:



    key  : 0
    value: a
    key : 1
    value: b
    key : 2
    value: c
    key : 3
    value: d
    key : four
    value: eeh





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2faskubuntu.com%2fquestions%2f1093961%2fsynchronized-combination-of-for-loops-output%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote



      accepted










      You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:



      arr1=(0 1 2 3)
      arr2=(a b c d)

      for i in ${!arr1[@]}; do
      echo ${arr1[i]}
      echo ${arr2[i]}
      done

      # Output below:
      0
      a
      1
      b
      2
      c
      3
      d


      Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.



      The for-loop iterates over ${!arr1[@]}, which returns a list of all ([@]) indexes (!) of the array variable arr1. This can be used here because we assume that both arr1 and arr2 have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.



      Accessing a specific element value of an array at the position defined by the value of the variable $i works by typing ${arr1[i]}.






      share|improve this answer

























        up vote
        5
        down vote



        accepted










        You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:



        arr1=(0 1 2 3)
        arr2=(a b c d)

        for i in ${!arr1[@]}; do
        echo ${arr1[i]}
        echo ${arr2[i]}
        done

        # Output below:
        0
        a
        1
        b
        2
        c
        3
        d


        Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.



        The for-loop iterates over ${!arr1[@]}, which returns a list of all ([@]) indexes (!) of the array variable arr1. This can be used here because we assume that both arr1 and arr2 have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.



        Accessing a specific element value of an array at the position defined by the value of the variable $i works by typing ${arr1[i]}.






        share|improve this answer























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:



          arr1=(0 1 2 3)
          arr2=(a b c d)

          for i in ${!arr1[@]}; do
          echo ${arr1[i]}
          echo ${arr2[i]}
          done

          # Output below:
          0
          a
          1
          b
          2
          c
          3
          d


          Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.



          The for-loop iterates over ${!arr1[@]}, which returns a list of all ([@]) indexes (!) of the array variable arr1. This can be used here because we assume that both arr1 and arr2 have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.



          Accessing a specific element value of an array at the position defined by the value of the variable $i works by typing ${arr1[i]}.






          share|improve this answer












          You could define two arrays of the same length with the values you want to iterate over first. Then you can iterate over the indexes of one of the arrays and look them up in both:



          arr1=(0 1 2 3)
          arr2=(a b c d)

          for i in ${!arr1[@]}; do
          echo ${arr1[i]}
          echo ${arr2[i]}
          done

          # Output below:
          0
          a
          1
          b
          2
          c
          3
          d


          Defining arrays works by assigning a list of elements in parentheses, as you see on the first two lines.



          The for-loop iterates over ${!arr1[@]}, which returns a list of all ([@]) indexes (!) of the array variable arr1. This can be used here because we assume that both arr1 and arr2 have exactly the same indexes, because they were defined with the same number of elements. In theory, arrays could have different lengths or even have "holes" i.e. unassigned indexes in between, but both is not the case here, so we don't care for it.



          Accessing a specific element value of an array at the position defined by the value of the variable $i works by typing ${arr1[i]}.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 14:12









          Byte Commander

          62.1k26167279




          62.1k26167279
























              up vote
              3
              down vote













              Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:



              #!/usr/bin/env bash

              declare -A items=(
              [0]=a
              [1]=b
              [2]=c
              [3]=d
              [four]=eeh
              );

              for key in "${!items[@]}"; do
              echo "key : $key";
              echo "value: ${items[$key]}";
              done


              Output:



              key  : 0
              value: a
              key : 1
              value: b
              key : 2
              value: c
              key : 3
              value: d
              key : four
              value: eeh





              share|improve this answer

























                up vote
                3
                down vote













                Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:



                #!/usr/bin/env bash

                declare -A items=(
                [0]=a
                [1]=b
                [2]=c
                [3]=d
                [four]=eeh
                );

                for key in "${!items[@]}"; do
                echo "key : $key";
                echo "value: ${items[$key]}";
                done


                Output:



                key  : 0
                value: a
                key : 1
                value: b
                key : 2
                value: c
                key : 3
                value: d
                key : four
                value: eeh





                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:



                  #!/usr/bin/env bash

                  declare -A items=(
                  [0]=a
                  [1]=b
                  [2]=c
                  [3]=d
                  [four]=eeh
                  );

                  for key in "${!items[@]}"; do
                  echo "key : $key";
                  echo "value: ${items[$key]}";
                  done


                  Output:



                  key  : 0
                  value: a
                  key : 1
                  value: b
                  key : 2
                  value: c
                  key : 3
                  value: d
                  key : four
                  value: eeh





                  share|improve this answer












                  Another possibility would be to use an associative array (aka hash aka dictionary) and then iterate over it and retrieving keys and values respectively:



                  #!/usr/bin/env bash

                  declare -A items=(
                  [0]=a
                  [1]=b
                  [2]=c
                  [3]=d
                  [four]=eeh
                  );

                  for key in "${!items[@]}"; do
                  echo "key : $key";
                  echo "value: ${items[$key]}";
                  done


                  Output:



                  key  : 0
                  value: a
                  key : 1
                  value: b
                  key : 2
                  value: c
                  key : 3
                  value: d
                  key : four
                  value: eeh






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 at 15:03









                  PerlDuck

                  4,83111130




                  4,83111130






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093961%2fsynchronized-combination-of-for-loops-output%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

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?