how to combine 2 arrays into one associative arrayError when constructing an associative arrayCombine Bash...
Why is it called a stateful and a stateless firewall?
Answer Not A Fool, or Answer A Fool?
Can Brexit be undone in an emergency?
How to make classical firearms effective on space habitats despite the coriolis effect?
How would you control supersoldiers in a late iron-age society?
How to publish superseding results without creating enemies
Can I travel to European countries with the Irish passport and without destination Visa?
Wrong Schengen Visa exit stamp on my passport, who can I complain to?
What are the typical trumpet parts in classical music?
Are there objective criteria for classifying consonance v. dissonance?
Permutations in Disguise
How clean are pets?
What 68-pin connector is this on my 2.5" solid state drive?
Why does an orbit become hyperbolic when total orbital energy is positive?
Help with wheel lock
An ES6 array of numbers - Double last number, delete the first number
What's the benefit of prohibiting the use of techniques/language constructs that have not been taught?
Can a business put whatever they want into a contract?
What would happen if Protagoras v Euathlus were heard in court today?
Asked to Not Use Transactions and to Use A Workaround to Simulate One
Assign every word from a line to a variable
Does a large scratch in an ND filter affect image quality?
Statistical tests for benchmark comparison
In what state are satellites left in when they are left in a graveyard orbit?
how to combine 2 arrays into one associative array
Error when constructing an associative arrayCombine Bash associative arraysHow to choose the highest number element in many files with many arraysInverting an associative arrayHow to pipe multiple results into a command?Merge duplicate keys in associative array BASH
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
i need to combine ARRAY1
and ARRAY2
into an associative array like ARRAY
. i'm using this code:
mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch)
mapfile -t ARRAY2 < <(/bin/awk '{ print $ 6 }' /output/gen_code )
declare -A ARRAY
for ((i=0; $i<${#ARRAY1[@]}; i++))
do
ARRAY+=( ["${ARRAY1[i]}"] = "${ARRAY2[i]}" )
done
##added this loop to check output
for value in "${!ARRAY[@]}"
do
echo "branch: $value"
echo "code: ${ARRAY[$value]}"
done
i expect something like this to use them in other parts of bash (it is important that the value of first element of ARRAY1
be the first element of ARRAY2
and so on) :
ARRAY1=( b1 b2 b3 )
ARRAY2=( c1 c2 c3 )
ARRAY= ( [b1]=c1 [b2]=c2 [b3]=c3 )
but when i run my code i get this error:
line 7: ARRAY: [b1]: must use subscript when assigning associative array
line 7: ARRAY: =: must use subscript when assigning associative array
line 7: ARRAY: c1: must use subscript when assigning associative array
(and it goes on like this for every entry)
i think i'm doing it all wrong on line 7.what should i do to fix this?
bash shell-script array merge associative-array
add a comment
|
i need to combine ARRAY1
and ARRAY2
into an associative array like ARRAY
. i'm using this code:
mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch)
mapfile -t ARRAY2 < <(/bin/awk '{ print $ 6 }' /output/gen_code )
declare -A ARRAY
for ((i=0; $i<${#ARRAY1[@]}; i++))
do
ARRAY+=( ["${ARRAY1[i]}"] = "${ARRAY2[i]}" )
done
##added this loop to check output
for value in "${!ARRAY[@]}"
do
echo "branch: $value"
echo "code: ${ARRAY[$value]}"
done
i expect something like this to use them in other parts of bash (it is important that the value of first element of ARRAY1
be the first element of ARRAY2
and so on) :
ARRAY1=( b1 b2 b3 )
ARRAY2=( c1 c2 c3 )
ARRAY= ( [b1]=c1 [b2]=c2 [b3]=c3 )
but when i run my code i get this error:
line 7: ARRAY: [b1]: must use subscript when assigning associative array
line 7: ARRAY: =: must use subscript when assigning associative array
line 7: ARRAY: c1: must use subscript when assigning associative array
(and it goes on like this for every entry)
i think i'm doing it all wrong on line 7.what should i do to fix this?
bash shell-script array merge associative-array
Remove spaces around=
– muru
37 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago
add a comment
|
i need to combine ARRAY1
and ARRAY2
into an associative array like ARRAY
. i'm using this code:
mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch)
mapfile -t ARRAY2 < <(/bin/awk '{ print $ 6 }' /output/gen_code )
declare -A ARRAY
for ((i=0; $i<${#ARRAY1[@]}; i++))
do
ARRAY+=( ["${ARRAY1[i]}"] = "${ARRAY2[i]}" )
done
##added this loop to check output
for value in "${!ARRAY[@]}"
do
echo "branch: $value"
echo "code: ${ARRAY[$value]}"
done
i expect something like this to use them in other parts of bash (it is important that the value of first element of ARRAY1
be the first element of ARRAY2
and so on) :
ARRAY1=( b1 b2 b3 )
ARRAY2=( c1 c2 c3 )
ARRAY= ( [b1]=c1 [b2]=c2 [b3]=c3 )
but when i run my code i get this error:
line 7: ARRAY: [b1]: must use subscript when assigning associative array
line 7: ARRAY: =: must use subscript when assigning associative array
line 7: ARRAY: c1: must use subscript when assigning associative array
(and it goes on like this for every entry)
i think i'm doing it all wrong on line 7.what should i do to fix this?
bash shell-script array merge associative-array
i need to combine ARRAY1
and ARRAY2
into an associative array like ARRAY
. i'm using this code:
mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch)
mapfile -t ARRAY2 < <(/bin/awk '{ print $ 6 }' /output/gen_code )
declare -A ARRAY
for ((i=0; $i<${#ARRAY1[@]}; i++))
do
ARRAY+=( ["${ARRAY1[i]}"] = "${ARRAY2[i]}" )
done
##added this loop to check output
for value in "${!ARRAY[@]}"
do
echo "branch: $value"
echo "code: ${ARRAY[$value]}"
done
i expect something like this to use them in other parts of bash (it is important that the value of first element of ARRAY1
be the first element of ARRAY2
and so on) :
ARRAY1=( b1 b2 b3 )
ARRAY2=( c1 c2 c3 )
ARRAY= ( [b1]=c1 [b2]=c2 [b3]=c3 )
but when i run my code i get this error:
line 7: ARRAY: [b1]: must use subscript when assigning associative array
line 7: ARRAY: =: must use subscript when assigning associative array
line 7: ARRAY: c1: must use subscript when assigning associative array
(and it goes on like this for every entry)
i think i'm doing it all wrong on line 7.what should i do to fix this?
bash shell-script array merge associative-array
bash shell-script array merge associative-array
asked 57 mins ago
BlackCrystalBlackCrystal
3851 silver badge15 bronze badges
3851 silver badge15 bronze badges
Remove spaces around=
– muru
37 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago
add a comment
|
Remove spaces around=
– muru
37 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago
Remove spaces around
=
– muru
37 mins ago
Remove spaces around
=
– muru
37 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago
add a comment
|
0
active
oldest
votes
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/4.0/"u003ecc by-sa 4.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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f541813%2fhow-to-combine-2-arrays-into-one-associative-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f541813%2fhow-to-combine-2-arrays-into-one-associative-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Remove spaces around
=
– muru
37 mins ago
@muru it worked. would you like to turn it into an answer so i can accept it?
– BlackCrystal
34 mins ago