Unix Script, problem with a 2d array

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












0














I have 2 arrays that refer to files for example



alpha=file 1
beta=file2
Charlie=file3
delta=file4
beta2=file5
beta3=file6
Charlie2=file7
charlie3=file8
delta2=file9
delta3=file10

array1=("$alpha" "$beta" "$Charlie" "$delta)
array2=("$beta2" "$beta3" "$Charlie2" "$Charlie3" "$delta1 "$delta3")


Then I have an algorithm that goes through and uses these files together example



for ((i=0;2;i++))
do
for((j=0;2;j++))
do
./$array1[$i].sh $array2[$j].in
done
done


Problem is I would like the array1 and array 2 to correspond such that



it would be for example



./file2.sh file5.in


But I can never get it to do it because of course when j resets for each loop of i. What is the solution for this?










share|improve this question























  • bash is not an ideal language for using 2d arrays. Choose other language for your project.
    – Ipor Sircer
    Dec 12 at 15:51










  • Is there a better way to do this in bash. I have no option other to use bash at the moment.
    – OB1
    Dec 12 at 15:53















0














I have 2 arrays that refer to files for example



alpha=file 1
beta=file2
Charlie=file3
delta=file4
beta2=file5
beta3=file6
Charlie2=file7
charlie3=file8
delta2=file9
delta3=file10

array1=("$alpha" "$beta" "$Charlie" "$delta)
array2=("$beta2" "$beta3" "$Charlie2" "$Charlie3" "$delta1 "$delta3")


Then I have an algorithm that goes through and uses these files together example



for ((i=0;2;i++))
do
for((j=0;2;j++))
do
./$array1[$i].sh $array2[$j].in
done
done


Problem is I would like the array1 and array 2 to correspond such that



it would be for example



./file2.sh file5.in


But I can never get it to do it because of course when j resets for each loop of i. What is the solution for this?










share|improve this question























  • bash is not an ideal language for using 2d arrays. Choose other language for your project.
    – Ipor Sircer
    Dec 12 at 15:51










  • Is there a better way to do this in bash. I have no option other to use bash at the moment.
    – OB1
    Dec 12 at 15:53













0












0








0







I have 2 arrays that refer to files for example



alpha=file 1
beta=file2
Charlie=file3
delta=file4
beta2=file5
beta3=file6
Charlie2=file7
charlie3=file8
delta2=file9
delta3=file10

array1=("$alpha" "$beta" "$Charlie" "$delta)
array2=("$beta2" "$beta3" "$Charlie2" "$Charlie3" "$delta1 "$delta3")


Then I have an algorithm that goes through and uses these files together example



for ((i=0;2;i++))
do
for((j=0;2;j++))
do
./$array1[$i].sh $array2[$j].in
done
done


Problem is I would like the array1 and array 2 to correspond such that



it would be for example



./file2.sh file5.in


But I can never get it to do it because of course when j resets for each loop of i. What is the solution for this?










share|improve this question















I have 2 arrays that refer to files for example



alpha=file 1
beta=file2
Charlie=file3
delta=file4
beta2=file5
beta3=file6
Charlie2=file7
charlie3=file8
delta2=file9
delta3=file10

array1=("$alpha" "$beta" "$Charlie" "$delta)
array2=("$beta2" "$beta3" "$Charlie2" "$Charlie3" "$delta1 "$delta3")


Then I have an algorithm that goes through and uses these files together example



for ((i=0;2;i++))
do
for((j=0;2;j++))
do
./$array1[$i].sh $array2[$j].in
done
done


Problem is I would like the array1 and array 2 to correspond such that



it would be for example



./file2.sh file5.in


But I can never get it to do it because of course when j resets for each loop of i. What is the solution for this?







bash shell-script array for






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 at 15:59









Rui F Ribeiro

38.8k1479128




38.8k1479128










asked Dec 12 at 15:48









OB1

1




1











  • bash is not an ideal language for using 2d arrays. Choose other language for your project.
    – Ipor Sircer
    Dec 12 at 15:51










  • Is there a better way to do this in bash. I have no option other to use bash at the moment.
    – OB1
    Dec 12 at 15:53
















  • bash is not an ideal language for using 2d arrays. Choose other language for your project.
    – Ipor Sircer
    Dec 12 at 15:51










  • Is there a better way to do this in bash. I have no option other to use bash at the moment.
    – OB1
    Dec 12 at 15:53















bash is not an ideal language for using 2d arrays. Choose other language for your project.
– Ipor Sircer
Dec 12 at 15:51




bash is not an ideal language for using 2d arrays. Choose other language for your project.
– Ipor Sircer
Dec 12 at 15:51












Is there a better way to do this in bash. I have no option other to use bash at the moment.
– OB1
Dec 12 at 15:53




Is there a better way to do this in bash. I have no option other to use bash at the moment.
– OB1
Dec 12 at 15:53










2 Answers
2






active

oldest

votes


















3














If you want the 1st item of array1 to correspond to the 1st item of array2, then you don't need nested loops: just use the same index variable



for i in 0..2; do
"./$array1[i].sh" "$array2[i].in"
# ..........^.................^
done


Or, use an associative array:



declare -A map=(
[$alpha]=$beta2
[$beta]=$beta3
[$Charlie]=$Charlie2
[$delta]=$Charlie3
)

for key in "$!map[@]"; do
do_something_with "$key" and "$map[$key]"
done





share|improve this answer






















  • There's no guarantee about the order of the keys in the for loop for an associative array.
    – glenn jackman
    Dec 12 at 16:19











  • I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
    – OB1
    Dec 12 at 16:47


















0














Sounds like you want an array zipping operator, then zsh may be a better option than bash here:



$ a=(A..D) b=(1..10)
$ for i j ($a:^b) echo $i $j
A 1
B 2
C 3
D 4
$ for i j ($a:^^b) echo $i $j
A 1
B 2
C 3
D 4
A 5
B 6
C 7
D 8
A 9
B 10


$a:^b and $a:^^b are two array zipping parameter expansion operators. The difference is seen when one array has fewer elements than the other, in which case the latter will reuse the elements from the shorter one to match the larger one.



Note that leaving a variable unquoted in zsh doesn't have the same nasty side effects as in other Bourne-like shells, but still removes empty elements. So, if your arrays may contain empty elements, you would need to write it:



for i j ("$(@)a:^^b") echo "$i" "$j"





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%2f487603%2funix-script-problem-with-a-2d-array%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









    3














    If you want the 1st item of array1 to correspond to the 1st item of array2, then you don't need nested loops: just use the same index variable



    for i in 0..2; do
    "./$array1[i].sh" "$array2[i].in"
    # ..........^.................^
    done


    Or, use an associative array:



    declare -A map=(
    [$alpha]=$beta2
    [$beta]=$beta3
    [$Charlie]=$Charlie2
    [$delta]=$Charlie3
    )

    for key in "$!map[@]"; do
    do_something_with "$key" and "$map[$key]"
    done





    share|improve this answer






















    • There's no guarantee about the order of the keys in the for loop for an associative array.
      – glenn jackman
      Dec 12 at 16:19











    • I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
      – OB1
      Dec 12 at 16:47















    3














    If you want the 1st item of array1 to correspond to the 1st item of array2, then you don't need nested loops: just use the same index variable



    for i in 0..2; do
    "./$array1[i].sh" "$array2[i].in"
    # ..........^.................^
    done


    Or, use an associative array:



    declare -A map=(
    [$alpha]=$beta2
    [$beta]=$beta3
    [$Charlie]=$Charlie2
    [$delta]=$Charlie3
    )

    for key in "$!map[@]"; do
    do_something_with "$key" and "$map[$key]"
    done





    share|improve this answer






















    • There's no guarantee about the order of the keys in the for loop for an associative array.
      – glenn jackman
      Dec 12 at 16:19











    • I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
      – OB1
      Dec 12 at 16:47













    3












    3








    3






    If you want the 1st item of array1 to correspond to the 1st item of array2, then you don't need nested loops: just use the same index variable



    for i in 0..2; do
    "./$array1[i].sh" "$array2[i].in"
    # ..........^.................^
    done


    Or, use an associative array:



    declare -A map=(
    [$alpha]=$beta2
    [$beta]=$beta3
    [$Charlie]=$Charlie2
    [$delta]=$Charlie3
    )

    for key in "$!map[@]"; do
    do_something_with "$key" and "$map[$key]"
    done





    share|improve this answer














    If you want the 1st item of array1 to correspond to the 1st item of array2, then you don't need nested loops: just use the same index variable



    for i in 0..2; do
    "./$array1[i].sh" "$array2[i].in"
    # ..........^.................^
    done


    Or, use an associative array:



    declare -A map=(
    [$alpha]=$beta2
    [$beta]=$beta3
    [$Charlie]=$Charlie2
    [$delta]=$Charlie3
    )

    for key in "$!map[@]"; do
    do_something_with "$key" and "$map[$key]"
    done






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 12 at 18:00









    Stéphane Chazelas

    298k54563910




    298k54563910










    answered Dec 12 at 16:14









    glenn jackman

    50.1k569106




    50.1k569106











    • There's no guarantee about the order of the keys in the for loop for an associative array.
      – glenn jackman
      Dec 12 at 16:19











    • I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
      – OB1
      Dec 12 at 16:47
















    • There's no guarantee about the order of the keys in the for loop for an associative array.
      – glenn jackman
      Dec 12 at 16:19











    • I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
      – OB1
      Dec 12 at 16:47















    There's no guarantee about the order of the keys in the for loop for an associative array.
    – glenn jackman
    Dec 12 at 16:19





    There's no guarantee about the order of the keys in the for loop for an associative array.
    – glenn jackman
    Dec 12 at 16:19













    I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
    – OB1
    Dec 12 at 16:47




    I think I have got round the issue by getting rid of the nested loops and made array 1 same size as array 2 by repeating the declarations for each element of array 2. This seems to work!
    – OB1
    Dec 12 at 16:47













    0














    Sounds like you want an array zipping operator, then zsh may be a better option than bash here:



    $ a=(A..D) b=(1..10)
    $ for i j ($a:^b) echo $i $j
    A 1
    B 2
    C 3
    D 4
    $ for i j ($a:^^b) echo $i $j
    A 1
    B 2
    C 3
    D 4
    A 5
    B 6
    C 7
    D 8
    A 9
    B 10


    $a:^b and $a:^^b are two array zipping parameter expansion operators. The difference is seen when one array has fewer elements than the other, in which case the latter will reuse the elements from the shorter one to match the larger one.



    Note that leaving a variable unquoted in zsh doesn't have the same nasty side effects as in other Bourne-like shells, but still removes empty elements. So, if your arrays may contain empty elements, you would need to write it:



    for i j ("$(@)a:^^b") echo "$i" "$j"





    share|improve this answer



























      0














      Sounds like you want an array zipping operator, then zsh may be a better option than bash here:



      $ a=(A..D) b=(1..10)
      $ for i j ($a:^b) echo $i $j
      A 1
      B 2
      C 3
      D 4
      $ for i j ($a:^^b) echo $i $j
      A 1
      B 2
      C 3
      D 4
      A 5
      B 6
      C 7
      D 8
      A 9
      B 10


      $a:^b and $a:^^b are two array zipping parameter expansion operators. The difference is seen when one array has fewer elements than the other, in which case the latter will reuse the elements from the shorter one to match the larger one.



      Note that leaving a variable unquoted in zsh doesn't have the same nasty side effects as in other Bourne-like shells, but still removes empty elements. So, if your arrays may contain empty elements, you would need to write it:



      for i j ("$(@)a:^^b") echo "$i" "$j"





      share|improve this answer

























        0












        0








        0






        Sounds like you want an array zipping operator, then zsh may be a better option than bash here:



        $ a=(A..D) b=(1..10)
        $ for i j ($a:^b) echo $i $j
        A 1
        B 2
        C 3
        D 4
        $ for i j ($a:^^b) echo $i $j
        A 1
        B 2
        C 3
        D 4
        A 5
        B 6
        C 7
        D 8
        A 9
        B 10


        $a:^b and $a:^^b are two array zipping parameter expansion operators. The difference is seen when one array has fewer elements than the other, in which case the latter will reuse the elements from the shorter one to match the larger one.



        Note that leaving a variable unquoted in zsh doesn't have the same nasty side effects as in other Bourne-like shells, but still removes empty elements. So, if your arrays may contain empty elements, you would need to write it:



        for i j ("$(@)a:^^b") echo "$i" "$j"





        share|improve this answer














        Sounds like you want an array zipping operator, then zsh may be a better option than bash here:



        $ a=(A..D) b=(1..10)
        $ for i j ($a:^b) echo $i $j
        A 1
        B 2
        C 3
        D 4
        $ for i j ($a:^^b) echo $i $j
        A 1
        B 2
        C 3
        D 4
        A 5
        B 6
        C 7
        D 8
        A 9
        B 10


        $a:^b and $a:^^b are two array zipping parameter expansion operators. The difference is seen when one array has fewer elements than the other, in which case the latter will reuse the elements from the shorter one to match the larger one.



        Note that leaving a variable unquoted in zsh doesn't have the same nasty side effects as in other Bourne-like shells, but still removes empty elements. So, if your arrays may contain empty elements, you would need to write it:



        for i j ("$(@)a:^^b") echo "$i" "$j"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 12 at 17:54

























        answered Dec 12 at 17:47









        Stéphane Chazelas

        298k54563910




        298k54563910



























            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.





            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%2f487603%2funix-script-problem-with-a-2d-array%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay