Ich möchte ein Array in ein Array von Arrays zu brechen.Iterate bedingt durch Array
test_ary = %w(101 This is the first label 102 This is the second label 103 This is
the third label 104 This is the fourth label)
result = iterate_array(test_ary)
Erwartete Ausgabe:
#⇒ [
# "101 This is the first label",
# "102 This is the second label",
# "103 This is the third label",
# "104 This is the fourth label" ]
schrieb ich die folgende Methode:
def iterate_array(ary)
temp_ary = []
final_ary =[]
idx = 0
temp_ary.push ary[idx]
idx +=1
done = ary.length - 1
while idx <= done
if ary[idx] =~ /\d/
final_ary.push temp_ary
temp_ary = []
temp_ary.push ary[idx]
else
temp_ary.push ary[idx]
end
idx +=1
end
final_ary.push temp_ary
returned_ary=final_ary.map {|nested_ary| nested_ary.join(" ")}
returned_ary
end
ich da denken muss ein einfacher und eleganter Weg. Irgendwelche Ideen?
Hallo M, würde es helfen, wenn Sie würde die erwartete Ausgabe enthalten, würde es helfen, einige Ihrer Formatierungsfehler zu bereinigen. – mwp
Sorry Leute. Ich bin gerade dabei, die Formatierung zu verstehen. Hoffentlich habe ich es aufgeräumt (w die Hilfe von Vlad) - M –