How to use collect and include for multidimensional array
I have:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
I want to replace elements in array1
that are included in @student_ids
with 'X'
. I want to see:
[['X','X','X',4,5],[7,8,9,10],[11,12,13,14]]
I have code that is intended to do this:
array1.collect! do |i|
if i.include?(@student_ids) #
i[i.index(@student_ids)] = 'X'; i # I want to replace all with X
else
i
end
end
If @student_ids
is 1
, then it works, but if @student_ids
has more than one element such as 1,2,3
, it raises errors. Any help?
ruby
add a comment |
I have:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
I want to replace elements in array1
that are included in @student_ids
with 'X'
. I want to see:
[['X','X','X',4,5],[7,8,9,10],[11,12,13,14]]
I have code that is intended to do this:
array1.collect! do |i|
if i.include?(@student_ids) #
i[i.index(@student_ids)] = 'X'; i # I want to replace all with X
else
i
end
end
If @student_ids
is 1
, then it works, but if @student_ids
has more than one element such as 1,2,3
, it raises errors. Any help?
ruby
1
Do you mean your input by,array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Oops, yeah sorry
– Jay
2 days ago
@Jay Using the notation@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation@student_ids = [1,2,3]
. I have already edited to do so.
– sawa
2 days ago
What you mean is, "I want to replace elements of elements inarray1
that are included in@student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!
– Cary Swoveland
2 days ago
add a comment |
I have:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
I want to replace elements in array1
that are included in @student_ids
with 'X'
. I want to see:
[['X','X','X',4,5],[7,8,9,10],[11,12,13,14]]
I have code that is intended to do this:
array1.collect! do |i|
if i.include?(@student_ids) #
i[i.index(@student_ids)] = 'X'; i # I want to replace all with X
else
i
end
end
If @student_ids
is 1
, then it works, but if @student_ids
has more than one element such as 1,2,3
, it raises errors. Any help?
ruby
I have:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
I want to replace elements in array1
that are included in @student_ids
with 'X'
. I want to see:
[['X','X','X',4,5],[7,8,9,10],[11,12,13,14]]
I have code that is intended to do this:
array1.collect! do |i|
if i.include?(@student_ids) #
i[i.index(@student_ids)] = 'X'; i # I want to replace all with X
else
i
end
end
If @student_ids
is 1
, then it works, but if @student_ids
has more than one element such as 1,2,3
, it raises errors. Any help?
ruby
ruby
edited 2 days ago
sawa
129k27197299
129k27197299
asked 2 days ago
Jay
337
337
1
Do you mean your input by,array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Oops, yeah sorry
– Jay
2 days ago
@Jay Using the notation@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation@student_ids = [1,2,3]
. I have already edited to do so.
– sawa
2 days ago
What you mean is, "I want to replace elements of elements inarray1
that are included in@student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!
– Cary Swoveland
2 days ago
add a comment |
1
Do you mean your input by,array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Oops, yeah sorry
– Jay
2 days ago
@Jay Using the notation@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation@student_ids = [1,2,3]
. I have already edited to do so.
– sawa
2 days ago
What you mean is, "I want to replace elements of elements inarray1
that are included in@student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!
– Cary Swoveland
2 days ago
1
1
Do you mean your input by,
array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Do you mean your input by,
array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Oops, yeah sorry
– Jay
2 days ago
Oops, yeah sorry
– Jay
2 days ago
@Jay Using the notation
@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation @student_ids = [1,2,3]
. I have already edited to do so.– sawa
2 days ago
@Jay Using the notation
@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation @student_ids = [1,2,3]
. I have already edited to do so.– sawa
2 days ago
What you mean is, "I want to replace elements of elements in
array1
that are included in @student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!– Cary Swoveland
2 days ago
What you mean is, "I want to replace elements of elements in
array1
that are included in @student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!– Cary Swoveland
2 days ago
add a comment |
4 Answers
4
active
oldest
votes
It's faster to use a hash or a set than to repeatedly test [1,2,3].include?(n)
.
arr = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
ids = [1,2,3]
Use a hash
h = ids.product(["X"]).to_h
#=> {1=>"X", 2=>"X", 3=>"X"}
arr.map { |a| a.map { |n| h.fetch(n, n) } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
See Hash#fetch.
Use a set
require 'set'
ids = ids.to_set
#=> #<Set: {1, 2, 3}>
arr.map { |a| a.map { |n| ids.include?(n) ? "X" : n } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Replace both map
s with map!
if the array is to be modified in place (mutated).
For mutating, it is sufficient to use onemap!
(outereach
, innermap!
or outermap!
, innermap
)
– Marcin Kołodziej
2 days ago
1
@MarcinKołodziej, true, buteach/map!
has the disadvantage that the modified array is not returned andmap!/map
creates unneeded temporary arrays.
– Cary Swoveland
2 days ago
add a comment |
Try following, (taking @student_ids = [1, 2, 3])
array1.inject() { |m,a| m << a.map { |x| @student_ids.include?(x) ? 'X' : x } }
# => [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
I don't see any advantage to usinginject
(akareduce
) instead ofmap
.
– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.tmap
&inject
for above case ?
– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to usemap
. Havingmap
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.
– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
add a comment |
You can use each_with_index
and replace the item you want:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
array1.each_with_index do |sub_array, index|
sub_array.each_with_index do |item, index2|
array1[index][index2] = 'X' if @student_ids.include?(item)
end
end
What is@student_ids = 1,2,3
? Does it execute properly?
– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.
– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
add a comment |
You can do the following:
def remove_student_ids(arr)
arr.each_with_index do |value, index|
arr[index] = 'X' if @student_ids.include?(value) }
end
end
array1.map{ |sub_arr| remove_student_ids(sub_arr)}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: 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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53945277%2fhow-to-use-collect-and-include-for-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's faster to use a hash or a set than to repeatedly test [1,2,3].include?(n)
.
arr = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
ids = [1,2,3]
Use a hash
h = ids.product(["X"]).to_h
#=> {1=>"X", 2=>"X", 3=>"X"}
arr.map { |a| a.map { |n| h.fetch(n, n) } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
See Hash#fetch.
Use a set
require 'set'
ids = ids.to_set
#=> #<Set: {1, 2, 3}>
arr.map { |a| a.map { |n| ids.include?(n) ? "X" : n } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Replace both map
s with map!
if the array is to be modified in place (mutated).
For mutating, it is sufficient to use onemap!
(outereach
, innermap!
or outermap!
, innermap
)
– Marcin Kołodziej
2 days ago
1
@MarcinKołodziej, true, buteach/map!
has the disadvantage that the modified array is not returned andmap!/map
creates unneeded temporary arrays.
– Cary Swoveland
2 days ago
add a comment |
It's faster to use a hash or a set than to repeatedly test [1,2,3].include?(n)
.
arr = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
ids = [1,2,3]
Use a hash
h = ids.product(["X"]).to_h
#=> {1=>"X", 2=>"X", 3=>"X"}
arr.map { |a| a.map { |n| h.fetch(n, n) } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
See Hash#fetch.
Use a set
require 'set'
ids = ids.to_set
#=> #<Set: {1, 2, 3}>
arr.map { |a| a.map { |n| ids.include?(n) ? "X" : n } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Replace both map
s with map!
if the array is to be modified in place (mutated).
For mutating, it is sufficient to use onemap!
(outereach
, innermap!
or outermap!
, innermap
)
– Marcin Kołodziej
2 days ago
1
@MarcinKołodziej, true, buteach/map!
has the disadvantage that the modified array is not returned andmap!/map
creates unneeded temporary arrays.
– Cary Swoveland
2 days ago
add a comment |
It's faster to use a hash or a set than to repeatedly test [1,2,3].include?(n)
.
arr = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
ids = [1,2,3]
Use a hash
h = ids.product(["X"]).to_h
#=> {1=>"X", 2=>"X", 3=>"X"}
arr.map { |a| a.map { |n| h.fetch(n, n) } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
See Hash#fetch.
Use a set
require 'set'
ids = ids.to_set
#=> #<Set: {1, 2, 3}>
arr.map { |a| a.map { |n| ids.include?(n) ? "X" : n } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Replace both map
s with map!
if the array is to be modified in place (mutated).
It's faster to use a hash or a set than to repeatedly test [1,2,3].include?(n)
.
arr = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
ids = [1,2,3]
Use a hash
h = ids.product(["X"]).to_h
#=> {1=>"X", 2=>"X", 3=>"X"}
arr.map { |a| a.map { |n| h.fetch(n, n) } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
See Hash#fetch.
Use a set
require 'set'
ids = ids.to_set
#=> #<Set: {1, 2, 3}>
arr.map { |a| a.map { |n| ids.include?(n) ? "X" : n } }
#=> [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Replace both map
s with map!
if the array is to be modified in place (mutated).
edited 2 days ago
answered 2 days ago
Cary Swoveland
67.6k53965
67.6k53965
For mutating, it is sufficient to use onemap!
(outereach
, innermap!
or outermap!
, innermap
)
– Marcin Kołodziej
2 days ago
1
@MarcinKołodziej, true, buteach/map!
has the disadvantage that the modified array is not returned andmap!/map
creates unneeded temporary arrays.
– Cary Swoveland
2 days ago
add a comment |
For mutating, it is sufficient to use onemap!
(outereach
, innermap!
or outermap!
, innermap
)
– Marcin Kołodziej
2 days ago
1
@MarcinKołodziej, true, buteach/map!
has the disadvantage that the modified array is not returned andmap!/map
creates unneeded temporary arrays.
– Cary Swoveland
2 days ago
For mutating, it is sufficient to use one
map!
(outer each
, inner map!
or outer map!
, inner map
)– Marcin Kołodziej
2 days ago
For mutating, it is sufficient to use one
map!
(outer each
, inner map!
or outer map!
, inner map
)– Marcin Kołodziej
2 days ago
1
1
@MarcinKołodziej, true, but
each/map!
has the disadvantage that the modified array is not returned and map!/map
creates unneeded temporary arrays.– Cary Swoveland
2 days ago
@MarcinKołodziej, true, but
each/map!
has the disadvantage that the modified array is not returned and map!/map
creates unneeded temporary arrays.– Cary Swoveland
2 days ago
add a comment |
Try following, (taking @student_ids = [1, 2, 3])
array1.inject() { |m,a| m << a.map { |x| @student_ids.include?(x) ? 'X' : x } }
# => [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
I don't see any advantage to usinginject
(akareduce
) instead ofmap
.
– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.tmap
&inject
for above case ?
– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to usemap
. Havingmap
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.
– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
add a comment |
Try following, (taking @student_ids = [1, 2, 3])
array1.inject() { |m,a| m << a.map { |x| @student_ids.include?(x) ? 'X' : x } }
# => [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
I don't see any advantage to usinginject
(akareduce
) instead ofmap
.
– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.tmap
&inject
for above case ?
– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to usemap
. Havingmap
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.
– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
add a comment |
Try following, (taking @student_ids = [1, 2, 3])
array1.inject() { |m,a| m << a.map { |x| @student_ids.include?(x) ? 'X' : x } }
# => [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
Try following, (taking @student_ids = [1, 2, 3])
array1.inject() { |m,a| m << a.map { |x| @student_ids.include?(x) ? 'X' : x } }
# => [["X", "X", "X", 4, 5], [7, 8, 9, 10], [11, 12, 13, 14]]
edited 2 days ago
answered 2 days ago
ray
939113
939113
I don't see any advantage to usinginject
(akareduce
) instead ofmap
.
– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.tmap
&inject
for above case ?
– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to usemap
. Havingmap
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.
– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
add a comment |
I don't see any advantage to usinginject
(akareduce
) instead ofmap
.
– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.tmap
&inject
for above case ?
– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to usemap
. Havingmap
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.
– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
I don't see any advantage to using
inject
(aka reduce
) instead of map
.– Cary Swoveland
2 days ago
I don't see any advantage to using
inject
(aka reduce
) instead of map
.– Cary Swoveland
2 days ago
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.t
map
& inject
for above case ?– ray
yesterday
@CarySwoveland oops, I just noticed it. Is there any performance difference for time in above w.r.t
map
& inject
for above case ?– ray
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to use
map
. Having map
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.– Cary Swoveland
yesterday
I don't know about the difference in performance. My guess is that it would be minimal. The main thing, as I see it, is that it would be easier for the reader (and maybe you, in a few months) to understand what you are doing if you were to use
map
. Having map
on the outside immediately tells the reader that you are merely transforming each element of the receiver, rather than doing something more complex with those elements.– Cary Swoveland
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
@CarySwoveland I got it, checked your answer's explanation also!
– ray
yesterday
add a comment |
You can use each_with_index
and replace the item you want:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
array1.each_with_index do |sub_array, index|
sub_array.each_with_index do |item, index2|
array1[index][index2] = 'X' if @student_ids.include?(item)
end
end
What is@student_ids = 1,2,3
? Does it execute properly?
– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.
– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
add a comment |
You can use each_with_index
and replace the item you want:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
array1.each_with_index do |sub_array, index|
sub_array.each_with_index do |item, index2|
array1[index][index2] = 'X' if @student_ids.include?(item)
end
end
What is@student_ids = 1,2,3
? Does it execute properly?
– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.
– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
add a comment |
You can use each_with_index
and replace the item you want:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
array1.each_with_index do |sub_array, index|
sub_array.each_with_index do |item, index2|
array1[index][index2] = 'X' if @student_ids.include?(item)
end
end
You can use each_with_index
and replace the item you want:
array1 = [[1,2,3,4,5],[7,8,9,10],[11,12,13,14]]
@student_ids = [1,2,3]
array1.each_with_index do |sub_array, index|
sub_array.each_with_index do |item, index2|
array1[index][index2] = 'X' if @student_ids.include?(item)
end
end
edited 2 days ago
answered 2 days ago
Roc Khalil
319114
319114
What is@student_ids = 1,2,3
? Does it execute properly?
– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.
– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
add a comment |
What is@student_ids = 1,2,3
? Does it execute properly?
– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.
– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
What is
@student_ids = 1,2,3
? Does it execute properly?– ray
2 days ago
What is
@student_ids = 1,2,3
? Does it execute properly?– ray
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for
@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.– sawa
2 days ago
@ray I was wondering about that too, but it ts actually a shorthand for
@student_ids = [1,2,3]
. Try it. Although, it is doubtful that the OP or Roc intended that.– sawa
2 days ago
@sawa, my bad :|
– ray
2 days ago
@sawa, my bad :|
– ray
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
@ray thanks for mentioning it; I fixed it in the answer :-)
– Roc Khalil
2 days ago
add a comment |
You can do the following:
def remove_student_ids(arr)
arr.each_with_index do |value, index|
arr[index] = 'X' if @student_ids.include?(value) }
end
end
array1.map{ |sub_arr| remove_student_ids(sub_arr)}
add a comment |
You can do the following:
def remove_student_ids(arr)
arr.each_with_index do |value, index|
arr[index] = 'X' if @student_ids.include?(value) }
end
end
array1.map{ |sub_arr| remove_student_ids(sub_arr)}
add a comment |
You can do the following:
def remove_student_ids(arr)
arr.each_with_index do |value, index|
arr[index] = 'X' if @student_ids.include?(value) }
end
end
array1.map{ |sub_arr| remove_student_ids(sub_arr)}
You can do the following:
def remove_student_ids(arr)
arr.each_with_index do |value, index|
arr[index] = 'X' if @student_ids.include?(value) }
end
end
array1.map{ |sub_arr| remove_student_ids(sub_arr)}
edited 2 days ago
sawa
129k27197299
129k27197299
answered 2 days ago
Dimitrius Lachi
32510
32510
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53945277%2fhow-to-use-collect-and-include-for-multidimensional-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
1
Do you mean your input by,
array1 = [[1,2,3,4,5], [7,8,9,10], [11,12,13,14]]
– ray
2 days ago
Oops, yeah sorry
– Jay
2 days ago
@Jay Using the notation
@student_ids = 1,2,3
is rarely seen, and is not reader friendly. Please use the more standard notation@student_ids = [1,2,3]
. I have already edited to do so.– sawa
2 days ago
What you mean is, "I want to replace elements of elements in
array1
that are included in@student_ids
. Sloppy statements bring nothing but trouble in the profession of coding. Be precise!– Cary Swoveland
2 days ago